/******************************************************************************* * Copyright 2009-2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. * * Licensed under the Apache License, Version 2.0 (the "License"). You may * not use this file except in compliance with the License. A copy of the * License is located at * * http://aws.amazon.com/apache2.0/ * * or in the "license" file accompanying this file. This file is * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the specific * language governing permissions and limitations under the License. *******************************************************************************/ using System; using System.Collections.Generic; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Http; using dotnetLexChatBot.Models; using dotnetLexChatBot.Data; using Amazon.Lex; // For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860 namespace dotnetLexChatBot.Controllers { public class HelloChatBotController : Controller { //Collection of ChatBot Messages //static Dictionary lexSessionData = new Dictionary(); private readonly IAWSLexService awsLexSvc; private ISession userHttpSession; private Dictionary lexSessionData; private List botMessages; private string botMsgKey = "ChatBotMessages", botAtrribsKey = "LexSessionData", userSessionID = String.Empty; public HelloChatBotController(IAWSLexService awsLexService) { awsLexSvc = awsLexService; } public IActionResult TestChat(List messages) { return View(messages); } public IActionResult ClearBot() { userHttpSession = HttpContext.Session; //Clear session keys and session information without removing Session ID userHttpSession.Clear(); //New botMessages and lexSessionData objects botMessages = new List(); lexSessionData = new Dictionary(); userHttpSession.Set>(botMsgKey, botMessages); userHttpSession.Set>(botAtrribsKey, lexSessionData); awsLexSvc.Dispose(); return View("TestChat", botMessages); } [HttpGet] [HttpPost] [ValidateAntiForgeryToken] public async Task GetChatMessage(string userMessage) { //Get user session and chat info userHttpSession = HttpContext.Session; userSessionID = userHttpSession.Id; botMessages = userHttpSession.Get>(botMsgKey) ?? new List(); lexSessionData = userHttpSession.Get>(botAtrribsKey) ?? new Dictionary(); //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>(botMsgKey, botMessages); userHttpSession.Set>(botAtrribsKey, lexSessionData); return View("TestChat", botMessages); } public async Task postUserData(List messages) { //testing return await Task.Run(() => TestChat(messages)); } } }