forked from microsoft/BotBuilder-Samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
IVRBot.cs
253 lines (209 loc) · 9.76 KB
/
IVRBot.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
namespace EmergencyServicesBot
{
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
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;
public class IVRBot : IDisposable, ICallingBot
{
// DTMF keys required for each of option, will be used for parsing results of recognize
private const string Support = "1";
// Response messages depending on user selection
private const string WelcomeMessage = "Hello, you have successfully contacted the Emergency Services Bot.";
private const string MainMenuPromptMessage = "If you have a life threatening medical emergency please contact the emergency services or go to your nearest hospital. For non-life threatening situations please press 1.";
private const string NoConsultantsMessage = "Whilst we wait to connect you, please leave your name and a description of your problem. You can press the hash key when finished. We will call you as soon as possible.";
private const string EndingMessage = "Thank you for leaving the message, goodbye";
private const string OptionMenuNotSupportedMessage = "The option you entered is not supported. Please try again.";
private readonly Dictionary<string, CallState> callStateMap = new Dictionary<string, CallState>();
private readonly MicrosoftCognitiveSpeechService speechService = new MicrosoftCognitiveSpeechService();
public IVRBot(ICallingBotService callingBotService)
{
if (callingBotService == null)
{
throw new ArgumentNullException(nameof(callingBotService));
}
this.CallingBotService = callingBotService;
this.CallingBotService.OnIncomingCallReceived += this.OnIncomingCallReceived;
this.CallingBotService.OnPlayPromptCompleted += this.OnPlayPromptCompleted;
this.CallingBotService.OnRecordCompleted += this.OnRecordCompleted;
this.CallingBotService.OnRecognizeCompleted += this.OnRecognizeCompleted;
this.CallingBotService.OnHangupCompleted += OnHangupCompleted;
}
public ICallingBotService CallingBotService { get; }
public void Dispose()
{
if (this.CallingBotService != null)
{
this.CallingBotService.OnIncomingCallReceived -= this.OnIncomingCallReceived;
this.CallingBotService.OnPlayPromptCompleted -= this.OnPlayPromptCompleted;
this.CallingBotService.OnRecordCompleted -= this.OnRecordCompleted;
this.CallingBotService.OnRecognizeCompleted -= this.OnRecognizeCompleted;
this.CallingBotService.OnHangupCompleted -= OnHangupCompleted;
}
}
private static Task OnHangupCompleted(HangupOutcomeEvent hangupOutcomeEvent)
{
hangupOutcomeEvent.ResultingWorkflow = null;
return Task.FromResult(true);
}
private static void SetupInitialMenu(Workflow workflow)
{
workflow.Actions = new List<ActionBase> { GetInitialMenu() };
}
private static void SetupInitialMenuWithErrorMessage(Workflow workflow)
{
workflow.Actions = new List<ActionBase>
{
GetPromptForText(OptionMenuNotSupportedMessage),
GetInitialMenu()
};
}
private static ActionBase GetInitialMenu()
{
return CreateIvrOptions(MainMenuPromptMessage, 1, false);
}
private static void ProcessMainMenuSelection(RecognizeOutcomeEvent outcome, CallState callStateForClient)
{
if (outcome.RecognizeOutcome.Outcome != Outcome.Success)
{
if (outcome.RecognizeOutcome.FailureReason == RecognitionCompletionReason.IncorrectDtmf.ToString())
{
SetupInitialMenuWithErrorMessage(outcome.ResultingWorkflow);
}
else
{
SetupInitialMenu(outcome.ResultingWorkflow);
}
return;
}
switch (outcome.RecognizeOutcome.ChoiceOutcome.ChoiceName)
{
case Support:
callStateForClient.ChosenMenuOption = Support;
SetupRecording(outcome.ResultingWorkflow);
break;
default:
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 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 = Guid.NewGuid().ToString(),
PlayPrompt = GetPromptForText(textToBeRead),
BargeInAllowed = true,
Choices = choices
};
return recognize;
}
private static void SetupRecording(Workflow workflow)
{
var id = Guid.NewGuid().ToString();
var prompt = GetPromptForText(NoConsultantsMessage);
var record = new Record
{
OperationId = id,
PlayPrompt = prompt,
MaxDurationInSeconds = 60,
InitialSilenceTimeoutInSeconds = 5,
MaxSilenceTimeoutInSeconds = 4,
PlayBeep = true,
RecordingFormat = RecordingFormat.Wav,
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 } };
}
private Task OnIncomingCallReceived(IncomingCallEvent incomingCallEvent)
{
this.callStateMap[incomingCallEvent.IncomingCall.Id] = new CallState(incomingCallEvent.IncomingCall.Participants);
incomingCallEvent.ResultingWorkflow.Actions = new List<ActionBase>
{
new Answer { OperationId = Guid.NewGuid().ToString() },
GetPromptForText(WelcomeMessage)
};
return Task.FromResult(true);
}
private Task OnPlayPromptCompleted(PlayPromptOutcomeEvent playPromptOutcomeEvent)
{
var callState = this.callStateMap[playPromptOutcomeEvent.ConversationResult.Id];
SetupInitialMenu(playPromptOutcomeEvent.ResultingWorkflow);
return Task.FromResult(true);
}
private async Task OnRecordCompleted(RecordOutcomeEvent recordOutcomeEvent)
{
recordOutcomeEvent.ResultingWorkflow.Actions = new List<ActionBase>
{
GetPromptForText(EndingMessage),
new Hangup { OperationId = Guid.NewGuid().ToString() }
};
// Convert the audio to text
if (recordOutcomeEvent.RecordOutcome.Outcome == Outcome.Success)
{
var record = await recordOutcomeEvent.RecordedContent;
string text = await this.GetTextFromAudioAsync(record);
var callState = this.callStateMap[recordOutcomeEvent.ConversationResult.Id];
await this.SendSTTResultToUser("We detected the following audio: " + text, callState.Participants);
}
recordOutcomeEvent.ResultingWorkflow.Links = null;
this.callStateMap.Remove(recordOutcomeEvent.ConversationResult.Id);
}
private async Task SendSTTResultToUser(string text, IEnumerable<Participant> participants)
{
var to = participants.Single(x => x.Originator);
var from = participants.First(x => !x.Originator);
await AgentListener.Resume(to.Identity, to.DisplayName, from.Identity, from.DisplayName, to.Identity, text);
}
/// <summary>
/// Gets text from an audio stream.
/// </summary>
/// <param name="audiostream"></param>
/// <returns>Transcribed text. </returns>
private async Task<string> GetTextFromAudioAsync(Stream audiostream)
{
var text = await this.speechService.GetTextFromAudioAsync(audiostream);
Debug.WriteLine(text);
return text;
}
private Task OnRecognizeCompleted(RecognizeOutcomeEvent recognizeOutcomeEvent)
{
var callState = this.callStateMap[recognizeOutcomeEvent.ConversationResult.Id];
ProcessMainMenuSelection(recognizeOutcomeEvent, callState);
return Task.FromResult(true);
}
private class CallState
{
public CallState(IEnumerable<Participant> participants)
{
this.Participants = participants;
}
public string ChosenMenuOption { get; set; }
public IEnumerable<Participant> Participants { get; }
}
}
}