示例#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 }));
            }
        }
示例#2
0
        public void BotTest_FindSolutionForError()
        {
            chatBot = new RestChatBot(_errorBotRules);
            string Message = "find the solution to this error";
            ChatSessionInterface session = new RestChatSession();
            var answer = chatBot.FindAnswer(session, Message);

            Assert.AreEqual(answer.Item1, "try this.  google.com");
        }
示例#3
0
        public void BotTest_HaveError()
        {
            chatBot = new RestChatBot(_errorBotRules);
            string Message = "I have exception";
            ChatSessionInterface session = new RestChatSession();
            var answer = chatBot.FindAnswer(session, Message);

            Assert.AreEqual(answer.Item1, "Whats the problem ?");
        }
示例#4
0
        public void BotTest_Goodbye()
        {
            chatBot = new RestChatBot(_goodByeBotRules);
            string Message = "ate";

            ChatSessionInterface session = new RestChatSession();
            var answer = chatBot.FindAnswer(session, Message);

            Assert.AreEqual(answer.Item1, "bye bye");
        }
示例#5
0
        public void JavaCourseAskTest()
        {
            CreateJavaBot();
            string Message = "What course name";

            ChatSessionInterface session = new RestChatSession();

            Assert.AreEqual(javaChatBot.FindAnswer(session, Message).Item1, "I do not know course name");
        }
示例#6
0
        public void BotTest_GetJoke()
        {
            chatBot = new RestChatBot(_jokeBotRules);
            string               Message = "tell me a joke";
            List <string>        jokes   = JokeRuleSet.jokeList;
            ChatSessionInterface session = new RestChatSession();
            var answer = chatBot.FindAnswer(session, Message);

            Console.WriteLine(answer.Item1);
            Assert.IsTrue(jokes.Contains(answer.Item1));
        }
示例#7
0
        public void BotTest_Greeting()
        {
            chatBot = new RestChatBot(_greetingBotRules);
            string Message = "hi";

            ChatSessionInterface session = new RestChatSession();
            var answer = chatBot.FindAnswer(session, Message);

            Assert.AreEqual(answer.Item1, "Hi!");

            //#2
            Message = "hello";

            session = new RestChatSession();
            answer  = chatBot.FindAnswer(session, Message);

            Assert.AreEqual(answer.Item1, "Hi!");
            //#3
            Message = "labas";

            session = new RestChatSession();
            answer  = chatBot.FindAnswer(session, Message);

            Assert.AreEqual(answer.Item1, "Hi!");
            //#4
            Message = "sveikas";

            session = new RestChatSession();
            answer  = chatBot.FindAnswer(session, Message);

            Assert.AreEqual(answer.Item1, "Hi!");
            //#5
            Message = "jfdbkfdkjldf";

            session = new RestChatSession();
            answer  = chatBot.FindAnswer(session, Message);

            Assert.AreEqual(answer.Item1, "I don't understand, could you try repeating that?");
        }
示例#8
0
        public void PhpCourseNameTest()
        {
            CreatePHPBot();
            string Message = "What course name";

            ChatSessionInterface session = new RestChatSession();

            Assert.AreEqual(PhpChatBot.FindAnswer(session, Message).Item1, "I do not know course name");
        }