-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBasicBot.cs
270 lines (234 loc) · 11.6 KB
/
BasicBot.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
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Bot.Builder;
using Microsoft.Bot.Builder.Dialogs;
using Microsoft.Bot.Schema;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
namespace Microsoft.BotBuilderSamples
{
/// <summary>
/// Main entry point and orchestration for bot.
/// </summary>
public class BasicBot : IBot
{
// Supported LUIS Intents
public const string GreetingIntent = "Greeting";
public const string CancelIntent = "Cancel";
public const string HelpIntent = "Help";
public const string NoneIntent = "None";
/// <summary>
/// Key in the bot config (.bot file) for the LUIS instance.
/// In the .bot file, multiple instances of LUIS can be configured.
/// </summary>
public static readonly string LuisConfiguration = "BasicBotLuisApplication";
private readonly IStatePropertyAccessor<GreetingState> _greetingStateAccessor;
private readonly IStatePropertyAccessor<DialogState> _dialogStateAccessor;
private readonly UserState _userState;
private readonly ConversationState _conversationState;
private readonly BotServices _services;
/// <summary>
/// Initializes a new instance of the <see cref="BasicBot"/> class.
/// </summary>
/// <param name="botServices">Bot services.</param>
/// <param name="accessors">Bot State Accessors.</param>
public BasicBot(BotServices services, UserState userState, ConversationState conversationState, ILoggerFactory loggerFactory)
{
_services = services ?? throw new ArgumentNullException(nameof(services));
_userState = userState ?? throw new ArgumentNullException(nameof(userState));
_conversationState = conversationState ?? throw new ArgumentNullException(nameof(conversationState));
_greetingStateAccessor = _userState.CreateProperty<GreetingState>(nameof(GreetingState));
_dialogStateAccessor = _conversationState.CreateProperty<DialogState>(nameof(DialogState));
// Verify LUIS configuration.
if (!_services.LuisServices.ContainsKey(LuisConfiguration))
{
throw new InvalidOperationException($"The bot configuration does not contain a service type of `luis` with the id `{LuisConfiguration}`.");
}
Dialogs = new DialogSet(_dialogStateAccessor);
Dialogs.Add(new GreetingDialog(_greetingStateAccessor, loggerFactory));
}
private DialogSet Dialogs { get; set; }
/// <summary>
/// Run every turn of the conversation. Handles orchestration of messages.
/// </summary>
/// <param name="turnContext">Bot Turn Context.</param>
/// <param name="cancellationToken">Task CancellationToken.</param>
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
public async Task OnTurnAsync(ITurnContext turnContext, CancellationToken cancellationToken)
{
var activity = turnContext.Activity;
// Create a dialog context
var dc = await Dialogs.CreateContextAsync(turnContext);
if (activity.Type == ActivityTypes.Message)
{
// Perform a call to LUIS to retrieve results for the current activity message.
var luisResults = await _services.LuisServices[LuisConfiguration].RecognizeAsync(dc.Context, cancellationToken);
// If any entities were updated, treat as interruption.
// For example, "no my name is tony" will manifest as an update of the name to be "tony".
var topScoringIntent = luisResults?.GetTopScoringIntent();
var topIntent = topScoringIntent.Value.intent;
// update greeting state with any entities captured
await UpdateGreetingState(luisResults, dc.Context);
// Handle conversation interrupts first.
var interrupted = await IsTurnInterruptedAsync(dc, topIntent);
if (interrupted)
{
// Bypass the dialog.
// Save state before the next turn.
await _conversationState.SaveChangesAsync(turnContext);
await _userState.SaveChangesAsync(turnContext);
return;
}
// Continue the current dialog
var dialogResult = await dc.ContinueDialogAsync();
// if no one has responded,
if (!dc.Context.Responded)
{
// examine results from active dialog
switch (dialogResult.Status)
{
case DialogTurnStatus.Empty:
switch (topIntent)
{
case GreetingIntent:
await dc.BeginDialogAsync(nameof(GreetingDialog));
break;
case NoneIntent:
default:
// Help or no intent identified, either way, let's provide some help.
// to the user
await dc.Context.SendActivityAsync("I didn't understand what you just said to me.");
break;
}
break;
case DialogTurnStatus.Waiting:
// The active dialog is waiting for a response from the user, so do nothing.
break;
case DialogTurnStatus.Complete:
await dc.EndDialogAsync();
break;
default:
await dc.CancelAllDialogsAsync();
break;
}
}
}
else if (activity.Type == ActivityTypes.ConversationUpdate)
{
if (activity.MembersAdded != null)
{
// Iterate over all new members added to the conversation.
foreach (var member in activity.MembersAdded)
{
// Greet anyone that was not the target (recipient) of this message.
// To learn more about Adaptive Cards, see https://aka.ms/msbot-adaptivecards for more details.
if (member.Id != activity.Recipient.Id)
{
var welcomeCard = CreateAdaptiveCardAttachment();
var response = CreateResponse(activity, welcomeCard);
await dc.Context.SendActivityAsync(response);
}
}
}
}
await _conversationState.SaveChangesAsync(turnContext);
await _userState.SaveChangesAsync(turnContext);
}
// Determine if an interruption has occurred before we dispatch to any active dialog.
private async Task<bool> IsTurnInterruptedAsync(DialogContext dc, string topIntent)
{
// See if there are any conversation interrupts we need to handle.
if (topIntent.Equals(CancelIntent))
{
if (dc.ActiveDialog != null)
{
await dc.CancelAllDialogsAsync();
await dc.Context.SendActivityAsync("Ok. I've canceled our last activity.");
}
else
{
await dc.Context.SendActivityAsync("I don't have anything to cancel.");
}
return true; // Handled the interrupt.
}
if (topIntent.Equals(HelpIntent))
{
await dc.Context.SendActivityAsync("Let me try to provide some help.");
await dc.Context.SendActivityAsync("I understand greetings, being asked for help, or being asked to cancel what I am doing.");
if (dc.ActiveDialog != null)
{
await dc.RepromptDialogAsync();
}
return true; // Handled the interrupt.
}
return false; // Did not handle the interrupt.
}
// Create an attachment message response.
private Activity CreateResponse(Activity activity, Attachment attachment)
{
var response = activity.CreateReply();
response.Attachments = new List<Attachment>() { attachment };
return response;
}
// Load attachment from file.
private Attachment CreateAdaptiveCardAttachment()
{
var adaptiveCard = File.ReadAllText(@".\Dialogs\Welcome\Resources\welcomeCard.json");
return new Attachment()
{
ContentType = "application/vnd.microsoft.card.adaptive",
Content = JsonConvert.DeserializeObject(adaptiveCard),
};
}
/// <summary>
/// Helper function to update greeting state with entities returned by LUIS.
/// </summary>
/// <param name="luisResult">LUIS recognizer <see cref="RecognizerResult"/>.</param>
/// <param name="turnContext">A <see cref="ITurnContext"/> containing all the data needed
/// for processing this conversation turn.</param>
/// <returns>A task that represents the work queued to execute.</returns>
private async Task UpdateGreetingState(RecognizerResult luisResult, ITurnContext turnContext)
{
if (luisResult.Entities != null && luisResult.Entities.HasValues)
{
// Get latest GreetingState
var greetingState = await _greetingStateAccessor.GetAsync(turnContext, () => new GreetingState());
var entities = luisResult.Entities;
// Supported LUIS Entities
string[] userNameEntities = { "userName", "userName_patternAny" };
string[] userLocationEntities = { "userLocation", "userLocation_patternAny" };
// Update any entities
// Note: Consider a confirm dialog, instead of just updating.
foreach (var name in userNameEntities)
{
// Check if we found valid slot values in entities returned from LUIS.
if (entities[name] != null)
{
// Capitalize and set new user name.
var newName = (string)entities[name][0];
greetingState.Name = char.ToUpper(newName[0]) + newName.Substring(1);
break;
}
}
foreach (var city in userLocationEntities)
{
if (entities[city] != null)
{
// Capitalize and set new city.
var newCity = (string)entities[city][0];
greetingState.City = char.ToUpper(newCity[0]) + newCity.Substring(1);
break;
}
}
// Set the new values into state.
await _greetingStateAccessor.SetAsync(turnContext, greetingState);
}
}
}
}