2017-05-31 163 views
0

我已經使用QnA Maker創建了一個bot並設置了一個FB應用程序連接到我們的FB頁面和Microsoft Bot Framework。但是,缺少一些東西。我如何將Microsoft QnA製造商連接到Bot Framework? (FWIW--目標是一個FB信使機器人,它回答有關非贏利事件的FAQ)。謝謝將Microsoft QnA Maker連接到Bot Framework

+0

最好只使用MS Azure,而不是Bot Framework。簡單! –

回答

0

您需要註冊QNA製造商並使用下面的代碼來獲取響應。無需在bot框架上註冊。

樣品申請

string responseString = string.Empty; 

var query = 「hi」; //User Query 
var knowledgebaseId = 「YOUR_KNOWLEDGE_BASE_ID」; // Use knowledge base id created. 
var qnamakerSubscriptionKey = 「YOUR_SUBSCRIPTION_KEY」; //Use subscription key assigned to you. 

//Build the URI 
Uri qnamakerUriBase = new Uri("https://westus.api.cognitive.microsoft.com/qnamaker/v1.0"); 
var builder = new UriBuilder($"{qnamakerUriBase}/knowledgebases/{knowledgebaseId}/generateAnswer"); 

//Add the question as part of the body 
var postBody = $"{{\"question\": \"{query}\"}}"; 

//Send the POST request 
using (WebClient client = new WebClient()) 
{ 
    //Set the encoding to UTF8 
    client.Encoding = System.Text.Encoding.UTF8; 

    //Add the subscription key header 
    client.Headers.Add("Ocp-Apim-Subscription-Key", qnamakerSubscriptionKey); 
    client.Headers.Add("Content-Type", "application/json"); 
    responseString = client.UploadString(builder.Uri, postBody); 
} 

樣品響應

using Newtonsoft.Json; 

private class QnAMakerResult 
{ 
    /// <summary> 
    /// The top answer found in the QnA Service. 
    /// </summary> 
    [JsonProperty(PropertyName = "answer")] 
    public string Answer { get; set; } 

    /// <summary> 
    /// The score in range [0, 100] corresponding to the top answer found in the QnA Service. 
    /// </summary> 
    [JsonProperty(PropertyName = "score")] 
    public double Score { get; set; } 
} 
//De-serialize the response 
QnAMakerResult response; 
try 
{ 
    response = JsonConvert.DeserializeObject<QnAMakerResult>(responseString); 
} 
catch 
{ 
    throw new Exception("Unable to deserialize QnA Maker response string."); 
} 

注:獲得您需要登錄和創建服務

知識庫ID和訂閱密鑰請告訴我,以防萬一你編輯任何幫助

相關問題