示例#1
0
        public WitReply(string json_raw)
            : base(json_raw)
        {
            var data = CalOneBotData.FromJson(json_raw);

            m_chatReply = IntentManager.ProcessChatBotMessage(data);
        }
        private static void AssignParameterData(IntentDefinition.IntentParam param, CalOneBotData chatdata)
        {
            if (param.ParamName == WitContants.entity_location && chatdata.Entities.Location != null)
            {
                param.Value = FindBestEntity(chatdata.Entities.Location.ToList()).Value;
            }

            if (param.ParamName == WitContants.entity_subjects && chatdata.Entities.EntitySubjects != null)
            {
                param.Value = FindBestEntity(chatdata.Entities.EntitySubjects.ToList()).Value;
            }

            if (param.ParamName == WitContants.entity_grade && chatdata.Entities.EntityGrades != null)
            {
                param.Value = FindBestEntity(chatdata.Entities.EntityGrades.ToList()).Value;
            }
        }
示例#3
0
 public static string ToJson(this CalOneBotData self)
 {
     return(JsonConvert.SerializeObject(self, Converter.Settings));
 }
        public static string ProcessChatBotMessage(CalOneBotData chatdata)
        {
            if (chatdata == null)
            {
                return(null);
            }

            var    reply_builder    = new StringBuilder();
            string intent_subject   = chatdata.Entities.Intent_Subject != null ? chatdata.Entities.Intent_Subject[0].Value : "";
            string intent_direction = chatdata.Entities.Intent_Direction != null ? chatdata.Entities.Intent_Direction[0].Value : "";
            string default_answer   = chatdata.Entities.DefaultAnswers != null ? chatdata.Entities.DefaultAnswers[0].Value : "";

            // IF SUBJECT AND NOT SURE INTENT!
            if (!string.IsNullOrEmpty(intent_subject) && string.IsNullOrEmpty(intent_direction))
            {
                // SUBJECT IS KNOW BUT DONT KNOW INTENT, ASSUME ASKING
                intent_direction = WitContantAction.intent_action_querytask;
            }

            if (!string.IsNullOrEmpty(intent_subject) && !string.IsNullOrEmpty(intent_direction))
            {
                string targetIntent = intent_subject + intent_direction;

                m_currentIntent         = null;
                m_currentIntentQuestion = null;

                var ff = m_DefinitionList.Where(
                    m => m.Intent == targetIntent
                    ).Select(m => m).ToList();

                m_currentIntentQuestion = null;

                if (ff.Count > 0)
                {
                    m_currentIntent = ff[0];
                }

                if (m_currentIntent != null)
                {
                    //INITIALIZE INTENT PARAMS
                    var paramList = m_currentIntent.IntentParameters.Where(m => m.IsRequired).ToList();
                    paramList.ForEach(m => {
                        m.Value = "";
                        AssignParameterData(m, chatdata);
                    });

                    reply_builder.Append(ReplyManager.Reply(m_currentIntent.Intent_Salutation));

                    var findRequiredField = m_currentIntent.IntentParameters.Where(m => m.IsRequired && string.IsNullOrEmpty(m.Value)).ToList();
                    if (findRequiredField.Count >= 2)
                    {
                        reply_builder.Append(ReplyManager.Reply(WitContants.salutation_askfewquestions));
                    }
                }
            }

            if (m_segway != null)
            {
                if (default_answer == "default_yes")
                {
                    m_currentIntent = m_segway;
                }

                if (m_currentIntent == null)
                {
                    reply_builder.Append("No problem.  Can I assist you in any other way?");
                }

                m_segway = null;
            }

            if (m_currentIntentQuestion != null)
            {
                m_currentIntentQuestion.Value = chatdata.Text; // IF NO ENTITY DETECTED AND HAS A DEFAULT QUESTION, ASSIGN IT TO THE QUESTION

                //Append any remarks after an answer
                reply_builder.Append(ReplyManager.Reply(m_currentIntentQuestion.AfterAnswer));

                m_currentIntentQuestion = null;
            }

            if (chatdata.Entities.Farewell != null)
            {
                reply_builder.Append(ReplyManager.Reply(WitContants.entity_bye));
            }

            if (m_currentIntent != null)
            {
                var  findRequiredField = m_currentIntent.IntentParameters.Where(m => m.IsRequired && string.IsNullOrEmpty(m.Value)).ToList();
                bool IsIntentSatisfied = findRequiredField.Count == 0;

                if (IsIntentSatisfied)
                {
                    string intent_value = "";
                    if (m_currentIntent.Intent == WitContantsIntent.intent_subject_payment + WitContantAction.intent_action_querytask)
                    {
                        intent_value = CalOneBotAnswer.GetTutionPayment(m_currentIntent.IntentParameters);
                    }

                    if (m_currentIntent.Intent == WitContantsIntent.intent_subject_complaint + WitContantAction.intent_action_dotask)
                    {
                        intent_value = "I have heard you and we will get back at you as soon as possible.";
                    }

                    if (m_currentIntent.Intent == WitContantsIntent.intent_subject_registration + WitContantAction.intent_action_dotask)
                    {
                        intent_value = "Thank you.  Your registration has been filed.";
                    }

                    if (m_currentIntent.Intent == WitContantsIntent.intent_subject_courses + WitContantAction.intent_action_querytask)
                    {
                        intent_value = "Math, Mandarin, Science.";

                        var seg1 = m_DefinitionList.Find(m => m.Intent == WitContantsIntent.intent_subject_registration + WitContantAction.intent_action_dotask);
                        m_SegwayList.Enqueue(seg1);
                    }

                    if (string.IsNullOrEmpty(intent_value))
                    {
                        intent_value = WitContants.no_answer_response;
                    }

                    reply_builder.Append(intent_value);

                    if (m_SegwayList.Count > 0)
                    {
                        m_segway = m_SegwayList.Dequeue();
                        reply_builder.Append("\n" + m_segway.Intent_AskToDo);
                    }

                    m_currentIntent = null;
                    return(reply_builder.ToString());
                }

                if (findRequiredField.Count > 0)
                {
                    m_currentIntentQuestion       = findRequiredField[0];
                    m_currentIntentQuestion.Value = "";

                    reply_builder.Append(m_currentIntentQuestion.QuestionIfMissing);
                }
                else
                {
                    reply_builder.Append("Processing.");
                }
            }
            return(reply_builder.ToString());
        }