in Code/dotnetLexChatBot/Controllers/HelloChatBotController.cs [73:121]
public async Task<IActionResult> GetChatMessage(string userMessage)
{
//Get user session and chat info
userHttpSession = HttpContext.Session;
userSessionID = userHttpSession.Id;
botMessages = userHttpSession.Get<List<ChatBotMessage>>(botMsgKey) ?? new List<ChatBotMessage>();
lexSessionData = userHttpSession.Get<Dictionary<string, string>>(botAtrribsKey) ?? new Dictionary<string, string>();
//No message was provided, return to current view
if (String.IsNullOrEmpty(userMessage)) return View("TestChat", botMessages);
//A Valid Message exists, Add to page and allow Lex to process
botMessages.Add(new ChatBotMessage()
{ MsgType = MessageType.UserMessage, ChatMessage = userMessage });
await postUserData(botMessages);
//Call Amazon Lex with Text, capture response
var lexResponse = await awsLexSvc.SendTextMsgToLex(userMessage, lexSessionData, userSessionID);
lexSessionData = lexResponse.SessionAttributes;
if (
lexResponse.DialogState == DialogState.ElicitSlot ||
lexResponse.DialogState == DialogState.ConfirmIntent
) {
botMessages.Add(
new ChatBotMessage()
{
MsgType = MessageType.LexMessage,
ChatMessage = lexResponse.Message
});
} else if (
lexResponse.DialogState == DialogState.ReadyForFulfillment ||
lexResponse.DialogState == DialogState.Fulfilled
) {
botMessages.Add(
new ChatBotMessage()
{
MsgType = MessageType.LexMessage,
ChatMessage = lexResponse.Message ?? "Your order is being processed. Thank you for your business!"
});
}
//Add updated botMessages and lexSessionData object to Session
userHttpSession.Set<List<ChatBotMessage>>(botMsgKey, botMessages);
userHttpSession.Set<Dictionary<string, string>>(botAtrribsKey, lexSessionData);
return View("TestChat", botMessages);
}