forked from microsoft/BotBuilder-Samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PagedCarouselDialog.cs
92 lines (75 loc) · 2.97 KB
/
PagedCarouselDialog.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
namespace ContosoFlowers.BotAssets.Dialogs
{
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.Bot.Builder.Dialogs;
using Microsoft.Bot.Connector;
using Properties;
[Serializable]
public abstract class PagedCarouselDialog<T> : IDialog<T>
{
private int pageNumber = 1;
private int pageSize = 5;
public virtual string Prompt { get; }
public async Task StartAsync(IDialogContext context)
{
await context.PostAsync(this.Prompt ?? Resources.PagedCarouselDialog_DefaultPrompt);
await this.ShowProducts(context);
context.Wait(this.MessageReceivedAsync);
}
public abstract PagedCarouselCards GetCarouselCards(int pageNumber, int pageSize);
public abstract Task ProcessMessageReceived(IDialogContext context, string message);
protected async Task ShowProducts(IDialogContext context)
{
var reply = context.MakeMessage();
reply.AttachmentLayout = AttachmentLayoutTypes.Carousel;
reply.Attachments = new List<Attachment>();
var productsResult = this.GetCarouselCards(this.pageNumber, this.pageSize);
foreach (HeroCard productCard in productsResult.Cards)
{
reply.Attachments.Add(productCard.ToAttachment());
}
await context.PostAsync(reply);
if (productsResult.TotalCount > this.pageNumber * this.pageSize)
{
await this.ShowMoreOptions(context);
}
}
protected async Task MessageReceivedAsync(IDialogContext context, IAwaitable<IMessageActivity> result)
{
var message = await result;
// TODO: validation
if (message.Text.Equals(Resources.PagedCarouselDialog_ShowMe, StringComparison.InvariantCultureIgnoreCase))
{
this.pageNumber++;
await this.StartAsync(context);
}
else
{
await this.ProcessMessageReceived(context, message.Text);
}
}
private async Task ShowMoreOptions(IDialogContext context)
{
var moreOptionsReply = context.MakeMessage();
moreOptionsReply.Attachments = new List<Attachment>
{
new HeroCard()
{
Text = Resources.PagedCarouselDialog_MoreOptions,
Buttons = new List<CardAction>
{
new CardAction(ActionTypes.ImBack, Resources.PagedCarouselDialog_ShowMe, value: Resources.PagedCarouselDialog_ShowMe)
}
}.ToAttachment()
};
await context.PostAsync(moreOptionsReply);
}
public class PagedCarouselCards
{
public IEnumerable<HeroCard> Cards { get; set; }
public int TotalCount { get; set; }
}
}
}