iT邦幫忙

0

關於QnA Maker 與 LINE BOT

QnA Maker 我這邊實作 但是一直發生發生錯誤:
The remote server returned an error: (400) Bad Request.
https://ithelp.ithome.com.tw/upload/images/20180930/20110539S4Je9hTdAf.jpg
但是我用Postman POST 測試正常
https://ithelp.ithome.com.tw/upload/images/20180930/20110539CrBlZTRLPL.jpg
搞不清楚 哪邊CODE 發生問題
https://ithelp.ithome.com.tw/upload/images/20180930/20110539BuR59gAYUl.jpg

你貼圖有成功送出表示 你的Line 發送機制沒問題
那問題就在於你去撈資料那一段出現了問題 而且上面也有錯誤號碼(400)
https://zh.wikipedia.org/wiki/HTTP%E7%8A%B6%E6%80%81%E7%A0%81
小鄭 iT邦新手 5 級 ‧ 2018-10-25 21:53:16 檢舉
已經處理好了~感謝 更新問題

1 個回答

1

不太清楚您使用的MsQnAMaker.Client內部是怎麼實作的,
但可能原因是發的request body格式不正確
您可以去你azure websites的LOG查看詳細的錯誤訊息

把您要送的request body整理好再POST出去

{
    "name" : "QnA Maker FAQ Bot",
    "qnaPairs": [
        {
            "answer": "You can change the default message if you use the QnAMakerDialog. See this for details: https://docs.botframework.com/en-us/azure-bot-service/templates/qnamaker/#navtitle",
            "question": "How can I change the default message from QnA Maker?"
        },
        {
            "answer": "You can use our REST apis to manage your KB. See here for details: https://westus.dev.cognitive.microsoft.com/docs/services/58994a073d9e04097c7ba6fe/operations/58994a073d9e041ad42d9baa",
            "question": "How do I programmatically update my KB?"
        }
    ],
    "urls": [
        "https://docs.microsoft.com/en-in/azure/cognitive-services/qnamaker/faqs"
    ]
}

可能關聯的錯誤400 Error

{
  "error": {
    "code": "BadArgument",
    "message": "Request body is invalid."
  }
}

建議可以先用官方的Sample Code測試看看喔

using System;
using System.Net.Http.Headers;
using System.Text;
using System.Net.Http;
using System.Web;

namespace CSHttpClientSample
{
    static class Program
    {
        static void Main()
        {
            MakeRequest();
            Console.WriteLine("Hit ENTER to exit...");
            Console.ReadLine();
        }
        
        static async void MakeRequest()
        {
            var client = new HttpClient();
            var queryString = HttpUtility.ParseQueryString(string.Empty);

            // Request headers
            client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", "{subscription key}");

            var uri = "https://westus.api.cognitive.microsoft.com/qnamaker/v2.0/knowledgebases/create?" + queryString;

            HttpResponseMessage response;

            // Request body
            byte[] byteData = Encoding.UTF8.GetBytes("{body}");

            using (var content = new ByteArrayContent(byteData))
            {
               content.Headers.ContentType = new MediaTypeHeaderValue("< your content type, i.e. application/json >");
               response = await client.PostAsync(uri, content);
            }

        }
    }
}	

參考資料:https://westus.dev.cognitive.microsoft.com/docs/services/58994a073d9e04097c7ba6fe/operations/58994a073d9e041ad42d9baa

小鄭 iT邦新手 5 級 ‧ 2018-10-25 21:52:48 檢舉

已經處理好了~感謝 更新問題

我要發表回答

立即登入回答