使用 Node.js 让 Facebook Messenger 在 BotFramework v4 中显示 QnA 跟进提示
Getting Facebook Messenger to show QnA Follow-Up Prompts in BotFramework v4 using Node.js
我正在尝试弄清楚如何使用 BotFramework v4 和 Node.js.
让 Facebook Messenger 显示来自 QnA Maker 的后续提示
我已经设法在 WebChat 中显示后续提示:
我遵循了 Matt Stannett 在此线程中的重要建议后成功解决了这个问题:
但是,要让它们出现在 Facebook Messenger 中,我真的很费劲。
我希望它会像在我的 onMessage 代码中为 Facebook 快速回复定义一些 channelData 一样简单,因为我只需要 Facebook 传回一个简单的文本负载。我想我可以用类似的方式来做,我得到了显示网络聊天的提示,代码如下:
this.onMessage(async (context, next) => {
this.logger.log('Processing a Message Activity');
const qnaResults = await this.qnaMaker.getAnswers(context);
// Show choices if the Facebook Payload from ChannelData is not handled
if (!await this.processFacebookPayload(context, context.activity.channelData)) {
if (context.activity.channelId == 'facebook') {
if (qnaResults[0]) {
const { answer, context: { prompts }} = qnaResults[0];
let reply;
if (prompts.length) {
const quickReply = {
channelData: {
"messaging_type":"RESPONSE",
"message":{
"text":"test1", //answer,
"quick_replies":[
{
"content_type":"text",
"title":"test2",//prompts.map({ displayText }),
"payload":"test3",//prompts.map({ displayText })
}
]
}
}
}
reply = quickReply;
} else {
reply = answer;
}
await context.sendActivity(reply);
// If no answers were returned from QnA Maker, reply with help.
} else {
await context.sendActivity('I\'m sorry, I don\'t have an answer for that. Please ask me something else, such as: \n\n "What Is Mental Health?" \n\n "What Is NeuroDiversity" \n\n "Help"');
}
} else {
// If an answer was received from QnA Maker, send the answer back to the user.
if (qnaResults[0]) {
const { answer, context: { prompts }} = qnaResults[0];
let reply;
if (prompts.length) {
const card = {
"type": "AdaptiveCard",
"body": [
{
"type": "TextBlock",
"text": answer,
wrap: true
}
],
"actions": prompts.map(({ displayText }) => ({ type: "Action.Submit", title: displayText, data: displayText })),
"$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
"version": "1.1"
}
reply = { attachments: [CardFactory.adaptiveCard(card)] };
} else {
reply = answer;
}
await context.sendActivity(reply);
// If no answers were returned from QnA Maker, reply with help.
} else {
await context.sendActivity('I\'m sorry, I don\'t have an answer for that. Please ask me something else, such as: \n\n "What Is Mental Health?" \n\n "What Is NeuroDiversity" \n\n "Help"');
}
}
}
// By calling next() you ensure that the next BotHandler is run.
await next();
});
虽然这不起作用。我得到的是 QnA 对我提出的任何问题的回复,这些问题在 QnA Maker 中没有设置后续提示,所以我知道 IF 语句正确地将 Facebook 标识为一个频道,并且答案有后续提示与之相关。我想我只是没有获得 Facebook 快速回复的正确代码。
有人能帮忙吗?
提前致谢!
我已经通过修改我的快速回复通道数据设法获得了一些通用的快速回复:
const quickReply = {
channelData: {
text: answer,
quick_replies: [
{
content_type: "text",
title: "Prompt 1",
payload: "Prompt 1"
},{
content_type: "text",
title: "Prompt 2",
payload: "Prompt 2"
}
]
}
}
然后您可以创建一个变量以将 follow-up 提示插入频道数据:
var qnaPrompts = null;
if(qnaResults[0].context != null){
qnaPrompts = qnaResults[0].context.prompts;
}
接下来您需要re-map将数组以正确的格式转换为新数组以便快速回复:
var qnaPromptsArray = qnaPrompts.map(obj =>{
return {content_type: "text", title: obj.displayText, payload: obj.displayText}
});
如果您按如下方式更新快速回复,现在会在 Facebook Messenger 中显示 QnA follow-up 提示作为快速回复:
const quickReply = {
channelData: {
text: answer,
quick_replies: qnaPromptsArray
}
}
最后要解决的问题是如何将有效载荷 re-format 返回到 QnA 以使其接受的格式。为此,您需要将 turnContext 调整为 Activity.Text 中的字符串,如下所示,然后调用 QnA:
turnContext.activity.text = quickReply.payload;
const qnaResults = await this.qnaMaker.getAnswers(turnContext);
我正在尝试弄清楚如何使用 BotFramework v4 和 Node.js.
让 Facebook Messenger 显示来自 QnA Maker 的后续提示我已经设法在 WebChat 中显示后续提示:
我遵循了 Matt Stannett 在此线程中的重要建议后成功解决了这个问题:
但是,要让它们出现在 Facebook Messenger 中,我真的很费劲。
我希望它会像在我的 onMessage 代码中为 Facebook 快速回复定义一些 channelData 一样简单,因为我只需要 Facebook 传回一个简单的文本负载。我想我可以用类似的方式来做,我得到了显示网络聊天的提示,代码如下:
this.onMessage(async (context, next) => {
this.logger.log('Processing a Message Activity');
const qnaResults = await this.qnaMaker.getAnswers(context);
// Show choices if the Facebook Payload from ChannelData is not handled
if (!await this.processFacebookPayload(context, context.activity.channelData)) {
if (context.activity.channelId == 'facebook') {
if (qnaResults[0]) {
const { answer, context: { prompts }} = qnaResults[0];
let reply;
if (prompts.length) {
const quickReply = {
channelData: {
"messaging_type":"RESPONSE",
"message":{
"text":"test1", //answer,
"quick_replies":[
{
"content_type":"text",
"title":"test2",//prompts.map({ displayText }),
"payload":"test3",//prompts.map({ displayText })
}
]
}
}
}
reply = quickReply;
} else {
reply = answer;
}
await context.sendActivity(reply);
// If no answers were returned from QnA Maker, reply with help.
} else {
await context.sendActivity('I\'m sorry, I don\'t have an answer for that. Please ask me something else, such as: \n\n "What Is Mental Health?" \n\n "What Is NeuroDiversity" \n\n "Help"');
}
} else {
// If an answer was received from QnA Maker, send the answer back to the user.
if (qnaResults[0]) {
const { answer, context: { prompts }} = qnaResults[0];
let reply;
if (prompts.length) {
const card = {
"type": "AdaptiveCard",
"body": [
{
"type": "TextBlock",
"text": answer,
wrap: true
}
],
"actions": prompts.map(({ displayText }) => ({ type: "Action.Submit", title: displayText, data: displayText })),
"$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
"version": "1.1"
}
reply = { attachments: [CardFactory.adaptiveCard(card)] };
} else {
reply = answer;
}
await context.sendActivity(reply);
// If no answers were returned from QnA Maker, reply with help.
} else {
await context.sendActivity('I\'m sorry, I don\'t have an answer for that. Please ask me something else, such as: \n\n "What Is Mental Health?" \n\n "What Is NeuroDiversity" \n\n "Help"');
}
}
}
// By calling next() you ensure that the next BotHandler is run.
await next();
});
虽然这不起作用。我得到的是 QnA 对我提出的任何问题的回复,这些问题在 QnA Maker 中没有设置后续提示,所以我知道 IF 语句正确地将 Facebook 标识为一个频道,并且答案有后续提示与之相关。我想我只是没有获得 Facebook 快速回复的正确代码。
有人能帮忙吗?
提前致谢!
我已经通过修改我的快速回复通道数据设法获得了一些通用的快速回复:
const quickReply = {
channelData: {
text: answer,
quick_replies: [
{
content_type: "text",
title: "Prompt 1",
payload: "Prompt 1"
},{
content_type: "text",
title: "Prompt 2",
payload: "Prompt 2"
}
]
}
}
然后您可以创建一个变量以将 follow-up 提示插入频道数据:
var qnaPrompts = null;
if(qnaResults[0].context != null){
qnaPrompts = qnaResults[0].context.prompts;
}
接下来您需要re-map将数组以正确的格式转换为新数组以便快速回复:
var qnaPromptsArray = qnaPrompts.map(obj =>{
return {content_type: "text", title: obj.displayText, payload: obj.displayText}
});
如果您按如下方式更新快速回复,现在会在 Facebook Messenger 中显示 QnA follow-up 提示作为快速回复:
const quickReply = {
channelData: {
text: answer,
quick_replies: qnaPromptsArray
}
}
最后要解决的问题是如何将有效载荷 re-format 返回到 QnA 以使其接受的格式。为此,您需要将 turnContext 调整为 Activity.Text 中的字符串,如下所示,然后调用 QnA:
turnContext.activity.text = quickReply.payload;
const qnaResults = await this.qnaMaker.getAnswers(turnContext);