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

RootDialog.cs 15 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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
  1. namespace ContosoFlowers.Dialogs
  2. {
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Globalization;
  6. using System.Threading.Tasks;
  7. using System.Web;
  8. using AutoMapper;
  9. using BotAssets.Dialogs;
  10. using BotAssets.Extensions;
  11. using Microsoft.Bot.Builder.Dialogs;
  12. using Microsoft.Bot.Builder.FormFlow;
  13. using Microsoft.Bot.Builder.Location;
  14. using Microsoft.Bot.Connector;
  15. using Models;
  16. using Properties;
  17. using Services;
  18. using Services.Models;
  19. [Serializable]
  20. public class RootDialog : IDialog<object>
  21. {
  22. private readonly string checkoutUriFormat;
  23. private readonly IContosoFlowersDialogFactory dialogFactory;
  24. private readonly IOrdersService ordersService;
  25. private Models.Order order;
  26. private ResumptionCookie resumptionCookie;
  27. public RootDialog(string checkoutUriFormat, IContosoFlowersDialogFactory dialogFactory, IOrdersService ordersService)
  28. {
  29. this.checkoutUriFormat = checkoutUriFormat;
  30. this.dialogFactory = dialogFactory;
  31. this.ordersService = ordersService;
  32. }
  33. public async Task StartAsync(IDialogContext context)
  34. {
  35. context.Wait(this.MessageReceivedAsync);
  36. }
  37. public virtual async Task MessageReceivedAsync(IDialogContext context, IAwaitable<IMessageActivity> result)
  38. {
  39. var message = await result;
  40. if (this.resumptionCookie == null)
  41. {
  42. this.resumptionCookie = new ResumptionCookie(message);
  43. }
  44. await this.WelcomeMessageAsync(context);
  45. }
  46. private async Task WelcomeMessageAsync(IDialogContext context)
  47. {
  48. var reply = context.MakeMessage();
  49. var options = new[]
  50. {
  51. Resources.RootDialog_Welcome_Orders,
  52. Resources.RootDialog_Welcome_Support
  53. };
  54. reply.AddHeroCard(
  55. Resources.RootDialog_Welcome_Title,
  56. Resources.RootDialog_Welcome_Subtitle,
  57. options,
  58. new[] { "https://placeholdit.imgix.net/~text?txtsize=56&txt=Contoso%20Flowers&w=640&h=330" });
  59. await context.PostAsync(reply);
  60. context.Wait(this.OnOptionSelected);
  61. }
  62. private async Task OnOptionSelected(IDialogContext context, IAwaitable<IMessageActivity> result)
  63. {
  64. var message = await result;
  65. if (message.Text == Resources.RootDialog_Welcome_Orders)
  66. {
  67. this.order = new Models.Order();
  68. // BotBuilder's LocationDialog
  69. // Leverage DI to inject other parameters
  70. var locationDialog = this.dialogFactory.Create<LocationDialog>(
  71. new Dictionary<string, object>()
  72. {
  73. { "prompt", string.Format(CultureInfo.CurrentCulture, Resources.RootDialog_DeliveryAddress_Prompt, message.From.Name ?? "User") },
  74. { "channelId", context.Activity.ChannelId }
  75. });
  76. context.Call(locationDialog, this.AfterDeliveryAddress);
  77. }
  78. else if (message.Text == Resources.RootDialog_Welcome_Support)
  79. {
  80. await this.StartOverAsync(context, Resources.RootDialog_Support_Message);
  81. }
  82. else
  83. {
  84. await this.StartOverAsync(context, Resources.RootDialog_Welcome_Error);
  85. }
  86. }
  87. private async Task AfterDeliveryAddress(IDialogContext context, IAwaitable<Place> result)
  88. {
  89. try
  90. {
  91. var place = await result;
  92. var formattedAddress = place.GetPostalAddress().FormattedAddress;
  93. this.order.DeliveryAddress = formattedAddress;
  94. context.Call(this.dialogFactory.Create<FlowerCategoriesDialog>(), this.AfterFlowerCategorySelected);
  95. }
  96. catch (TooManyAttemptsException)
  97. {
  98. await this.StartOverAsync(context, Resources.RootDialog_TooManyAttempts);
  99. }
  100. }
  101. private async Task AfterFlowerCategorySelected(IDialogContext context, IAwaitable<string> result)
  102. {
  103. this.order.FlowerCategoryName = await result;
  104. context.Call(this.dialogFactory.Create<BouquetsDialog, string>(this.order.FlowerCategoryName), this.AfterBouquetSelected);
  105. }
  106. private async Task AfterBouquetSelected(IDialogContext context, IAwaitable<Bouquet> result)
  107. {
  108. var selectedBouquet = await result;
  109. this.order.Bouquet = selectedBouquet;
  110. await context.PostAsync(string.Format(CultureInfo.CurrentCulture, Resources.RootDialog_Bouquet_Selected, this.order.Bouquet.Name));
  111. PromptDialog.Choice(context, this.AfterDeliveryDateSelected, new[] { StringConstants.Today, StringConstants.Tomorrow }, Resources.RootDialog_DeliveryDate_Prompt);
  112. }
  113. private async Task AfterDeliveryDateSelected(IDialogContext context, IAwaitable<string> result)
  114. {
  115. try
  116. {
  117. this.order.DeliveryDate = (await result == StringConstants.Today) ? DateTime.Today : DateTime.Today.AddDays(1);
  118. await context.PostAsync(string.Format(CultureInfo.CurrentCulture, Resources.RootDialog_DeliveryDate_Selected, this.order.Bouquet.Name, this.order.DeliveryDate.ToShortDateString()));
  119. UserPreferences userPreferences;
  120. if (context.UserData.TryGetValue(StringConstants.UserPreferencesKey, out userPreferences))
  121. {
  122. this.order.SenderEmail = userPreferences.SenderEmail;
  123. this.order.SenderPhoneNumber = userPreferences.SenderPhoneNumber;
  124. this.order.AskToUseSavedSenderInfo = !string.IsNullOrWhiteSpace(this.order.SenderEmail) && !string.IsNullOrWhiteSpace(this.order.SenderPhoneNumber);
  125. }
  126. var orderForm = new FormDialog<Models.Order>(this.order, Models.Order.BuildOrderForm, FormOptions.PromptInStart);
  127. context.Call(orderForm, this.AfterOrderForm);
  128. }
  129. catch (TooManyAttemptsException)
  130. {
  131. await this.StartOverAsync(context, Resources.RootDialog_TooManyAttempts);
  132. }
  133. }
  134. private async Task AfterOrderForm(IDialogContext context, IAwaitable<Models.Order> result)
  135. {
  136. try
  137. {
  138. await result;
  139. if (this.order.SaveSenderInfo)
  140. {
  141. context.UserData.UpdateValue<UserPreferences>(
  142. StringConstants.UserPreferencesKey,
  143. userPreferences =>
  144. {
  145. userPreferences.SenderEmail = this.order.SenderEmail;
  146. userPreferences.SenderPhoneNumber = this.order.SenderPhoneNumber;
  147. });
  148. }
  149. var savedAddresses = new Dictionary<string, string>();
  150. UserPreferences preferences;
  151. if (context.UserData.TryGetValue(StringConstants.UserPreferencesKey, out preferences))
  152. {
  153. savedAddresses = preferences.BillingAddresses;
  154. }
  155. var addressDialog = this.dialogFactory.CreateSavedAddressDialog(
  156. Resources.RootDialog_BillingAddress_Prompt,
  157. Resources.RootDialog_BillingAddress_SelectSaved,
  158. Resources.RootDialog_BillingAddress_ShouldSave,
  159. savedAddresses,
  160. new[] { StringConstants.HomeBillingAddress, StringConstants.WorkBillingAddress });
  161. context.Call(addressDialog, this.AfterBillingAddress);
  162. }
  163. catch (FormCanceledException e)
  164. {
  165. string reply;
  166. if (e.InnerException == null)
  167. {
  168. reply = Resources.RootDialog_Order_Cancelation;
  169. }
  170. else
  171. {
  172. reply = string.Format(CultureInfo.CurrentCulture, Resources.RootDialog_Order_Error, e.InnerException.Message);
  173. }
  174. await this.StartOverAsync(context, reply);
  175. }
  176. catch (TooManyAttemptsException)
  177. {
  178. await this.StartOverAsync(context, Resources.RootDialog_TooManyAttempts);
  179. }
  180. }
  181. private async Task AfterBillingAddress(IDialogContext context, IAwaitable<SavedAddressDialog.SavedAddressResult> result)
  182. {
  183. try
  184. {
  185. var addressResult = await result;
  186. this.order.BillingAddress = addressResult.Value;
  187. if (!string.IsNullOrWhiteSpace(addressResult.SaveOptionName))
  188. {
  189. context.UserData.UpdateValue<UserPreferences>(
  190. StringConstants.UserPreferencesKey,
  191. userPreferences =>
  192. {
  193. userPreferences.BillingAddresses = userPreferences.BillingAddresses ?? new Dictionary<string, string>();
  194. userPreferences.BillingAddresses[addressResult.SaveOptionName.ToLower()] = this.order.BillingAddress;
  195. });
  196. }
  197. await this.PaymentSelectionAsync(context);
  198. }
  199. catch (TooManyAttemptsException)
  200. {
  201. await this.StartOverAsync(context, Resources.RootDialog_TooManyAttempts);
  202. }
  203. }
  204. private async Task PaymentSelectionAsync(IDialogContext context)
  205. {
  206. var paymentReply = context.MakeMessage();
  207. var serviceModel = Mapper.Map<Services.Models.Order>(this.order);
  208. if (this.order.OrderID == null)
  209. {
  210. this.order.OrderID = this.ordersService.PlacePendingOrder(serviceModel);
  211. }
  212. var checkoutUrl = this.BuildCheckoutUrl(this.order.OrderID);
  213. paymentReply.Attachments = new List<Attachment>
  214. {
  215. new HeroCard()
  216. {
  217. Text = string.Format(CultureInfo.CurrentCulture, Resources.RootDialog_Checkout_Prompt, this.order.Bouquet.Price.ToString("C")),
  218. Buttons = new List<CardAction>
  219. {
  220. new CardAction(ActionTypes.OpenUrl, Resources.RootDialog_Checkout_Continue, value: checkoutUrl),
  221. new CardAction(ActionTypes.ImBack, Resources.RootDialog_Checkout_Cancel, value: Resources.RootDialog_Checkout_Cancel)
  222. }
  223. }.ToAttachment()
  224. };
  225. await context.PostAsync(paymentReply);
  226. context.Wait(this.AfterPaymentSelection);
  227. }
  228. private string BuildCheckoutUrl(string orderID)
  229. {
  230. var encodedCookie = this.resumptionCookie.GZipSerialize();
  231. var uriBuilder = new UriBuilder(this.checkoutUriFormat);
  232. var query = HttpUtility.ParseQueryString(uriBuilder.Query);
  233. query["state"] = encodedCookie;
  234. query["orderID"] = orderID;
  235. uriBuilder.Query = query.ToString();
  236. var checkoutUrl = uriBuilder.Uri.ToString();
  237. return checkoutUrl;
  238. }
  239. private async Task AfterPaymentSelection(IDialogContext context, IAwaitable<IMessageActivity> result)
  240. {
  241. var selection = await result;
  242. if (selection.Text == Resources.RootDialog_Checkout_Cancel)
  243. {
  244. var options = new[] { Resources.RootDialog_Menu_StartOver, Resources.RootDialog_Menu_Cancel, Resources.RootDialog_Welcome_Support };
  245. PromptDialog.Choice(context, this.AfterChangedMyMind, options, Resources.RootDialog_Menu_Prompt);
  246. }
  247. else
  248. {
  249. var serviceOrder = this.ordersService.RetrieveOrder(selection.Text);
  250. if (serviceOrder == null || !serviceOrder.Payed)
  251. {
  252. await context.PostAsync(string.Format(CultureInfo.CurrentCulture, Resources.RootDialog_Checkout_Error, selection.Text));
  253. await this.PaymentSelectionAsync(context);
  254. return;
  255. }
  256. var message = context.MakeMessage();
  257. message.Text = string.Format(
  258. CultureInfo.CurrentCulture,
  259. Resources.RootDialog_Receipt_Text,
  260. selection.Text,
  261. this.order.Bouquet.Name,
  262. this.order.RecipientFirstName,
  263. this.order.RecipientLastName,
  264. this.order.Note);
  265. message.Attachments.Add(this.GetReceiptCard());
  266. await this.StartOverAsync(context, message);
  267. }
  268. }
  269. private async Task AfterChangedMyMind(IDialogContext context, IAwaitable<string> result)
  270. {
  271. try
  272. {
  273. var option = await result;
  274. if (option == Resources.RootDialog_Menu_StartOver)
  275. {
  276. await this.StartOverAsync(context, Resources.RootDialog_Welcome_Message);
  277. }
  278. else if (option == Resources.RootDialog_Menu_Cancel)
  279. {
  280. await this.PaymentSelectionAsync(context);
  281. }
  282. else
  283. {
  284. await this.StartOverAsync(context, Resources.RootDialog_Support_Message);
  285. }
  286. }
  287. catch (TooManyAttemptsException)
  288. {
  289. await this.StartOverAsync(context, Resources.RootDialog_TooManyAttempts);
  290. }
  291. }
  292. private Attachment GetReceiptCard()
  293. {
  294. var order = this.ordersService.RetrieveOrder(this.order.OrderID);
  295. var creditCardOffuscated = order.PaymentDetails.CreditCardNumber.Substring(0, 4) + "-****";
  296. var receiptCard = new ReceiptCard
  297. {
  298. Title = Resources.RootDialog_Receipt_Title,
  299. Facts = new List<Fact>
  300. {
  301. new Fact(Resources.RootDialog_Receipt_OrderID, order.OrderID),
  302. new Fact(Resources.RootDialog_Receipt_PaymentMethod, creditCardOffuscated)
  303. },
  304. Items = new List<ReceiptItem>
  305. {
  306. new ReceiptItem(
  307. title: order.FlowerCategoryName,
  308. subtitle: order.Bouquet.Name,
  309. price: order.Bouquet.Price.ToString("C"),
  310. image: new CardImage(order.Bouquet.ImageUrl)),
  311. },
  312. Total = order.Bouquet.Price.ToString("C")
  313. };
  314. return receiptCard.ToAttachment();
  315. }
  316. private async Task StartOverAsync(IDialogContext context, string text)
  317. {
  318. var message = context.MakeMessage();
  319. message.Text = text;
  320. await this.StartOverAsync(context, message);
  321. }
  322. private async Task StartOverAsync(IDialogContext context, IMessageActivity message)
  323. {
  324. await context.PostAsync(message);
  325. this.order = new Models.Order();
  326. await this.WelcomeMessageAsync(context);
  327. }
  328. }
  329. }
Tip!

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

Comments

Loading...