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 7.0 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
  1. namespace AppInsightsBot
  2. {
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Threading.Tasks;
  6. using System.Web;
  7. using Microsoft.Bot.Builder.Dialogs;
  8. using Microsoft.Bot.Connector;
  9. using Newtonsoft.Json;
  10. [Serializable]
  11. public class StateDialog : IDialog<object>
  12. {
  13. 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'";
  14. private bool userWelcomed;
  15. public async Task StartAsync(IDialogContext context)
  16. {
  17. var telemetry = context.CreateTraceTelemetry(nameof(StartAsync), new Dictionary<string, string> { { @"SetDefault", bool.FalseString } });
  18. string defaultCity;
  19. if (!context.ConversationData.TryGetValue(ContextConstants.CityKey, out defaultCity))
  20. {
  21. defaultCity = "Seattle";
  22. context.ConversationData.SetValue(ContextConstants.CityKey, defaultCity);
  23. telemetry.Properties[@"SetDefault"] = bool.TrueString;
  24. }
  25. await context.PostAsync($"Welcome to the Search City bot. I'm currently configured to search for things in {defaultCity}");
  26. WebApiApplication.Telemetry.TrackTrace(telemetry);
  27. context.Wait(this.MessageReceivedAsync);
  28. }
  29. public virtual async Task MessageReceivedAsync(IDialogContext context, IAwaitable<IMessageActivity> result)
  30. {
  31. var message = await result;
  32. // Here's how we can serialize an entire object to an App Insights event
  33. WebApiApplication.Telemetry.TrackTrace(context.CreateTraceTelemetry(
  34. nameof(MessageReceivedAsync),
  35. new Dictionary<string, string> { { "message", JsonConvert.SerializeObject(message) } }));
  36. string userName;
  37. if (!context.UserData.TryGetValue(ContextConstants.UserNameKey, out userName))
  38. {
  39. var t = context.CreateEventTelemetry(@"new user");
  40. t.Properties.Add("userName", userName); // You can add properties after-the-fact as well
  41. WebApiApplication.Telemetry.TrackEvent(t);
  42. PromptDialog.Text(context, this.ResumeAfterPrompt, "Before get started, please tell me your name?");
  43. return;
  44. }
  45. if (!this.userWelcomed)
  46. {
  47. this.userWelcomed = true;
  48. await context.PostAsync($"Welcome back {userName}! Remember the rules: {HelpMessage}");
  49. context.Wait(this.MessageReceivedAsync);
  50. return;
  51. }
  52. if (message.Text.Equals("current city", StringComparison.InvariantCultureIgnoreCase))
  53. {
  54. WebApiApplication.Telemetry.TrackEvent(context.CreateEventTelemetry(@"current city"));
  55. string userCity;
  56. var city = context.ConversationData.Get<string>(ContextConstants.CityKey);
  57. if (context.PrivateConversationData.TryGetValue(ContextConstants.CityKey, out userCity))
  58. {
  59. await context.PostAsync($"{userName}, you have overridden the city. Your searches are for things in {userCity}. The default conversation city is {city}.");
  60. }
  61. else
  62. {
  63. await context.PostAsync($"Hey {userName}, I'm currently configured to search for things in {city}.");
  64. }
  65. }
  66. else if (message.Text.StartsWith("change city to", StringComparison.InvariantCultureIgnoreCase))
  67. {
  68. WebApiApplication.Telemetry.TrackEvent(context.CreateEventTelemetry(@"change city to"));
  69. var newCity = message.Text.Substring("change city to".Length).Trim();
  70. context.ConversationData.SetValue(ContextConstants.CityKey, newCity);
  71. await context.PostAsync($"All set {userName}. From now on, all my searches will be for things in {newCity}.");
  72. }
  73. else if (message.Text.StartsWith("change my city to", StringComparison.InvariantCultureIgnoreCase))
  74. {
  75. WebApiApplication.Telemetry.TrackEvent(context.CreateEventTelemetry(@"change my city to"));
  76. var newCity = message.Text.Substring("change my city to".Length).Trim();
  77. context.PrivateConversationData.SetValue(ContextConstants.CityKey, newCity);
  78. await context.PostAsync($"All set {userName}. I have overridden the city to {newCity} just for you.");
  79. }
  80. else
  81. {
  82. var measuredEvent = context.CreateEventTelemetry(@"search");
  83. var timer = new System.Diagnostics.Stopwatch();
  84. timer.Start();
  85. try
  86. {
  87. string city;
  88. if (!context.PrivateConversationData.TryGetValue(ContextConstants.CityKey, out city))
  89. {
  90. city = context.ConversationData.Get<string>(ContextConstants.CityKey);
  91. }
  92. await context.PostAsync($"{userName}, wait a few seconds. Searching for '{message.Text}' in '{city}'...");
  93. await context.PostAsync($"https://www.bing.com/search?q={HttpUtility.UrlEncode(message.Text)}+in+{HttpUtility.UrlEncode(city)}");
  94. }
  95. catch (Exception ex)
  96. {
  97. measuredEvent.Properties.Add("exception", ex.ToString());
  98. WebApiApplication.Telemetry.TrackException(context.CreateExceptionTelemetry(ex));
  99. }
  100. finally
  101. {
  102. timer.Stop();
  103. measuredEvent.Metrics.Add(@"timeTakenMs", timer.ElapsedMilliseconds);
  104. WebApiApplication.Telemetry.TrackEvent(measuredEvent);
  105. }
  106. }
  107. context.Wait(this.MessageReceivedAsync);
  108. }
  109. private async Task ResumeAfterPrompt(IDialogContext context, IAwaitable<string> result)
  110. {
  111. try
  112. {
  113. var userName = await result;
  114. this.userWelcomed = true;
  115. await context.PostAsync($"Welcome {userName}! {HelpMessage}");
  116. context.UserData.SetValue(ContextConstants.UserNameKey, userName);
  117. }
  118. catch (TooManyAttemptsException ex)
  119. {
  120. WebApiApplication.Telemetry.TrackException(context.CreateExceptionTelemetry(ex));
  121. }
  122. finally
  123. {
  124. // It's a good idea to log telemetry in finally {} blocks so you don't end up with gaps of execution
  125. // as you follow a conversation
  126. WebApiApplication.Telemetry.TrackTrace(context.CreateTraceTelemetry(nameof(ResumeAfterPrompt)));
  127. }
  128. context.Wait(this.MessageReceivedAsync);
  129. }
  130. }
  131. }
Tip!

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

Comments

Loading...