forked from microsoft/BotBuilder-Samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
StateDialog.cs
164 lines (131 loc) · 6.95 KB
/
StateDialog.cs
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
namespace AppInsightsBot
{
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Web;
using Microsoft.Bot.Builder.Dialogs;
using Microsoft.Bot.Connector;
using Newtonsoft.Json;
[Serializable]
public class StateDialog : IDialog<object>
{
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'";
private bool userWelcomed;
public async Task StartAsync(IDialogContext context)
{
var telemetry = context.CreateTraceTelemetry(nameof(StartAsync), new Dictionary<string, string> { { @"SetDefault", bool.FalseString } });
string defaultCity;
if (!context.ConversationData.TryGetValue(ContextConstants.CityKey, out defaultCity))
{
defaultCity = "Seattle";
context.ConversationData.SetValue(ContextConstants.CityKey, defaultCity);
telemetry.Properties[@"SetDefault"] = bool.TrueString;
}
await context.PostAsync($"Welcome to the Search City bot. I'm currently configured to search for things in {defaultCity}");
WebApiApplication.Telemetry.TrackTrace(telemetry);
context.Wait(this.MessageReceivedAsync);
}
public virtual async Task MessageReceivedAsync(IDialogContext context, IAwaitable<IMessageActivity> result)
{
var message = await result;
// Here's how we can serialize an entire object to an App Insights event
WebApiApplication.Telemetry.TrackTrace(context.CreateTraceTelemetry(
nameof(MessageReceivedAsync),
new Dictionary<string, string> { { "message", JsonConvert.SerializeObject(message) } }));
string userName;
if (!context.UserData.TryGetValue(ContextConstants.UserNameKey, out userName))
{
var t = context.CreateEventTelemetry(@"new user");
t.Properties.Add("userName", userName); // You can add properties after-the-fact as well
WebApiApplication.Telemetry.TrackEvent(t);
PromptDialog.Text(context, this.ResumeAfterPrompt, "Before get started, please tell me your name?");
return;
}
if (!this.userWelcomed)
{
this.userWelcomed = true;
await context.PostAsync($"Welcome back {userName}! Remember the rules: {HelpMessage}");
context.Wait(this.MessageReceivedAsync);
return;
}
if (message.Text.Equals("current city", StringComparison.InvariantCultureIgnoreCase))
{
WebApiApplication.Telemetry.TrackEvent(context.CreateEventTelemetry(@"current city"));
string userCity;
var city = context.ConversationData.Get<string>(ContextConstants.CityKey);
if (context.PrivateConversationData.TryGetValue(ContextConstants.CityKey, out userCity))
{
await context.PostAsync($"{userName}, you have overridden the city. Your searches are for things in {userCity}. The default conversation city is {city}.");
}
else
{
await context.PostAsync($"Hey {userName}, I'm currently configured to search for things in {city}.");
}
}
else if (message.Text.StartsWith("change city to", StringComparison.InvariantCultureIgnoreCase))
{
WebApiApplication.Telemetry.TrackEvent(context.CreateEventTelemetry(@"change city to"));
var newCity = message.Text.Substring("change city to".Length).Trim();
context.ConversationData.SetValue(ContextConstants.CityKey, newCity);
await context.PostAsync($"All set {userName}. From now on, all my searches will be for things in {newCity}.");
}
else if (message.Text.StartsWith("change my city to", StringComparison.InvariantCultureIgnoreCase))
{
WebApiApplication.Telemetry.TrackEvent(context.CreateEventTelemetry(@"change my city to"));
var newCity = message.Text.Substring("change my city to".Length).Trim();
context.PrivateConversationData.SetValue(ContextConstants.CityKey, newCity);
await context.PostAsync($"All set {userName}. I have overridden the city to {newCity} just for you.");
}
else
{
var measuredEvent = context.CreateEventTelemetry(@"search");
var timer = new System.Diagnostics.Stopwatch();
timer.Start();
try
{
string city;
if (!context.PrivateConversationData.TryGetValue(ContextConstants.CityKey, out city))
{
city = context.ConversationData.Get<string>(ContextConstants.CityKey);
}
await context.PostAsync($"{userName}, wait a few seconds. Searching for '{message.Text}' in '{city}'...");
await context.PostAsync($"https://www.bing.com/search?q={HttpUtility.UrlEncode(message.Text)}+in+{HttpUtility.UrlEncode(city)}");
}
catch (Exception ex)
{
measuredEvent.Properties.Add("exception", ex.ToString());
WebApiApplication.Telemetry.TrackException(context.CreateExceptionTelemetry(ex));
}
finally
{
timer.Stop();
measuredEvent.Metrics.Add(@"timeTakenMs", timer.ElapsedMilliseconds);
WebApiApplication.Telemetry.TrackEvent(measuredEvent);
}
}
context.Wait(this.MessageReceivedAsync);
}
private async Task ResumeAfterPrompt(IDialogContext context, IAwaitable<string> result)
{
try
{
var userName = await result;
this.userWelcomed = true;
await context.PostAsync($"Welcome {userName}! {HelpMessage}");
context.UserData.SetValue(ContextConstants.UserNameKey, userName);
}
catch (TooManyAttemptsException ex)
{
WebApiApplication.Telemetry.TrackException(context.CreateExceptionTelemetry(ex));
}
finally
{
// It's a good idea to log telemetry in finally {} blocks so you don't end up with gaps of execution
// as you follow a conversation
WebApiApplication.Telemetry.TrackTrace(context.CreateTraceTelemetry(nameof(ResumeAfterPrompt)));
}
context.Wait(this.MessageReceivedAsync);
}
}
}