Example #1
0
 public static void UpdateAIML(this AIMLFile aiml, AIMLViewModel aimlVm)
 {
     aiml.BotID          = aimlVm.BotID;
     aiml.FormQnAnswerID = aimlVm.FormQnAnswerID;
     aiml.CardID         = aimlVm.CardID;
     aiml.ID             = aimlVm.ID;
     aiml.Name           = aimlVm.Name;
     aiml.Src            = aimlVm.Src;
     aiml.Extension      = aimlVm.Extension;
     aiml.Content        = aimlVm.Content;
     aiml.UserID         = aimlVm.UserID;
 }
        public HttpResponseMessage GetAimlQnA(HttpRequestMessage request, int formQnaID, string formAlias, string userID, int botID, bool status = false)
        {
            return(CreateHttpResponse(request, () =>
            {
                HttpResponseMessage response = null;
                var lstQnaGroup = _qnaService.GetListQuesGroupToAimlByFormQnAnswerID(formQnaID).ToList();
                bool IsAiml = false;
                // open file bot aiml
                //string pathFolderAIML = ConfigurationManager.AppSettings["AIMLPath"].ToString() + "User_" + userID + "_BotID_" + botID;
                string pathFolderAIML = PathServer.PathAIML;
                string nameFolderAIML = "User_" + userID + "_BotID_" + botID + "\\formQnA_ID_" + formQnaID + "_" + formAlias + ".aiml";
                string pathString = System.IO.Path.Combine(pathFolderAIML, nameFolderAIML);

                var aimlDb = _aimlService.GetByFormId(formQnaID);
                StringBuilder sbFormDb = new StringBuilder();
                //if (System.IO.File.Exists(pathString))
                //{
                //File.WriteAllText(pathString, String.Empty);
                try
                {
                    //StreamWriter sw = new StreamWriter(pathString, true, Encoding.UTF8);
                    //////sw.WriteLine("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
                    ////sw.WriteLine("<aiml>");
                    //sw.WriteLine("<category>");
                    //sw.WriteLine("<pattern>*</pattern>");
                    //sw.WriteLine("<template>");
                    //sw.WriteLine("<random>");
                    //sw.WriteLine("<li> NOT_MATCH_01 </li>");
                    //sw.WriteLine("<li> NOT_MATCH_02 </li>");
                    //sw.WriteLine("<li> NOT_MATCH_03 </li>");
                    //sw.WriteLine("<li> NOT_MATCH_04 </li>");
                    //sw.WriteLine("<li> NOT_MATCH_05 </li>");
                    //sw.WriteLine("<li> NOT_MATCH_06 </li>");
                    //sw.WriteLine("</random>");
                    //sw.WriteLine("</template>");
                    //sw.WriteLine("</category>");

                    sbFormDb.AppendLine("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
                    sbFormDb.AppendLine("<aiml>");
                    sbFormDb.AppendLine("<category>");
                    sbFormDb.AppendLine("<pattern>*</pattern>");
                    sbFormDb.AppendLine("<template>");
                    sbFormDb.AppendLine("<random>");
                    sbFormDb.AppendLine("<li> NOT_MATCH_01 </li>");
                    sbFormDb.AppendLine("<li> NOT_MATCH_02 </li>");
                    sbFormDb.AppendLine("<li> NOT_MATCH_03 </li>");
                    sbFormDb.AppendLine("<li> NOT_MATCH_04 </li>");
                    sbFormDb.AppendLine("<li> NOT_MATCH_05 </li>");
                    sbFormDb.AppendLine("<li> NOT_MATCH_06 </li>");
                    sbFormDb.AppendLine("</random>");
                    sbFormDb.AppendLine("</template>");
                    sbFormDb.AppendLine("</category>");
                    if (lstQnaGroup != null && lstQnaGroup.Count() != 0)
                    {
                        int total = lstQnaGroup.Count();
                        for (int indexQGroup = 0; indexQGroup < total; indexQGroup++)
                        {
                            var itemQGroup = lstQnaGroup[indexQGroup];
                            var lstAnswer = itemQGroup.Answers.ToList();
                            var lstQuestion = itemQGroup.Questions.ToList();
                            string postbackAnswer = String.Empty;
                            if (lstAnswer.Count() != 0 && lstAnswer.Count() > 1)
                            {
                                StringBuilder sb = new StringBuilder();

                                int totalAnswer = lstAnswer.Count();
                                postbackAnswer = "postback_answer_" + itemQGroup.ID;
                                sb.AppendLine("<category>");
                                sb.AppendLine("<pattern>" + postbackAnswer + "</pattern>");
                                sb.AppendLine("<template>");
                                sb.AppendLine("<random>");
                                for (int indexA = 0; indexA < totalAnswer; indexA++)
                                {
                                    var itemAnswer = lstAnswer[indexA];
                                    if (!String.IsNullOrEmpty(itemAnswer.ContentText))
                                    {
                                        sb.AppendLine("<li>" + itemAnswer.ContentText + "</li>");
                                    }
                                    else
                                    {
                                        sb.AppendLine("<li>" + itemAnswer.CardPayload + "</li>");
                                    }
                                }
                                sb.AppendLine("</random>");
                                sb.AppendLine("</template>");
                                sb.AppendLine("</category>");
                                //sw.WriteLine(sb.ToString());
                                sbFormDb.AppendLine(sb.ToString());
                            }
                            else
                            {
                                if (!String.IsNullOrEmpty(lstAnswer[0].ContentText))
                                {
                                    postbackAnswer = lstAnswer[0].ContentText;
                                }
                                else
                                {
                                    postbackAnswer = lstAnswer[0].CardPayload;
                                }
                            }
                            if (lstQuestion.Count != 0)
                            {
                                foreach (var item in lstQuestion)
                                {
                                    for (int indexQ = 0; indexQ < _userSayStart.Length; indexQ++)
                                    {
                                        var itemQ = item;
                                        string patternText = "";
                                        string tempAnswer = postbackAnswer;
                                        if (postbackAnswer.Contains("postback"))
                                        {
                                            tempAnswer = "<srai>" + postbackAnswer + "</srai>";
                                        }
                                        //sw.WriteLine("<category>");
                                        //sw.WriteLine("<pattern>"+itemQ.ContentText.ToUpper()+"</pattern>");
                                        //sw.WriteLine("<template>"+ tempAnswer + "</template>");
                                        //sw.WriteLine("</category>");
                                        if (_userSayStart[indexQ] == CommonConstants.UserSay_IsStartDefault)
                                        {
                                            patternText = itemQ.ContentText.ToUpper();
                                        }
                                        if (_userSayStart[indexQ] == CommonConstants.UserSay_IsStartFirst)
                                        {
                                            patternText = "* " + itemQ.ContentText.ToUpper();
                                        }
                                        if (_userSayStart[indexQ] == CommonConstants.UserSay_IsStartLast)
                                        {
                                            patternText = itemQ.ContentText.ToUpper() + " *";
                                        }
                                        if (_userSayStart[indexQ] == CommonConstants.UserSay_IsStartDouble)
                                        {
                                            patternText = "* " + itemQ.ContentText.ToUpper() + " *";
                                        }
                                        sbFormDb.AppendLine("<category>");
                                        sbFormDb.AppendLine("<pattern>" + patternText + "</pattern>");
                                        sbFormDb.AppendLine("<template>" + tempAnswer + "</template>");
                                        sbFormDb.AppendLine("</category>");
                                    }
                                }
                            }
                        }
                    }
                    //sw.WriteLine("</aiml>");
                    sbFormDb.AppendLine("</aiml>");
                    //sw.Close();
                    IsAiml = true;

                    if (aimlDb == null)
                    {
                        AIMLFile aimlFileDb = new AIMLFile();
                        aimlFileDb.UserID = userID;
                        aimlFileDb.BotID = botID;
                        aimlFileDb.Src = nameFolderAIML;
                        aimlFileDb.FormQnAnswerID = formQnaID;
                        aimlFileDb.Extension = "aiml";
                        aimlFileDb.Name = "Form-" + formAlias;
                        aimlFileDb.Status = status;
                        aimlFileDb.Content = sbFormDb.ToString();
                        _aimlService.Create(aimlFileDb);
                        _aimlService.Save();
                    }
                    else
                    {
                        aimlDb.Content = sbFormDb.ToString();
                        aimlDb.Status = status;
                        _aimlService.Update(aimlDb);
                        _aimlService.Save();
                    }
                }
                catch (Exception ex)
                {
                    IsAiml = false;
                    response = request.CreateResponse(HttpStatusCode.OK, IsAiml);
                    return response;
                }
                finally
                {
                }
                //}
                response = request.CreateResponse(HttpStatusCode.OK, IsAiml);
                return response;
            }));
        }
Example #3
0
 public void Update(AIMLFile aiml)
 {
     _aimlRepository.Update(aiml);
 }
Example #4
0
 public AIMLFile Create(AIMLFile aiml)
 {
     return(_aimlRepository.Add(aiml));
 }