forked from microsoft/botframework-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleIVRBot.cs
More file actions
310 lines (275 loc) · 12.1 KB
/
SimpleIVRBot.cs
File metadata and controls
310 lines (275 loc) · 12.1 KB
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
using Microsoft.Bot.Builder.Calling;
using Microsoft.Bot.Builder.Calling.Events;
using Microsoft.Bot.Builder.Calling.ObjectModel.Contracts;
using Microsoft.Bot.Builder.Calling.ObjectModel.Misc;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace Microsoft.Bot.Sample.SimpleIVRBot
{
public class SimpleIVRBot : IDisposable, ICallingBot
{
// below are the dtmf keys required for each of option, will be used for parsing results of recognize
private const string NewClient = "1";
private const string Support = "2";
private const string Payments = "3";
private const string MoreInfo = "4";
private const string NewClientOffer = "1";
private const string NewClientOrder = "2";
private const string SupportOutages = "1";
private const string SupportConsultant = "2";
private const string PaymentDetails = "1";
private const string PaymentNotVisible = "2";
private readonly Dictionary<string, CallState> _callStateMap = new Dictionary<string, CallState>();
public ICallingBotService CallingBotService { get; private set; }
public SimpleIVRBot(ICallingBotService callingBotService)
{
if (callingBotService == null)
throw new ArgumentNullException(nameof(callingBotService));
CallingBotService = callingBotService;
CallingBotService.OnIncomingCallReceived += OnIncomingCallReceived;
CallingBotService.OnPlayPromptCompleted += OnPlayPromptCompleted;
CallingBotService.OnRecordCompleted += OnRecordCompleted;
CallingBotService.OnRecognizeCompleted += OnRecognizeCompleted;
CallingBotService.OnHangupCompleted += OnHangupCompleted;
}
private Task OnIncomingCallReceived(IncomingCallEvent incomingCallEvent)
{
var id = Guid.NewGuid().ToString();
_callStateMap[incomingCallEvent.IncomingCall.Id] = new CallState();
incomingCallEvent.ResultingWorkflow.Actions = new List<ActionBase>
{
new Answer { OperationId = id },
GetPromptForText(IvrOptions.WelcomeMessage)
};
return Task.FromResult(true);
}
private Task OnHangupCompleted(HangupOutcomeEvent hangupOutcomeEvent)
{
hangupOutcomeEvent.ResultingWorkflow = null;
return Task.FromResult(true);
}
private Task OnPlayPromptCompleted(PlayPromptOutcomeEvent playPromptOutcomeEvent)
{
var callStateForClient = _callStateMap[playPromptOutcomeEvent.ConversationResult.Id];
callStateForClient.InitiallyChosenMenuOption = null;
SetupInitialMenu(playPromptOutcomeEvent.ResultingWorkflow);
return Task.FromResult(true);
}
private Task OnRecordCompleted(RecordOutcomeEvent recordOutcomeEvent)
{
var id = Guid.NewGuid().ToString();
recordOutcomeEvent.ResultingWorkflow.Actions = new List<ActionBase>
{
GetPromptForText(IvrOptions.Ending),
new Hangup { OperationId = id }
};
recordOutcomeEvent.ResultingWorkflow.Links = null;
_callStateMap.Remove(recordOutcomeEvent.ConversationResult.Id);
return Task.FromResult(true);
}
private Task OnRecognizeCompleted(RecognizeOutcomeEvent recognizeOutcomeEvent)
{
var callStateForClient = _callStateMap[recognizeOutcomeEvent.ConversationResult.Id];
switch (callStateForClient.InitiallyChosenMenuOption)
{
case null:
ProcessMainMenuSelection(recognizeOutcomeEvent, callStateForClient);
break;
case NewClient:
ProcessNewClientSelection(recognizeOutcomeEvent, callStateForClient);
break;
case Support:
ProcessSupportSelection(recognizeOutcomeEvent, callStateForClient);
break;
case Payments:
ProcessPaymentsSelection(recognizeOutcomeEvent, callStateForClient);
break;
default:
SetupInitialMenu(recognizeOutcomeEvent.ResultingWorkflow);
break;
}
return Task.FromResult(true);
}
private void SetupInitialMenu(Workflow workflow)
{
workflow.Actions = new List<ActionBase> { CreateIvrOptions(IvrOptions.MainMenuPrompt, 5, false) };
}
private Recognize CreateNewClientMenu()
{
return CreateIvrOptions(IvrOptions.NewClientPrompt, 2, true);
}
private Recognize CreateSupportMenu()
{
return CreateIvrOptions(IvrOptions.SupportPrompt, 2, true);
}
private Recognize CreatePaymentsMenu()
{
return CreateIvrOptions(IvrOptions.PaymentPrompt, 2, true);
}
private void ProcessMainMenuSelection(RecognizeOutcomeEvent outcome, CallState callStateForClient)
{
if (outcome.RecognizeOutcome.Outcome != Outcome.Success)
{
SetupInitialMenu(outcome.ResultingWorkflow);
return;
}
switch (outcome.RecognizeOutcome.ChoiceOutcome.ChoiceName)
{
case NewClient:
callStateForClient.InitiallyChosenMenuOption = NewClient;
outcome.ResultingWorkflow.Actions = new List<ActionBase> { CreateNewClientMenu() };
break;
case Support:
callStateForClient.InitiallyChosenMenuOption = Support;
outcome.ResultingWorkflow.Actions = new List<ActionBase> { CreateSupportMenu() };
break;
case Payments:
callStateForClient.InitiallyChosenMenuOption = Payments;
outcome.ResultingWorkflow.Actions = new List<ActionBase> { CreatePaymentsMenu() };
break;
case MoreInfo:
callStateForClient.InitiallyChosenMenuOption = MoreInfo;
outcome.ResultingWorkflow.Actions = new List<ActionBase> { GetPromptForText(IvrOptions.MoreInfoPrompt) };
break;
default:
SetupInitialMenu(outcome.ResultingWorkflow);
break;
}
}
private void ProcessNewClientSelection(RecognizeOutcomeEvent outcome, CallState callStateForClient)
{
if (outcome.RecognizeOutcome.Outcome != Outcome.Success)
{
outcome.ResultingWorkflow.Actions = new List<ActionBase> { CreateNewClientMenu() };
return;
}
switch (outcome.RecognizeOutcome.ChoiceOutcome.ChoiceName)
{
case NewClientOffer:
outcome.ResultingWorkflow.Actions = new List<ActionBase>
{
GetPromptForText(IvrOptions.Offer),
CreateNewClientMenu()
};
break;
case NewClientOrder:
SetupRecording(outcome.ResultingWorkflow);
break;
default:
callStateForClient.InitiallyChosenMenuOption = null;
SetupInitialMenu(outcome.ResultingWorkflow);
break;
}
}
private void ProcessSupportSelection(RecognizeOutcomeEvent outcome, CallState callStateForClient)
{
if (outcome.RecognizeOutcome.Outcome != Outcome.Success)
{
outcome.ResultingWorkflow.Actions = new List<ActionBase> { CreateSupportMenu() };
return;
}
switch (outcome.RecognizeOutcome.ChoiceOutcome.ChoiceName)
{
case SupportOutages:
outcome.ResultingWorkflow.Actions = new List<ActionBase>
{
GetPromptForText(IvrOptions.CurrentOutages),
CreateSupportMenu()
};
break;
case SupportConsultant:
SetupRecording(outcome.ResultingWorkflow);
break;
default:
callStateForClient.InitiallyChosenMenuOption = null;
SetupInitialMenu(outcome.ResultingWorkflow);
break;
}
}
private void ProcessPaymentsSelection(RecognizeOutcomeEvent outcome, CallState callStateForClient)
{
if (outcome.RecognizeOutcome.Outcome != Outcome.Success)
{
outcome.ResultingWorkflow.Actions = new List<ActionBase> { CreatePaymentsMenu() };
return;
}
switch (outcome.RecognizeOutcome.ChoiceOutcome.ChoiceName)
{
case PaymentDetails:
outcome.ResultingWorkflow.Actions = new List<ActionBase>
{
GetPromptForText(IvrOptions.PaymentDetailsMessage),
CreatePaymentsMenu()
};
break;
case PaymentNotVisible:
SetupRecording(outcome.ResultingWorkflow);
break;
default:
callStateForClient.InitiallyChosenMenuOption = null;
SetupInitialMenu(outcome.ResultingWorkflow);
break;
}
}
private static Recognize CreateIvrOptions(string textToBeRead, int numberOfOptions, bool includeBack)
{
if (numberOfOptions > 9)
throw new Exception("too many options specified");
var id = Guid.NewGuid().ToString();
var choices = new List<RecognitionOption>();
for (int i = 1; i <= numberOfOptions; i++)
{
choices.Add(new RecognitionOption { Name = Convert.ToString(i), DtmfVariation = (char)('0' + i) });
}
if (includeBack)
choices.Add(new RecognitionOption { Name = "#", DtmfVariation = '#' });
var recognize = new Recognize
{
OperationId = id,
PlayPrompt = GetPromptForText(textToBeRead),
BargeInAllowed = true,
Choices = choices
};
return recognize;
}
private static void SetupRecording(Workflow workflow)
{
var id = Guid.NewGuid().ToString();
var prompt = GetPromptForText(IvrOptions.NoConsultants);
var record = new Record
{
OperationId = id,
PlayPrompt = prompt,
MaxDurationInSeconds = 10,
InitialSilenceTimeoutInSeconds = 5,
MaxSilenceTimeoutInSeconds = 2,
PlayBeep = true,
StopTones = new List<char> { '#' }
};
workflow.Actions = new List<ActionBase> { record };
}
private static PlayPrompt GetPromptForText(string text)
{
var prompt = new Prompt { Value = text, Voice = VoiceGender.Male };
return new PlayPrompt { OperationId = Guid.NewGuid().ToString(), Prompts = new List<Prompt> { prompt } };
}
#region Implementation of IDisposable
public void Dispose()
{
if (CallingBotService != null)
{
CallingBotService.OnIncomingCallReceived -= OnIncomingCallReceived;
CallingBotService.OnPlayPromptCompleted -= OnPlayPromptCompleted;
CallingBotService.OnRecordCompleted -= OnRecordCompleted;
CallingBotService.OnRecognizeCompleted -= OnRecognizeCompleted;
CallingBotService.OnHangupCompleted -= OnHangupCompleted;
}
}
#endregion
private class CallState
{
public string InitiallyChosenMenuOption { get; set; }
}
}
}