Ejemplo n.º 1
0
        public async Task <JsonResult> ChatAsync([FromBody] ChatRequestBody body, [FromHeader] string sessionId)
        {
            IRestChatSession currentChatSession = null;

            Domain.ChatBot chatBot = await GetChatBotAsync();

            Domain.ChatSession chatSession = null;
            if (String.IsNullOrEmpty(sessionId))
            {
                chatSession = await _chatSessionService.Create(new Domain.ChatSession()
                {
                    ChatBotId = chatBot.ChatBotId
                });

                currentChatSession = new RestChatSession();
            }
            else
            {
                if (await _chatSessionService.Exists(session => session.ChatSessionId == Int32.Parse(sessionId)))
                {
                    chatSession = await _chatSessionService.FindBy(session => session.ChatSessionId == Int32.Parse(sessionId));
                }
                else
                {
                    chatSession = await _chatSessionService.Create(new Domain.ChatSession()
                    {
                        ChatBotId = chatBot.ChatBotId
                    });
                }

                Dictionary <string, string> sessionData = null;
                if (String.IsNullOrEmpty(chatSession.Data))
                {
                    sessionData = new Dictionary <string, string>();
                }
                else
                {
                    sessionData = JsonConvert.DeserializeObject <Dictionary <string, string> >(chatSession.Data);
                }

                currentChatSession = new RestChatSession(chatSession.ChatSessionId, sessionData);
            }
            Tuple <string, object> chatBotResponse = _chatBot.FindAnswer(currentChatSession, body.Message);

            string messageResponse = chatBotResponse.Item1;
            object responseObject  = chatBotResponse.Item2;

            await SaveSessionDataAsync(currentChatSession);

            sessionId = chatSession.ChatSessionId.ToString();

            if (responseObject != null && responseObject.GetType() == typeof(ExerciseResponse))
            {
                return(Json(new { sessionId = sessionId, chatbotResponse = messageResponse, exercise = responseObject }));
            }
            else
            {
                return(Json(new { sessionId = sessionId, chatbotResponse = messageResponse }));
            }
        }
        public async Task <IActionResult> GetChatSession([FromRoute] int id)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var chatSession = await _chatSessionService.FindBy(m => m.ChatSessionId == id);

            if (chatSession == null)
            {
                return(NotFound());
            }

            return(Ok(chatSession));
        }