/// <summary> /// 處理 Train & Publish /// </summary> private void QNAMakerUpdate() { string trainMsg, publishMsg; maduka_QnAMakerLibrary.API.QnAMaker QNAMaker = new maduka_QnAMakerLibrary.API.QnAMaker(); QNAMaker.SubscriptionKey = SubscriptionKey; HttpStatusCode code = HttpStatusCode.OK; QNAMaker.TrainKB(strKbId, out code); if (code == HttpStatusCode.NoContent) { trainMsg = "Train KB Success"; } else { trainMsg = "Train KB Fail:" + code.ToString(); } QNAMaker.PublishKB(strKbId, out code); if (code == HttpStatusCode.NoContent) { publishMsg = "Publish KB Success"; } else { publishMsg = "Publish KB Fail:" + code.ToString(); } LintBot.ReplyMessage(ReceivedMessage.events[0].replyToken, string.Format("{0},{1} ; {2}", username, trainMsg, publishMsg)); }
/// <summary> /// 處理送出 Question 取回 Answer /// </summary> /// <param name="pquestion"></param> private void QNAMakerGenerateAnswer(string pquestion) { KBModel.GenerateAnswerModel objQuery = new KBModel.GenerateAnswerModel() { question = pquestion, top = 1, }; maduka_QnAMakerLibrary.API.QnAMaker QNAMaker = new maduka_QnAMakerLibrary.API.QnAMaker(); QNAMaker.SubscriptionKey = SubscriptionKey; HttpStatusCode code = HttpStatusCode.OK; KBModel.GenerateAnswerResultModel result = QNAMaker.GenerateAnswer(strKbId, objQuery, out code); string remsg = string.Empty; if (code == HttpStatusCode.OK) { // 取出最相似的回覆,並放在文字方塊中==> 以下程式碼取消,因為就算 match 不到,系統也會回覆:No good match found in the KB (Count = 1) //if (result.answers.Count > 0) //{ // remsg = string.Format("{0},{1}({2})", username, result.answers[0].answer, result.answers[0].score); //} //else //{ // remsg = string.Format("{0},{1}", username, "哩哄啥,哇聽某!"); //} // 取出回覆的分數是 >= 50 ==> 改採用分數來判斷,如果不到規定的分數就完全不理會 if (result.answers[0].score >= 50) { //處理回覆內容中是否已含了自己的名稱,如果有,就不再加稱呼, if (result.answers[0].answer.Contains(username)) { remsg = string.Format("{0}(Score={1})", result.answers[0].answer, result.answers[0].score); } else { remsg = string.Format("{0},{1}(Score={2})", username, result.answers[0].answer, result.answers[0].score); } LintBot.ReplyMessage(ReceivedMessage.events[0].replyToken, remsg); } } else { LintBot.ReplyMessage(ReceivedMessage.events[0].replyToken, "Generate Answer Fail:" + code.ToString()); } }
/// <summary> /// 處理新增 Knowledge Base Q & A 問題 /// </summary> /// <param name="pquestion"></param> private void QNAMakerAddQnA(string pquestion) { List <KBModel.QnAQueryList> objQnA = new List <KBModel.QnAQueryList>(); KBModel.UpdateKBModel objUpdate = new KBModel.UpdateKBModel() { add = new KBModel.UpdateKBModel.Add() { qnaPairs = new List <KBModel.QnAList>(), urls = new List <string>() }, delete = new KBModel.UpdateKBModel.Delete() { qnaPairs = new List <KBModel.QnAList>() }, }; int startQ = pquestion.IndexOf("AddQ:"); int startA = pquestion.IndexOf("AddA:"); string AddQ = pquestion.Substring(startQ + 5, startA - 5); string AddA = pquestion.Substring(startA + 5); Console.WriteLine(AddQ.Trim()); Console.WriteLine(AddA.Trim()); objQnA.Add( new KBModel.QnAQueryList() { answer = AddA.Trim(), question = AddQ.Trim(), source = "add", } ); for (int i = 0; i < objQnA.Count; i++) { if (objQnA[i].source == "delete") { objUpdate.delete.qnaPairs.Add(new KBModel.QnAList() { answer = objQnA[i].answer, question = objQnA[i].question, } ); } else if (objQnA[i].source == "add") { objUpdate.add.qnaPairs.Add(new KBModel.QnAList() { answer = objQnA[i].answer, question = objQnA[i].question, } ); } } maduka_QnAMakerLibrary.API.QnAMaker QNAMaker = new maduka_QnAMakerLibrary.API.QnAMaker(); QNAMaker.SubscriptionKey = SubscriptionKey; HttpStatusCode code = HttpStatusCode.OK; string strMsg = QNAMaker.UpdateKB(strKbId, objUpdate, out code); if (code == HttpStatusCode.NoContent) { LintBot.ReplyMessage(ReceivedMessage.events[0].replyToken, string.Format("{0},{1}", username, "Update KB Success")); } else { LintBot.ReplyMessage(ReceivedMessage.events[0].replyToken, "Update KB Fail:" + code.ToString()); } }