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

HotelsDialog.cs 4.6 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
  1. namespace MultiDialogsBot.Dialogs
  2. {
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Threading.Tasks;
  7. using System.Web;
  8. using Microsoft.Bot.Builder.Dialogs;
  9. using Microsoft.Bot.Builder.FormFlow;
  10. using Microsoft.Bot.Connector;
  11. [Serializable]
  12. public class HotelsDialog : IDialog<object>
  13. {
  14. public async Task StartAsync(IDialogContext context)
  15. {
  16. await context.PostAsync("Welcome to the Hotels finder!");
  17. var hotelsFormDialog = FormDialog.FromForm(this.BuildHotelsForm, FormOptions.PromptInStart);
  18. context.Call(hotelsFormDialog, this.ResumeAfterHotelsFormDialog);
  19. }
  20. private IForm<HotelsQuery> BuildHotelsForm()
  21. {
  22. OnCompletionAsyncDelegate<HotelsQuery> processHotelsSearch = async (context, state) =>
  23. {
  24. await context.PostAsync($"Ok. Searching for Hotels in {state.Destination} from {state.CheckIn.ToString("MM/dd")} to {state.CheckIn.AddDays(state.Nights).ToString("MM/dd")}...");
  25. };
  26. return new FormBuilder<HotelsQuery>()
  27. .Field(nameof(HotelsQuery.Destination))
  28. .Message("Looking for hotels in {Destination}...")
  29. .AddRemainingFields()
  30. .OnCompletion(processHotelsSearch)
  31. .Build();
  32. }
  33. private async Task ResumeAfterHotelsFormDialog(IDialogContext context, IAwaitable<HotelsQuery> result)
  34. {
  35. try
  36. {
  37. var searchQuery = await result;
  38. var hotels = await this.GetHotelsAsync(searchQuery);
  39. await context.PostAsync($"I found in total {hotels.Count()} hotels for your dates:");
  40. var resultMessage = context.MakeMessage();
  41. resultMessage.AttachmentLayout = AttachmentLayoutTypes.Carousel;
  42. resultMessage.Attachments = new List<Attachment>();
  43. foreach (var hotel in hotels)
  44. {
  45. HeroCard heroCard = new HeroCard()
  46. {
  47. Title = hotel.Name,
  48. Subtitle = $"{hotel.Rating} starts. {hotel.NumberOfReviews} reviews. From ${hotel.PriceStarting} per night.",
  49. Images = new List<CardImage>()
  50. {
  51. new CardImage() { Url = hotel.Image }
  52. },
  53. Buttons = new List<CardAction>()
  54. {
  55. new CardAction()
  56. {
  57. Title = "More details",
  58. Type = ActionTypes.OpenUrl,
  59. Value = $"https://www.bing.com/search?q=hotels+in+" + HttpUtility.UrlEncode(hotel.Location)
  60. }
  61. }
  62. };
  63. resultMessage.Attachments.Add(heroCard.ToAttachment());
  64. }
  65. await context.PostAsync(resultMessage);
  66. }
  67. catch (FormCanceledException ex)
  68. {
  69. string reply;
  70. if (ex.InnerException == null)
  71. {
  72. reply = "You have canceled the operation. Quitting from the HotelsDialog";
  73. }
  74. else
  75. {
  76. reply = $"Oops! Something went wrong :( Technical Details: {ex.InnerException.Message}";
  77. }
  78. await context.PostAsync(reply);
  79. }
  80. finally
  81. {
  82. context.Done<object>(null);
  83. }
  84. }
  85. private async Task<IEnumerable<Hotel>> GetHotelsAsync(HotelsQuery searchQuery)
  86. {
  87. var hotels = new List<Hotel>();
  88. // Filling the hotels results manually just for demo purposes
  89. for (int i = 1; i <= 5; i++)
  90. {
  91. var random = new Random(i);
  92. Hotel hotel = new Hotel()
  93. {
  94. Name = $"{searchQuery.Destination} Hotel {i}",
  95. Location = searchQuery.Destination,
  96. Rating = random.Next(1, 5),
  97. NumberOfReviews = random.Next(0, 5000),
  98. PriceStarting = random.Next(80, 450),
  99. Image = $"https://placeholdit.imgix.net/~text?txtsize=35&txt=Hotel+{i}&w=500&h=260"
  100. };
  101. hotels.Add(hotel);
  102. }
  103. hotels.Sort((h1, h2) => h1.PriceStarting.CompareTo(h2.PriceStarting));
  104. return hotels;
  105. }
  106. }
  107. }
Tip!

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

Comments

Loading...