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

SettingsDialog.cs 8.8 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
  1. namespace ContosoFlowers.Dialogs
  2. {
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Globalization;
  6. using System.Threading.Tasks;
  7. using BotAssets;
  8. using BotAssets.Dialogs;
  9. using BotAssets.Extensions;
  10. using Microsoft.Bot.Builder.Dialogs;
  11. using Microsoft.Bot.Builder.Internals.Fibers;
  12. using Microsoft.Bot.Builder.Location;
  13. using Microsoft.Bot.Connector;
  14. using Models;
  15. using Properties;
  16. [Serializable]
  17. public class SettingsDialog : IDialog<object>
  18. {
  19. private readonly IContosoFlowersDialogFactory dialogFactory;
  20. private string selectedAddressToUpdate;
  21. public SettingsDialog(IContosoFlowersDialogFactory dialogFactory)
  22. {
  23. SetField.NotNull(out this.dialogFactory, nameof(dialogFactory), dialogFactory);
  24. }
  25. public async Task StartAsync(IDialogContext context)
  26. {
  27. this.selectedAddressToUpdate = null;
  28. var preferencesOptions = new[]
  29. {
  30. Resources.SettingsDialog_Edit_Email,
  31. Resources.SettingsDialog_Edit_PhoneNumber,
  32. Resources.SettingsDialog_Edit_BillingAddress
  33. };
  34. CancelablePromptChoice<string>.Choice(
  35. context,
  36. this.ResumeAfterOptionSelected,
  37. preferencesOptions,
  38. Resources.SettingsDialog_Prompt);
  39. }
  40. public async virtual Task MessageReceivedAsync(IDialogContext context, IAwaitable<IMessageActivity> result)
  41. {
  42. var message = await result;
  43. context.Wait(this.MessageReceivedAsync);
  44. }
  45. private static UserPreferences GetUserPreferences(IDialogContext context)
  46. {
  47. UserPreferences userPreferences = null;
  48. context.UserData.TryGetValue(StringConstants.UserPreferencesKey, out userPreferences);
  49. return userPreferences;
  50. }
  51. private async Task ResumeAfterOptionSelected(IDialogContext context, IAwaitable<string> result)
  52. {
  53. try
  54. {
  55. var option = await result;
  56. if (option == null)
  57. {
  58. context.Done<object>(null);
  59. return;
  60. }
  61. var userPreferences = GetUserPreferences(context);
  62. switch (option.Substring(0, 1))
  63. {
  64. case "1":
  65. this.PromptString(context, "email", userPreferences != null ? userPreferences.SenderEmail : null, RegexConstants.Email, this.ResumeAfterEmailEntered);
  66. break;
  67. case "2":
  68. this.PromptString(context, "phone number", userPreferences != null ? userPreferences.SenderPhoneNumber : null, RegexConstants.Phone, this.ResumeAfterPhoneNumberEntered);
  69. break;
  70. case "3":
  71. await this.PromptAddress(context, userPreferences);
  72. break;
  73. }
  74. }
  75. catch (TooManyAttemptsException)
  76. {
  77. await this.StartAsync(context);
  78. }
  79. }
  80. private async Task PromptAddress(IDialogContext context, UserPreferences userPreferences)
  81. {
  82. string homeAddress = null;
  83. string workAddress = null;
  84. userPreferences?.BillingAddresses?.TryGetValue(StringConstants.HomeBillingAddress.ToLower(), out homeAddress);
  85. userPreferences?.BillingAddresses?.TryGetValue(StringConstants.WorkBillingAddress.ToLower(), out workAddress);
  86. var message = context.MakeMessage();
  87. message.Text = Resources.SettingsDialog_PromptAddress_Ask;
  88. message.AttachmentLayout = AttachmentLayoutTypes.Carousel;
  89. message.AddHeroCard(StringConstants.HomeBillingAddress, homeAddress ?? StringConstants.NotSetBillingAddress, new[] { StringConstants.HomeBillingAddress });
  90. message.AddHeroCard(StringConstants.WorkBillingAddress, workAddress ?? StringConstants.NotSetBillingAddress, new[] { StringConstants.WorkBillingAddress });
  91. message.AddHeroCard("Back", "Go back", new[] { "Back" });
  92. await context.PostAsync(message);
  93. context.Wait(this.OnAddressSelectedReceived);
  94. }
  95. private async Task OnAddressSelectedReceived(IDialogContext context, IAwaitable<IMessageActivity> result)
  96. {
  97. var message = await result;
  98. if (message.Text.Equals(StringConstants.HomeBillingAddress, StringComparison.InvariantCultureIgnoreCase)
  99. || message.Text.Equals(StringConstants.WorkBillingAddress, StringComparison.InvariantCultureIgnoreCase))
  100. {
  101. this.selectedAddressToUpdate = message.Text;
  102. // BotBuilder's LocationDialog
  103. // Leverage DI to inject other parameters
  104. var locationDialog = this.dialogFactory.Create<LocationDialog>(
  105. new Dictionary<string, object>()
  106. {
  107. { "prompt", Resources.SettingsDialog_BillingAddress_Prompt },
  108. { "channelId", context.Activity.ChannelId }
  109. });
  110. context.Call(locationDialog, this.ResumeAfterAddressEntered);
  111. }
  112. else if (message.Text.Equals("B", StringComparison.InvariantCultureIgnoreCase) || message.Text.Equals("Back", StringComparison.InvariantCultureIgnoreCase))
  113. {
  114. await this.StartAsync(context);
  115. }
  116. else
  117. {
  118. await this.PromptAddress(context, GetUserPreferences(context));
  119. }
  120. }
  121. private async Task ResumeAfterAddressEntered(IDialogContext context, IAwaitable<Place> result)
  122. {
  123. string reply;
  124. var place = await result;
  125. if (place == null)
  126. {
  127. reply = "No address was selected, returning to settings menu.";
  128. }
  129. else
  130. {
  131. var formattedAddress = place.GetPostalAddress().FormattedAddress;
  132. context.UserData.UpdateValue<UserPreferences>(
  133. StringConstants.UserPreferencesKey,
  134. userPreferences =>
  135. {
  136. userPreferences.BillingAddresses = userPreferences.BillingAddresses ?? new Dictionary<string, string>();
  137. userPreferences.BillingAddresses[this.selectedAddressToUpdate.ToLower()] = formattedAddress;
  138. });
  139. reply = string.Format(CultureInfo.CurrentCulture, Resources.SettingsDialog_Address_Entered, this.selectedAddressToUpdate, formattedAddress);
  140. }
  141. await context.PostAsync(reply);
  142. await this.StartAsync(context);
  143. }
  144. private void PromptString(IDialogContext context, string ask, string currentValue, string regexPattern, ResumeAfter<string> resume)
  145. {
  146. string prompt = string.Format(CultureInfo.CurrentCulture, Resources.SettingsDialog_PrompString, ask);
  147. if (!string.IsNullOrEmpty(currentValue))
  148. {
  149. prompt = string.Format(CultureInfo.CurrentCulture, Resources.SettingsDialog_PrompString_CurrentValue, ask, currentValue);
  150. }
  151. string retry = string.Format(CultureInfo.CurrentCulture, Resources.SettingsDialog_PrompString_Retry, ask);
  152. var promptString = new PromptStringRegex(prompt, regexPattern, retry);
  153. context.Call(promptString, resume);
  154. }
  155. private async Task ResumeAfterEmailEntered(IDialogContext context, IAwaitable<string> result)
  156. {
  157. try
  158. {
  159. var email = await result;
  160. if (!string.IsNullOrEmpty(email))
  161. {
  162. context.UserData.UpdateValue<UserPreferences>(StringConstants.UserPreferencesKey, (u) => u.SenderEmail = email);
  163. await context.PostAsync(string.Format(CultureInfo.CurrentCulture, Resources.SettingsDialog_Email_Entered, email));
  164. }
  165. }
  166. finally
  167. {
  168. await this.StartAsync(context);
  169. }
  170. }
  171. private async Task ResumeAfterPhoneNumberEntered(IDialogContext context, IAwaitable<string> result)
  172. {
  173. try
  174. {
  175. var phone = await result;
  176. if (!string.IsNullOrEmpty(phone))
  177. {
  178. context.UserData.UpdateValue<UserPreferences>(StringConstants.UserPreferencesKey, (u) => u.SenderPhoneNumber = phone);
  179. await context.PostAsync(string.Format(CultureInfo.CurrentCulture, Resources.SettingsDialog_PhoneNumber_Entered, phone));
  180. }
  181. }
  182. finally
  183. {
  184. await this.StartAsync(context);
  185. }
  186. }
  187. }
  188. }
Tip!

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

Comments

Loading...