Facebook 将Microsoft QnA Maker连接到Bot框架

Facebook 将Microsoft QnA Maker连接到Bot框架,facebook,frameworks,bots,Facebook,Frameworks,Bots,我已经使用QnA Maker创建了一个机器人,并设置了一个连接到我们的FB页面和Microsoft机器人框架的FB应用程序。然而,缺少了一些东西。如何将Microsoft QnA maker连接到Bot框架?(FWIW-目标是一个FB Messenger机器人,回答有关非营利活动的常见问题)。谢谢您不能直接将QnAMaker端点链接到FB。您首先需要使用QnAMaker模板创建一个Bot服务,然后在FB频道上启用它。请参见您需要在QNA maker上注册并使用以下代码获得响应。无需在bot框架上

我已经使用QnA Maker创建了一个机器人,并设置了一个连接到我们的FB页面和Microsoft机器人框架的FB应用程序。然而,缺少了一些东西。如何将Microsoft QnA maker连接到Bot框架?(FWIW-目标是一个FB Messenger机器人,回答有关非营利活动的常见问题)。谢谢

您不能直接将QnAMaker端点链接到FB。您首先需要使用QnAMaker模板创建一个Bot服务,然后在FB频道上启用它。请参见

您需要在QNA maker上注册并使用以下代码获得响应。无需在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.");
}
使用Newtonsoft.Json;
私有类QnAMakerResult
{
/// 
///在QnA服务中找到的最佳答案。
/// 
[JsonProperty(PropertyName=“answer”)]
公共字符串答案{get;set;}
/// 
///与QnA服务中的顶级答案相对应的[01100]范围内的分数。
/// 
[JsonProperty(PropertyName=“score”)]
公共双倍分数{get;set;}
}
//反序列化响应
QnAMakerResult响应;
尝试
{
response=JsonConvert.DeserializeObject(responseString);
}
抓住
{
抛出新异常(“无法反序列化QnA生成器响应字符串”);
}
注意:要获取知识库id和订阅密钥,您需要登录并创建服务

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.");
}

如果您需要任何帮助,请务必告诉我,以便在bot框架中使用QnAMaker,当您获得无意图或某些意图时,您可以处理t。 只需将其添加到您的LUIS对话框中即可

[LuisModel("modelID", "SubscriptionKey")]
[Serializable]
public class RootDialog : LuisDialog<object>
{
    [LuisIntent("None")]
    public async Task NoneRes(IDialogContext context, LuisResult result)
    {
            var qnadialog = new QnADialog();
            await context.Forward(new QnADialog(), AfterQnADialog, context.Activity, CancellationToken.None);
    }
    private async Task AfterQnADialog(IDialogContext context, IAwaitable<object> result)
    {
        var answerFound = await result;
        // handle after qna response
    }
}
完成了。
希望这将有助于

最好只使用MS Azure而不是Bot框架。简单!