public AiResponseExtract ProcessUserRequest(Conversation conversation, UserRequest userRequest)
        {
            AiResponseExtract response = new AiResponseExtract();

            AIRequest aiRequest = new AIRequest(userRequest.Body);
            AIContext aiContext = new AIContext()
            {
                Name = conversation.CurrentMessage.Name
            };

            aiRequest.Contexts = new List <AIContext>()
            {
                aiContext
            };

            AIResponse aiResponse = apiAi.TextRequest(aiRequest);

            response.Intent = aiResponse.Result.Action;

            if (aiResponse.Result.Action.Contains("smalltalk"))
            {
                response.SmallTalk = true;
            }

            response.Speech = aiResponse.Result.Fulfillment.Speech;

            return(response);
        }
示例#2
0
        public string MaintainConversation(UserRequest userRequest)
        {
            Person       person       = PersonService.GetPersonByUserRequest(userRequest);
            Conversation conversation = ConversationService.GetActiveConversationForPerson(person);

            if (conversation == null)
            { // TODO clean up / look for reactive chatbot
                return("You do not have an active conversation");
            }

            AiResponseExtract response = AiService.ProcessUserRequest(conversation, userRequest);

            // Handle incoming Text
            Communication comm = CommunicationService.CreateIncomingCommunication(conversation, userRequest, response);

            conversation.Communications.Add(comm);

            // Change State (Message)
            DataClasses.Message nextMessage = MessageService.GetNextMessage(conversation, response);
            ConversationService.UpdateCurrentMessage(conversation, nextMessage, response);

            // Handle outbound Text
            Communication newComm = CommunicationService.CreateOutgoingCommunication(conversation, nextMessage);

            conversation.Communications.Add(newComm);

            Context.SaveChanges();
            return(nextMessage.Body);
        }
示例#3
0
        public DataClasses.Message GetNextMessage(Conversation conversation, AiResponseExtract response)
        {
            DataClasses.Message nextMessage = null;

            var query = from MessageTransitioner in Context.Transitions
                        where MessageTransitioner.CurrentMessage.Name == conversation.CurrentMessage.Name &&
                        MessageTransitioner.Intent.Name == response.Intent
                        select MessageTransitioner.NextMessage;

            nextMessage = query.SingleOrDefault();

            if (nextMessage == null)
            {
                nextMessage = CreateSmallTalkMessage(conversation, response);
            }

            return(nextMessage);
        }
示例#4
0
        private DataClasses.Message CreateSmallTalkMessage(Conversation conversation, AiResponseExtract response)
        {
            DataClasses.Message smallTalkMessage = new DataClasses.Message()
            {
                Name    = conversation.CurrentMessage.Name + "-smalltalk",
                Body    = response.Speech,
                IsStart = false,
                IsEnd   = false
            };

            return(smallTalkMessage);
        }
示例#5
0
        public void UpdateCurrentMessage(Conversation conversation, DataClasses.Message nextMessage, AiResponseExtract response)
        {
            if (response.Intent.Equals(IntentTypeService.GetMisunderstoodIntent().Name) ||
                response.SmallTalk)
            {
                // TODO increment times user got stuck.  If at a certain amount, Trigger move on.
                // Move on will send an extra message.  Need to determine how to store the extra message in the DB.
                return;
            }

            conversation.CurrentMessage = nextMessage;

            if (nextMessage.IsEnd)
            {
                conversation.Active = false;
            }
        }
示例#6
0
        public Communication CreateIncomingCommunication(Conversation conversation, UserRequest userRequest, AiResponseExtract aiResponse)
        {
            Communication comm = new Communication()
            {
                Conversation = conversation,
                Outbound     = false,
                Body         = userRequest.Body,
                Context      = aiResponse.Intent
            };

            return(comm);
        }