private async Task MessageReceivedAsync(IDialogContext context, IAwaitable <string> result) { var message = await result; await context.PostAsync($"ちょっと調べてみますね!"); // QnA Service からデータを取得する QnAMService QnAMService = new QnAMService(); QnAModel qnaRes = await QnAMService.GetQnA(message, "tomohiku", ""); if (qnaRes.answers[0].id != -1) { for (int x = 0; x < qnaRes.answers.Length; ++x) { if (qnaRes.answers[x].score > QnAConfidenceThreshold) { Array.Resize(ref questions, x + 1); // metadataに入っている「rep」キーのテキストを代表質問として扱う foreach (var i in qnaRes.answers[x].metadata) { if (i.name == "rep") { questions[x] = i.value; } } // metadataに該当のキーが無かったら、最初の質問を代表質問として扱う if (string.IsNullOrEmpty(questions[x])) { questions[x] = qnaRes.answers[x].questions[0]; } Array.Resize(ref answers, x + 1); answers[x] = qnaRes.answers[x].answer; } } if (questions.Length >= 2) { // await context.PostAsync($"■代表質問■\n" + questions[0] + "\n■回答■\n" + answers[0]); PromptDialog.Choice(context, this.ShowAnswerAsync, questions, $"この中にご質問はありますか?"); } else if (questions.Length == 1) { await context.PostAsync($"■代表質問■\n" + questions[0] + "\n■回答■\n" + answers[0]); PromptDialog.Choice(context, this.SaveyAsync, ShowAnswerAsyncText, $"お役に立ちましたでしょうか?"); } } else { PromptDialog.Text(context, MessageReceivedAsync, $"申し訳ありません。適した答えが見つかりませんでした。再度ご質問をどうぞ!"); } }
/// <summary> /// QnA Makerに問い合わせし、回答を取得するFunction /// </summary> public async Task <QnAModel> GetQnA(string message, string UserId, string FilterStirng) { // AppInsightの定義 TelemetryClient telemetry = new TelemetryClient(); telemetry.Context.User.Id = UserId; var methodname = "/knowledgebases/" + qnaKBId + "/generateAnswer/"; var uri = endpointHostName + methodname; string option = ""; if (!string.IsNullOrEmpty(UserId)) { option = ",'userId': '" + UserId + "'"; } if (!string.IsNullOrEmpty(FilterStirng)) { option = ",'strictFilters': [{'name': 'category','value': '" + FilterStirng + "'}]"; } var question = @"{'question': '" + message + @"','top': " + QnAGetCnt + option + " }"; //private QnAModel qnaRes; var qnaRes = new QnAModel(); try { var resjson = await Post(uri, question); qnaRes = JsonConvert.DeserializeObject <QnAModel>(resjson); // App Insightにログをセットするためのコード string TraceMessage = "QnAMaker Response"; var properties = new Dictionary <string, string> { }; properties.Add("InputText", message); for (int x = 0; x < qnaRes.answers.Length; ++x) { string questions = ""; foreach (var i in qnaRes.answers[x].questions) { if (questions == "") { questions = i; } else { questions = questions + ";" + i; } } properties.Add("questions_" + x, questions); properties.Add("answer_" + x, qnaRes.answers[x].answer); properties.Add("score_" + x, Convert.ToString(qnaRes.answers[x].score)); properties.Add("id_" + x, Convert.ToString(qnaRes.answers[x].id)); properties.Add("source_" + x, qnaRes.answers[x].source); // metadataに入っている「rep」キーのテキストを代表質問として扱う foreach (var i in qnaRes.answers[x].metadata) { properties.Add(i.name + x, i.value); } } //telemetry.TrackEvent("QnA Maker Result", properties); telemetry.TrackTrace(TraceMessage, SeverityLevel.Information, properties); } catch (Exception ex) { telemetry.TrackException(ex); } return(qnaRes); }