Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

PagedCarouselDialog.cs 3.0 KB

You have to be logged in to leave a comment. Sign In
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
  1. namespace ContosoFlowers.BotAssets.Dialogs
  2. {
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Threading.Tasks;
  6. using Microsoft.Bot.Builder.Dialogs;
  7. using Microsoft.Bot.Connector;
  8. using Properties;
  9. [Serializable]
  10. public abstract class PagedCarouselDialog<T> : IDialog<T>
  11. {
  12. private int pageNumber = 1;
  13. private int pageSize = 5;
  14. public virtual string Prompt { get; }
  15. public async Task StartAsync(IDialogContext context)
  16. {
  17. await context.PostAsync(this.Prompt ?? Resources.PagedCarouselDialog_DefaultPrompt);
  18. await this.ShowProducts(context);
  19. context.Wait(this.MessageReceivedAsync);
  20. }
  21. public abstract PagedCarouselCards GetCarouselCards(int pageNumber, int pageSize);
  22. public abstract Task ProcessMessageReceived(IDialogContext context, string message);
  23. protected async Task ShowProducts(IDialogContext context)
  24. {
  25. var reply = context.MakeMessage();
  26. reply.AttachmentLayout = AttachmentLayoutTypes.Carousel;
  27. reply.Attachments = new List<Attachment>();
  28. var productsResult = this.GetCarouselCards(this.pageNumber, this.pageSize);
  29. foreach (HeroCard productCard in productsResult.Cards)
  30. {
  31. reply.Attachments.Add(productCard.ToAttachment());
  32. }
  33. await context.PostAsync(reply);
  34. if (productsResult.TotalCount > this.pageNumber * this.pageSize)
  35. {
  36. await this.ShowMoreOptions(context);
  37. }
  38. }
  39. protected async Task MessageReceivedAsync(IDialogContext context, IAwaitable<IMessageActivity> result)
  40. {
  41. var message = await result;
  42. // TODO: validation
  43. if (message.Text.Equals(Resources.PagedCarouselDialog_ShowMe, StringComparison.InvariantCultureIgnoreCase))
  44. {
  45. this.pageNumber++;
  46. await this.StartAsync(context);
  47. }
  48. else
  49. {
  50. await this.ProcessMessageReceived(context, message.Text);
  51. }
  52. }
  53. private async Task ShowMoreOptions(IDialogContext context)
  54. {
  55. var moreOptionsReply = context.MakeMessage();
  56. moreOptionsReply.Attachments = new List<Attachment>
  57. {
  58. new HeroCard()
  59. {
  60. Text = Resources.PagedCarouselDialog_MoreOptions,
  61. Buttons = new List<CardAction>
  62. {
  63. new CardAction(ActionTypes.ImBack, Resources.PagedCarouselDialog_ShowMe, value: Resources.PagedCarouselDialog_ShowMe)
  64. }
  65. }.ToAttachment()
  66. };
  67. await context.PostAsync(moreOptionsReply);
  68. }
  69. public class PagedCarouselCards
  70. {
  71. public IEnumerable<HeroCard> Cards { get; set; }
  72. public int TotalCount { get; set; }
  73. }
  74. }
  75. }
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...