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

RootLuisDialog.cs 8.3 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
  1. namespace LuisBot.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.Builder.Luis;
  11. using Microsoft.Bot.Builder.Luis.Models;
  12. using Microsoft.Bot.Connector;
  13. [LuisModel("YourModelId", "YourSubscriptionKey")]
  14. [Serializable]
  15. public class RootLuisDialog : LuisDialog<object>
  16. {
  17. private const string EntityGeographyCity = "builtin.geography.city";
  18. private const string EntityHotelName = "Hotel";
  19. private const string EntityAirportCode = "AirportCode";
  20. private IList<string> titleOptions = new List<string> { "“Very stylish, great stay, great staff”", "“good hotel awful meals”", "“Need more attention to little things”", "“Lovely small hotel ideally situated to explore the area.”", "“Positive surprise”", "“Beautiful suite and resort”" };
  21. [LuisIntent("")]
  22. [LuisIntent("None")]
  23. public async Task None(IDialogContext context, LuisResult result)
  24. {
  25. string message = $"Sorry, I did not understand '{result.Query}'. Type 'help' if you need assistance.";
  26. await context.PostAsync(message);
  27. context.Wait(this.MessageReceived);
  28. }
  29. [LuisIntent("SearchHotels")]
  30. public async Task Search(IDialogContext context, IAwaitable<IMessageActivity> activity, LuisResult result)
  31. {
  32. var message = await activity;
  33. await context.PostAsync($"Welcome to the Hotels finder! We are analyzing your message: '{message.Text}'...");
  34. var hotelsQuery = new HotelsQuery();
  35. EntityRecommendation cityEntityRecommendation;
  36. if (result.TryFindEntity(EntityGeographyCity, out cityEntityRecommendation))
  37. {
  38. cityEntityRecommendation.Type = "Destination";
  39. }
  40. var hotelsFormDialog = new FormDialog<HotelsQuery>(hotelsQuery, this.BuildHotelsForm, FormOptions.PromptInStart, result.Entities);
  41. context.Call(hotelsFormDialog, this.ResumeAfterHotelsFormDialog);
  42. }
  43. [LuisIntent("ShowHotelsReviews")]
  44. public async Task Reviews(IDialogContext context, LuisResult result)
  45. {
  46. EntityRecommendation hotelEntityRecommendation;
  47. if (result.TryFindEntity(EntityHotelName, out hotelEntityRecommendation))
  48. {
  49. await context.PostAsync($"Looking for reviews of '{hotelEntityRecommendation.Entity}'...");
  50. var resultMessage = context.MakeMessage();
  51. resultMessage.AttachmentLayout = AttachmentLayoutTypes.Carousel;
  52. resultMessage.Attachments = new List<Attachment>();
  53. for (int i = 0; i < 5; i++)
  54. {
  55. var random = new Random(i);
  56. ThumbnailCard thumbnailCard = new ThumbnailCard()
  57. {
  58. Title = this.titleOptions[random.Next(0, this.titleOptions.Count - 1)],
  59. Text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris odio magna, sodales vel ligula sit amet, vulputate vehicula velit. Nulla quis consectetur neque, sed commodo metus.",
  60. Images = new List<CardImage>()
  61. {
  62. new CardImage() { Url = "https://upload.wikimedia.org/wikipedia/en/e/ee/Unknown-person.gif" }
  63. },
  64. };
  65. resultMessage.Attachments.Add(thumbnailCard.ToAttachment());
  66. }
  67. await context.PostAsync(resultMessage);
  68. }
  69. context.Wait(this.MessageReceived);
  70. }
  71. [LuisIntent("Help")]
  72. public async Task Help(IDialogContext context, LuisResult result)
  73. {
  74. await context.PostAsync("Hi! Try asking me things like 'search hotels in Seattle', 'search hotels near LAX airport' or 'show me the reviews of The Bot Resort'");
  75. context.Wait(this.MessageReceived);
  76. }
  77. private IForm<HotelsQuery> BuildHotelsForm()
  78. {
  79. OnCompletionAsyncDelegate<HotelsQuery> processHotelsSearch = async (context, state) =>
  80. {
  81. var message = "Searching for hotels";
  82. if (!string.IsNullOrEmpty(state.Destination))
  83. {
  84. message += $" in {state.Destination}...";
  85. }
  86. else if (!string.IsNullOrEmpty(state.AirportCode))
  87. {
  88. message += $" near {state.AirportCode.ToUpperInvariant()} airport...";
  89. }
  90. await context.PostAsync(message);
  91. };
  92. return new FormBuilder<HotelsQuery>()
  93. .Field(nameof(HotelsQuery.Destination), (state) => string.IsNullOrEmpty(state.AirportCode))
  94. .Field(nameof(HotelsQuery.AirportCode), (state) => string.IsNullOrEmpty(state.Destination))
  95. .OnCompletion(processHotelsSearch)
  96. .Build();
  97. }
  98. private async Task ResumeAfterHotelsFormDialog(IDialogContext context, IAwaitable<HotelsQuery> result)
  99. {
  100. try
  101. {
  102. var searchQuery = await result;
  103. var hotels = await this.GetHotelsAsync(searchQuery);
  104. await context.PostAsync($"I found {hotels.Count()} hotels:");
  105. var resultMessage = context.MakeMessage();
  106. resultMessage.AttachmentLayout = AttachmentLayoutTypes.Carousel;
  107. resultMessage.Attachments = new List<Attachment>();
  108. foreach (var hotel in hotels)
  109. {
  110. HeroCard heroCard = new HeroCard()
  111. {
  112. Title = hotel.Name,
  113. Subtitle = $"{hotel.Rating} starts. {hotel.NumberOfReviews} reviews. From ${hotel.PriceStarting} per night.",
  114. Images = new List<CardImage>()
  115. {
  116. new CardImage() { Url = hotel.Image }
  117. },
  118. Buttons = new List<CardAction>()
  119. {
  120. new CardAction()
  121. {
  122. Title = "More details",
  123. Type = ActionTypes.OpenUrl,
  124. Value = $"https://www.bing.com/search?q=hotels+in+" + HttpUtility.UrlEncode(hotel.Location)
  125. }
  126. }
  127. };
  128. resultMessage.Attachments.Add(heroCard.ToAttachment());
  129. }
  130. await context.PostAsync(resultMessage);
  131. }
  132. catch (FormCanceledException ex)
  133. {
  134. string reply;
  135. if (ex.InnerException == null)
  136. {
  137. reply = "You have canceled the operation.";
  138. }
  139. else
  140. {
  141. reply = $"Oops! Something went wrong :( Technical Details: {ex.InnerException.Message}";
  142. }
  143. await context.PostAsync(reply);
  144. }
  145. finally
  146. {
  147. context.Done<object>(null);
  148. }
  149. }
  150. private async Task<IEnumerable<Hotel>> GetHotelsAsync(HotelsQuery searchQuery)
  151. {
  152. var hotels = new List<Hotel>();
  153. // Filling the hotels results manually just for demo purposes
  154. for (int i = 1; i <= 5; i++)
  155. {
  156. var random = new Random(i);
  157. Hotel hotel = new Hotel()
  158. {
  159. Name = $"{searchQuery.Destination ?? searchQuery.AirportCode} Hotel {i}",
  160. Location = searchQuery.Destination ?? searchQuery.AirportCode,
  161. Rating = random.Next(1, 5),
  162. NumberOfReviews = random.Next(0, 5000),
  163. PriceStarting = random.Next(80, 450),
  164. Image = $"https://placeholdit.imgix.net/~text?txtsize=35&txt=Hotel+{i}&w=500&h=260"
  165. };
  166. hotels.Add(hotel);
  167. }
  168. hotels.Sort((h1, h2) => h1.PriceStarting.CompareTo(h2.PriceStarting));
  169. return hotels;
  170. }
  171. }
  172. }
Tip!

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

Comments

Loading...