Exemple #1
0
        //[ValidateAntiForgeryToken]
        public async Task <List <ChatBotMessage> > GetChatMessage(string userMessage)
        {
            logger.LogInformation("Get chat Message");
            //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(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;
            botMessages.Add(new ChatBotMessage()
            {
                MsgType = MessageType.LexMessage, ChatMessage = lexResponse.Message
            });

            //Add updated botMessages and lexSessionData object to Session
            userHttpSession.Set(botMsgKey, botMessages);
            userHttpSession.Set(botAtrribsKey, lexSessionData);

            return(botMessages);
        }
        public async Task <IActionResult> GetChatMessage(string userMessage)
        {
            botMessages.Add(new ChatBotMessage()
            {
                MsgType = MessageType.UserMessage, ChatMessage = userMessage
            });

            await postUserData();

            var lexResponse = await awsLexSvc.SendTextMsgToLex(userMessage, lexSessionData);

            lexSessionData = lexResponse.SessionAttributes;
            botMessages.Add(new ChatBotMessage()
            {
                MsgType = MessageType.LexMessage, ChatMessage = lexResponse.Message
            });


            // botMessages.Add(new ChatBotMessage()
            // { MsgType = MessageType.LexMessage, ChatMessage = "Testing Return" });

            return(View("TestChat", botMessages));
        }
Exemple #3
0
        public async Task <IActionResult> GetChatMessage(string userMessage)
        {
            _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>();
            _currentSlot     = _userHttpSession.GetString(_botCurrentSlotKey);
            if (!string.IsNullOrEmpty(_currentSlot))
            {
                _currentSlot = _currentSlot.Replace("\"", "");
            }

            if (string.IsNullOrEmpty(userMessage))
            {
                return(View(nameof(Index), _botMessages));
            }

            _botMessages.Add(new ChatBotMessage()
            {
                MsgType = MessageType.UserMessage, ChatMessage = userMessage
            });

            await PostUserData(_botMessages);

            if (_currentSlot == MAIL_LIST_SLOT)
            {
                if (userMessage.Contains("|"))
                {
                    userMessage = userMessage.Substring(0, userMessage.IndexOf("|"));
                }
                var list = await _context.MailLists.Select(m => m.Name).ToListAsync();

                var delimitedList = string.Join(";", list);
                userMessage = $"{userMessage}|{delimitedList}";
            }

            var lexResponse = await _awsLexService.SendTextMsgToLex(userMessage, _lexSessionData, _userSessionID);

            _lexSessionData = lexResponse.SessionAttributes;
            AssignmentDTO assignmentFromLex = new AssignmentDTO();
            var           currentObjectJson = _lexSessionData.GetValueOrDefault("CURRENT_OBJECT");

            if (!string.IsNullOrEmpty(currentObjectJson))
            {
                assignmentFromLex = DeserializeObject <AssignmentDTO>(currentObjectJson);
            }
            _botMessages.Add(new ChatBotMessage()
            {
                MsgType = MessageType.LexMessage, ChatMessage = lexResponse.Message
            });


            if (!string.IsNullOrEmpty(_lexSessionData.GetValueOrDefault("DONE")))
            {
                if (_lexSessionData.GetValueOrDefault("DONE") == "TRUE")
                {
                    await ScheduleAssignmentAsync(assignmentFromLex);
                }
            }

            _userHttpSession.Set(_botMsgKey, _botMessages);
            _userHttpSession.Set(_botAtrribsKey, _lexSessionData);
            _userHttpSession.Set(_botCurrentSlotKey, lexResponse.SlotToElicit);

            return(View(nameof(Index), _botMessages));
        }