private static async Task HandleBotResponse(IEnumerable <Activity> activities, string userQuery) { foreach (Activity activity in activities) { if (activity.Text != null) { string[] allAnswers = activity.Text.Split('|'); Random rand = new Random(); string randomAns = allAnswers[rand.Next(allAnswers.Length)]; AnswerTemplate answer = new AnswerTemplate(); answer.SplitMessage(randomAns); ConversationGlobalFlow.AskAgain = false; //answer.MessageToSpeak = "ask again" Debug.WriteLine("Reply: " + answer.MessageToSpeak); ExcelHelper.AddData(userQuery, answer.MessageToSpeak); if (GlobalFlowControl.moduleActivated == false) { return; } if (answer.MessageToSpeak.ToLower().Contains("ask again")) { if (GlobalFlowControl.moduleActivated == false || GlobalFlowControl.ChatbotInterrupted == true) { return; } /*ConversationGlobalFlow.AskAgain = true; * askAgainCount++; * if (askAgainCount == 2) * { * GlobalFlowControl.SendToBase("ChatBot", "StopTalking"); * askAgainCount = 0; * ConversationGlobalFlow.AskAgain = false; * return; * } */ //GlobalFlowControl.SendToBase("ChatBot", "IsTalking"); //SpeechGeneration.SpeakSync("Sorry. I don't understand!"); answer.MessageToSpeak = "Sorry. I don't understand"; //return; } else { askAgainCount = 0; } if (GlobalFlowControl.moduleActivated == false) { return; } if (answer.PicturePath != null) { if (answer.AutoNavLocation != null) { answer.PicturePath = @"locations\" + answer.PicturePath; } else { answer.PicturePath = @"qna\" + answer.PicturePath; } GlobalFlowControl.SendToBase("DisplayMedia", answer.PicturePath); if (answer.DisplayMediaDuration == 0) { answer.DisplayMediaDuration += 2; } } try { if (GlobalFlowControl.moduleActivated == false) { return; } GlobalFlowControl.SendToBase("ChatBot", "IsTalking"); SaySync(answer.MessageToSpeak); if (answer.VideoPath != null) { GlobalFlowControl.SendToBase("VideoPath", answer.VideoPath); } if (answer.DisplayMediaDuration > 0) { Thread.Sleep(1000 * answer.DisplayMediaDuration); } if (answer.AutoNavLocation != null) { SaySync("Do you want me to lead you there?"); SayAsync("Yes or No?"); GlobalFlowControl.SendToBase("DisplayMedia", GlobalData.ListeningModeImg); string reply = await SpeechRecognition.RecognizeQuery(3000).ConfigureAwait(false); if (reply != null) { reply = reply.ToLower(); if (reply.Contains("yes") || reply.Contains("yeah") || reply.Contains("yup") || reply.Contains("ok")) { SaySync("OK! Follow me"); GlobalFlowControl.IsNavigationInChatBot = true; GlobalFlowControl.IsReachedGoal = false; GlobalFlowControl.IsCancelledNavigation = false; var data = new SynchronisationData { Navigation = answer.AutoNavLocation, ChatBot = "NavigationOn" }; GlobalFlowControl.SendToBase(data); /*while (!GlobalFlowControl.IsReachedGoal * && !GlobalFlowControl.IsCancelledNavigation) ; * Thread.Sleep(500); * GlobalFlowControl.SendToBase("Navigation", "Stop"); * if (GlobalFlowControl.IsCancelledNavigation) return; */ return; } SaySync("Thank you, have a nice day"); } } } catch { if (GlobalFlowControl.ChatbotInterrupted) { continue; } } GlobalFlowControl.SendToBase("ChatBot", "StopTalking"); } } }
private static async Task Conversation() { while (GlobalFlowControl.moduleActivated) { GlobalFlowControl.ChatbotInterrupted = false; if (timer.Enabled == false && !GlobalFlowControl.IsRoving && !GlobalFlowControl.AskByPressingButton) { CheckIdle(); } string userQuery = null; userQuery = await SpeechRecognition.Recognize().ConfigureAwait(false); //System.Windows.Forms.MessageBox.Show(userQuery); if (GlobalFlowControl.ChatbotInterrupted) { continue; } if (GlobalFlowControl.moduleActivated == false) { break; } if (userQuery != null) { if (GlobalFlowControl.ChatbotInterrupted) { continue; } if (GlobalFlowControl.moduleActivated == false) { break; } await directLineClient.PostQuestionToBotAsync(userQuery).ConfigureAwait(false); IEnumerable <Activity> activities = await directLineClient.ReadBotMessagesAsyncDriver().ConfigureAwait(false); if (GlobalFlowControl.ChatbotInterrupted) { continue; } if (GlobalFlowControl.moduleActivated == false) { break; } await HandleBotResponse(activities, userQuery).ConfigureAwait(false); if (GlobalFlowControl.ChatbotInterrupted) { continue; } if (GlobalFlowControl.moduleActivated == false) { break; } } else { ConversationGlobalFlow.AskAgain = false; } } directLineClient.EndConversion(); GlobalFlowControl.moduleActivated = false; timer.Stop(); //System.Windows.Forms.MessageBox.Show("Chatbot end" + GlobalFlowControl.ChatBotDisabledByBody); if (GlobalFlowControl.TelepresenceMode == false && GlobalFlowControl.ChatBotDisabledByBody == false) { GlobalFlowControl.SendToBase("ChatBot", "quit"); } GlobalFlowControl.ChatBotDisabledByBody = false; //System.Windows.Forms.MessageBox.Show("Chatbot end 2" + GlobalFlowControl.ChatBotDisabledByBody); }