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

SavedAddressDialog.cs 3.7 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
  1. namespace ContosoFlowers.BotAssets.Dialogs
  2. {
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Threading.Tasks;
  7. using Microsoft.Bot.Builder.Dialogs;
  8. using Microsoft.Bot.Builder.Location;
  9. using Microsoft.Bot.Connector;
  10. using Properties;
  11. [Serializable]
  12. public class SavedAddressDialog : IDialog<SavedAddressDialog.SavedAddressResult>
  13. {
  14. private readonly IDictionary<string, string> savedAddresses;
  15. private readonly IEnumerable<string> saveOptionNames;
  16. private readonly string prompt;
  17. private readonly string useSavedAddressPrompt;
  18. private readonly string saveAddressPrompt;
  19. private readonly IDialogFactory dialogFactory;
  20. private string currentAddress;
  21. public SavedAddressDialog(
  22. string prompt,
  23. string useSavedAddressPrompt,
  24. string saveAddressPrompt,
  25. IDictionary<string, string> savedAddresses,
  26. IEnumerable<string> saveOptionNames,
  27. IDialogFactory dialogFactory)
  28. {
  29. this.savedAddresses = savedAddresses ?? new Dictionary<string, string>();
  30. this.saveOptionNames = saveOptionNames;
  31. this.prompt = prompt;
  32. this.useSavedAddressPrompt = useSavedAddressPrompt;
  33. this.saveAddressPrompt = saveAddressPrompt;
  34. this.dialogFactory = dialogFactory;
  35. }
  36. public async Task StartAsync(IDialogContext context)
  37. {
  38. if (this.savedAddresses.Any())
  39. {
  40. PromptDialog.Choice(context, this.AfterSelectSavedAddress, this.savedAddresses.Values.Concat(new[] { Resources.SavedAddressDialog_AddNewAddress }), this.useSavedAddressPrompt);
  41. }
  42. else
  43. {
  44. this.AddressPrompt(context);
  45. }
  46. }
  47. private void AddressPrompt(IDialogContext context)
  48. {
  49. // BotBuilder's LocationDialog
  50. // Leverage DI to inject other parameters
  51. var locationDialog = this.dialogFactory.Create<LocationDialog>(
  52. new Dictionary<string, object>()
  53. {
  54. { "prompt", this.prompt },
  55. { "channelId", context.Activity.ChannelId }
  56. });
  57. context.Call(locationDialog, this.AfterAddressPrompt);
  58. }
  59. private async Task AfterAddressPrompt(IDialogContext context, IAwaitable<Place> result)
  60. {
  61. var place = await result;
  62. this.currentAddress = place.GetPostalAddress().FormattedAddress;
  63. PromptDialog.Choice(context, this.AfterSelectToSaveAddress, this.saveOptionNames.Concat(new[] { Resources.SavedAddressDialog_NotThisTime }), this.saveAddressPrompt);
  64. }
  65. private async Task AfterSelectToSaveAddress(IDialogContext context, IAwaitable<string> result)
  66. {
  67. var saveOptionName = await result;
  68. saveOptionName = saveOptionName == Resources.SavedAddressDialog_NotThisTime ? null : saveOptionName;
  69. context.Done(new SavedAddressResult { Value = this.currentAddress, SaveOptionName = saveOptionName });
  70. }
  71. private async Task AfterSelectSavedAddress(IDialogContext context, IAwaitable<string> result)
  72. {
  73. this.currentAddress = await result;
  74. if (this.currentAddress == Resources.SavedAddressDialog_AddNewAddress)
  75. {
  76. this.AddressPrompt(context);
  77. }
  78. else
  79. {
  80. context.Done(new SavedAddressResult { Value = this.currentAddress });
  81. }
  82. }
  83. public class SavedAddressResult
  84. {
  85. public string Value { get; set; }
  86. public string SaveOptionName { get; set; }
  87. }
  88. }
  89. }
Tip!

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

Comments

Loading...