private async Task OnRecordCompleted(RecordOutcomeEvent recordOutcomeEvent) { List <ActionBase> actions = new List <ActionBase>(); // Convert the audio to text string spokenText = string.Empty; if (recordOutcomeEvent.RecordOutcome.Outcome == Outcome.Success) { var record = await recordOutcomeEvent.RecordedContent; spokenText = await this.GetTextFromAudioAsync(record); var intentRecieved = await MicrosoftCognitiveSpeechService.SendToLUISViaAPI(spokenText); Intent luisIntent = JsonConvert.DeserializeObject <Intent>(intentRecieved); var rootIntent = rootIntentActiveState.Values.All(a => a == false) ? RootIntentFinder(luisIntent) : rootIntentActiveState.Where(a => a.Value == true).FirstOrDefault().Key; var callState = this.callStateMap[recordOutcomeEvent.ConversationResult.Id]; if (!luisIntent.topScoringIntent.intent.Equals("Exit", StringComparison.InvariantCultureIgnoreCase) || rootIntentActiveState.Values.Contains(true)) { if (callerId == null) { int idTemp = ExtractNumbers(spokenText); var userRecognised = BotStubs.AuthenticateUser(idTemp); if (string.IsNullOrEmpty(userRecognised)) { SetupRecording(recordOutcomeEvent.ResultingWorkflow, "User id not recognised please retry."); } else { callerId = idTemp; //Check whether any registration is there against the user var userRegistrationDate = BotStubs.GetUserRegistrationDate(callerId); //if there is any registration available then give option to reschedule , cancel if (userRegistrationDate == null) { SetupRecording(recordOutcomeEvent.ResultingWorkflow, String.Format("Welcome {0}, You have no appointments booked as of now.", userRecognised)); } else { SetupRecording(recordOutcomeEvent.ResultingWorkflow, String.Format("Welcome {0}, You have appointments booked on {1} " + " Do you want to reschedule or cancel it.", userRecognised, userRegistrationDate.Value.ToString())); } } } else { if (rootIntentActiveState.Values.All(a => a == false) && !rootIntentActiveState[rootIntent]) { HandleRootIntent(rootIntent, recordOutcomeEvent.ResultingWorkflow); } else { switch (rootIntent) { case RootIntent.REGISTER: if (rootIntentActiveState[RootIntent.REGISTER]) { var dates = BotStubs.GetDates(); String DateAcceptance = String.Format("Following dates are available for registration" + "First. {0} Second. {0} Third. {0}, Please choose the number", dates[0].ToString(), dates[1].ToString() , dates[3].ToString()); SetupRecording(recordOutcomeEvent.ResultingWorkflow, DateAcceptance); int dateChoiceIndex = spokenText.ToLower().Contains("first") ? 0 : spokenText.ToLower().Contains("second") ? 1 : spokenText.ToLower().Contains("third") ? 2 : -1; if (dateChoiceIndex >= 0) { var selectedDate = dates[dateChoiceIndex]; var registrationId = BotStubs.Register(callerId.Value, selectedDate); await this.SendSTTResultToUser(String.Format("Registration Booked successfully on {0}," + " Registration Id is {1}", selectedDate, registrationId), callState.Participants); rootIntentActiveState[RootIntent.REGISTER] = false; recordOutcomeEvent.ResultingWorkflow.Actions = new List <ActionBase> { GetPromptForText(String.Format("Registration Booked successfully on {0}," + " Registration Id is {1}. Thank you for using the service.", selectedDate, registrationId)), new Hangup { OperationId = Guid.NewGuid().ToString() } }; callerId = null; recordOutcomeEvent.ResultingWorkflow.Links = null; this.callStateMap.Remove(recordOutcomeEvent.ConversationResult.Id); } } break; case RootIntent.RESCHEDULE: if (rootIntentActiveState[RootIntent.RESCHEDULE]) { var dates = BotStubs.GetDates(); String DateAcceptance = String.Format("Following dates are available for reschedule" + "First. {0} Second. {0} Third. {0}, Please choose the number", dates[0].ToString(), dates[1].ToString() , dates[3].ToString()); SetupRecording(recordOutcomeEvent.ResultingWorkflow, DateAcceptance); int dateChoiceIndex = spokenText.ToLower().Contains("first") ? 0 : spokenText.ToLower().Contains("second") ? 1 : spokenText.ToLower().Contains("third") ? 2 : -1; if (dateChoiceIndex >= 0) { var selectedDate = dates[dateChoiceIndex]; var registrationId = BotStubs.Register(callerId.Value, selectedDate); await this.SendSTTResultToUser(String.Format("Registration Rescheduled successfully on {0}," + " Registration Id is {1}", selectedDate, registrationId), callState.Participants); rootIntentActiveState[RootIntent.RESCHEDULE] = false; recordOutcomeEvent.ResultingWorkflow.Actions = new List <ActionBase> { GetPromptForText(String.Format("Registration rescheduled successfully on {0}," + " Registration Id is {1}. Thank you for using the service.", selectedDate, registrationId)), new Hangup { OperationId = Guid.NewGuid().ToString() } }; callerId = null; recordOutcomeEvent.ResultingWorkflow.Links = null; this.callStateMap.Remove(recordOutcomeEvent.ConversationResult.Id); } } break; case RootIntent.CANCEL: if (rootIntentActiveState[RootIntent.CANCEL]) { rootIntentActiveState[RootIntent.CANCEL] = false; if (BotStubs.CancelRegistration(callerId.Value)) { var cancelStatus = BotStubs.CancelRegistration(callerId.Value); if (cancelStatus) { recordOutcomeEvent.ResultingWorkflow.Actions = new List <ActionBase> { GetPromptForText("Registration cancelled successfully. Thank you for using the service."), new Hangup { OperationId = Guid.NewGuid().ToString() } }; callerId = null; recordOutcomeEvent.ResultingWorkflow.Links = null; this.callStateMap.Remove(recordOutcomeEvent.ConversationResult.Id); } } else { SetupRecording(recordOutcomeEvent.ResultingWorkflow, "Please retry, cancelation failed"); } } break; case RootIntent.LOOK_UP: return; } } } } else if (luisIntent.topScoringIntent.intent.Equals("Exit", StringComparison.InvariantCultureIgnoreCase) && luisIntent.topScoringIntent.score > 0.50) { //recordOutcomeEvent.ResultingWorkflow.Actions = new List<ActionBase> //{ // GetPromptForText("Thanks for using emergency bot"), // new Hangup { OperationId = Guid.NewGuid().ToString() } //}; //callerId = null; //recordOutcomeEvent.ResultingWorkflow.Links = null; //this.callStateMap.Remove(recordOutcomeEvent.ConversationResult.Id); HangUpCall(recordOutcomeEvent, "Thanks for using emergency bot"); } } else { recordOutcomeEvent.ResultingWorkflow.Actions = actions; SetupRecording(recordOutcomeEvent.ResultingWorkflow, "Recording failed please retry."); } }
/// <summary> /// POST: api/Messages /// Receive a message from a user and reply to it /// </summary> public async Task <HttpResponseMessage> Post([FromBody] Activity activity) { StringBuilder messageLog = new StringBuilder(); if (activity.Type == ActivityTypes.Message) { System.Diagnostics.Trace.TraceInformation(JsonConvert.SerializeObject(activity)); ConnectorClient connector = new ConnectorClient(new Uri(activity.ServiceUrl)); //await DisplayMessage(connector, activity, $"this is what i have got Content:{activity.Attachments[0].Content}\n ContentType:{activity.Attachments[0].ContentType}\n ContentUrl:{activity.Attachments[0].ContentUrl}\n Name:{activity.Attachments[0].Name}\n Properties:{activity.Attachments[0].Properties}" ); IncomingVoiceMessage voice = null; try { voice = new IncomingVoiceMessage(activity); } catch { //message = "Send me a voice message instead!"; // no voice file found await DisplayMessage(connector, activity, "No voice file found"); } try { if (voice.ContentType != null) { await DisplayMessage(connector, activity, "I have received a file \n\n Content Type: " + voice.ContentType + "\n\n Name: " + voice.FileName); //Download original voice message MicrosoftCognitiveSpeechService service = new MicrosoftCognitiveSpeechService(); var stream = await service.GetAudioStream(connector, activity, voice); var wavStream = await voice.SaveFile(stream, messageLog); if (wavStream.Length > 0) { await DisplayMessage(connector, activity, "reading contents of the wave file"); var text = await service.GetTextFromAudioAsync(wavStream, messageLog); await DisplayMessage(connector, activity, "Done length of text is: " + text.Length); if (text.Length > 0) { Activity reply = activity.CreateReply($"Did you say: {text}?"); await connector.Conversations.ReplyToActivityAsync(reply); } else { await DisplayMessage(connector, activity, "Failed if"); Activity reply = activity.CreateReply($"Sorry.. couldn't undertsand your intent! Please try again.."); await connector.Conversations.ReplyToActivityAsync(reply); } } else { await DisplayMessage(connector, activity, "Empty if"); Activity reply = activity.CreateReply($"wavStream is empty"); await connector.Conversations.ReplyToActivityAsync(reply); } } else { // calculate something for us to return int length = (activity.Text ?? string.Empty).Length; // return our reply to the user Activity reply = activity.CreateReply($"You sent {activity.Text} which was {length} characters. You can also send me a .wav file and I will tell you it says ;)"); await connector.Conversations.ReplyToActivityAsync(reply); } } catch (Exception ex) { //var innerMessage = (ex.InnerException != null) ? ex.InnerException.ToString() : string.Empty; //var stacktrace = (ex.StackTrace != null) ? ex.StackTrace.ToString() : string.Empty; //await DisplayMessage(connector, activity, "Caught Exception: " + ex.Message.Trim().Trim('.')+" \n\n MessageTrace: "+ messageLog.ToString()); await DisplayMessage(connector, activity, "Caught Exception: " + ex.Message); await DisplayMessage(connector, activity, "MessageLength: " + messageLog.Length); await DisplayMessage(connector, activity, "MessageTrace: " + messageLog.ToString()); } /* if (audioAttachment != null) * { * try * { * MicrosoftCognitiveSpeechService service = new MicrosoftCognitiveSpeechService(); * await DisplayMessage(connector, activity, "Analysing your voice request.."); * var stream = await service.GetAudioStream(connector,activity, audioAttachment); * await DisplayMessage(connector, activity, "I'm still working on it..."); * await DisplayMessage(connector, activity, "I'm still working on it..." +(stream == null)); * var text = await service.GetTextFromAudioAsync(stream); * if (text.Length > 0) * { * Activity reply = activity.CreateReply($"Did you say: {text}?"); * await connector.Conversations.ReplyToActivityAsync(reply); * } * else * { * Activity reply = activity.CreateReply($"Sorry.. couldn't undertsand your intent! Please try again.."); * await connector.Conversations.ReplyToActivityAsync(reply); * } * } * catch (Exception ex) * { * Activity reply = activity.CreateReply($"Exception found {ex.Message}"); * await connector.Conversations.ReplyToActivityAsync(reply); * } * }*/ } else { this.HandleSystemMessage(activity); } var response = Request.CreateResponse(HttpStatusCode.OK); return(response); }