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

StateDialog.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
  1. namespace StateBot
  2. {
  3. using System;
  4. using System.Threading.Tasks;
  5. using System.Web;
  6. using Microsoft.Bot.Builder.Dialogs;
  7. using Microsoft.Bot.Connector;
  8. [Serializable]
  9. public class StateDialog : IDialog<object>
  10. {
  11. private const string HelpMessage = "\n * If you want to know which city I'm using for my searches type 'current city'. \n * Want to change the current city? Type 'change city to cityName'. \n * Want to change it just for your searches? Type 'change my city to cityName'";
  12. private bool userWelcomed;
  13. public async Task StartAsync(IDialogContext context)
  14. {
  15. string defaultCity;
  16. if (!context.ConversationData.TryGetValue(ContextConstants.CityKey, out defaultCity))
  17. {
  18. defaultCity = "Seattle";
  19. context.ConversationData.SetValue(ContextConstants.CityKey, defaultCity);
  20. }
  21. await context.PostAsync($"Welcome to the Search City bot. I'm currently configured to search for things in {defaultCity}");
  22. context.Wait(this.MessageReceivedAsync);
  23. }
  24. public virtual async Task MessageReceivedAsync(IDialogContext context, IAwaitable<IMessageActivity> result)
  25. {
  26. var message = await result;
  27. string userName;
  28. if (!context.UserData.TryGetValue(ContextConstants.UserNameKey, out userName))
  29. {
  30. PromptDialog.Text(context, this.ResumeAfterPrompt, "Before get started, please tell me your name?");
  31. return;
  32. }
  33. if (!this.userWelcomed)
  34. {
  35. this.userWelcomed = true;
  36. await context.PostAsync($"Welcome back {userName}! Remember the rules: {HelpMessage}");
  37. context.Wait(this.MessageReceivedAsync);
  38. return;
  39. }
  40. if (message.Text.Equals("current city", StringComparison.InvariantCultureIgnoreCase))
  41. {
  42. string userCity;
  43. var city = context.ConversationData.GetValue<string>(ContextConstants.CityKey);
  44. if (context.PrivateConversationData.TryGetValue(ContextConstants.CityKey, out userCity))
  45. {
  46. await context.PostAsync($"{userName}, you have overridden the city. Your searches are for things in {userCity}. The default conversation city is {city}.");
  47. }
  48. else
  49. {
  50. await context.PostAsync($"Hey {userName}, I'm currently configured to search for things in {city}.");
  51. }
  52. }
  53. else if (message.Text.StartsWith("change city to", StringComparison.InvariantCultureIgnoreCase))
  54. {
  55. var newCity = message.Text.Substring("change city to".Length).Trim();
  56. context.ConversationData.SetValue(ContextConstants.CityKey, newCity);
  57. await context.PostAsync($"All set {userName}. From now on, all my searches will be for things in {newCity}.");
  58. }
  59. else if (message.Text.StartsWith("change my city to", StringComparison.InvariantCultureIgnoreCase))
  60. {
  61. var newCity = message.Text.Substring("change my city to".Length).Trim();
  62. context.PrivateConversationData.SetValue(ContextConstants.CityKey, newCity);
  63. await context.PostAsync($"All set {userName}. I have overridden the city to {newCity} just for you.");
  64. }
  65. else
  66. {
  67. string city;
  68. if (!context.PrivateConversationData.TryGetValue(ContextConstants.CityKey, out city))
  69. {
  70. city = context.ConversationData.GetValue<string>(ContextConstants.CityKey);
  71. }
  72. await context.PostAsync($"{userName}, wait a few seconds. Searching for '{message.Text}' in '{city}'...");
  73. await context.PostAsync($"https://www.bing.com/search?q={HttpUtility.UrlEncode(message.Text)}+in+{HttpUtility.UrlEncode(city)}");
  74. }
  75. context.Wait(this.MessageReceivedAsync);
  76. }
  77. private async Task ResumeAfterPrompt(IDialogContext context, IAwaitable<string> result)
  78. {
  79. try
  80. {
  81. var userName = await result;
  82. this.userWelcomed = true;
  83. await context.PostAsync($"Welcome {userName}! {HelpMessage}");
  84. context.UserData.SetValue(ContextConstants.UserNameKey, userName);
  85. }
  86. catch (TooManyAttemptsException)
  87. {
  88. }
  89. context.Wait(this.MessageReceivedAsync);
  90. }
  91. }
  92. }
Tip!

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

Comments

Loading...