使用Node.js让Facebook Messenger在BotFramework v4中显示QnA后续提示 [英] Getting Facebook Messenger to show QnA Follow-Up Prompts in BotFramework v4 using Node.js

查看:132
本文介绍了使用Node.js让Facebook Messenger在BotFramework v4中显示QnA后续提示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试弄清楚如何使Facebook Messenger使用BotFramework v4和Node.js从QnA Maker显示后续提示.

I'm trying to figure out how to get Facebook Messenger to show follow-up prompts from QnA Maker using BotFramework v4 and Node.js.

我设法在WebChat中显示了后续提示:

I've managed to get the follow-up prompts showing in WebChat:

我在遵循此主题的Matt Stannett的出色建议后设法做到了这一点:

I managed this after following the great advice from Matt Stannett in this thread: How to implement cards in a QnA question which has follow up prompts and uses them in the cards

但是,要让它们出现在Facebook Messenger中,我真的很努力.

However, when it comes to getting them to appear in Facebook Messenger, I'm really struggling.

我希望它就像在onMessage代码中为Facebook快速回复定义一些channelData一样简单,因为我只需要Facebook回传一个简单的文本有效负载即可.我以为我可以用类似的方式来做到这一点,因为我得到了有关Webchat的提示,如下所示:

I was hoping it would be as straightforward as defining some channelData for Facebook Quick Replies in my onMessage code, as I just need Facebook to pass back a simple text payload. I thought I could do it in a similar way I got the prompts showing for Webchat, code below:

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中快速回复.

This isn't working though. What I do get is QnA replies for any questions I ask that don't have a follow-up prompt set in QnA Maker, so I know that the IF statement is correctly identifying Facebook as a channel and that an answer has follow-up prompts associated with it. I think I just haven't got the code right for the quick replies in Facebook.

任何人都可以帮忙吗?

提前谢谢!

推荐答案

通过修改quickReply channelData,我设法获得了一些通用的快速回复:

I've managed to get some generic Quick Replies working by modifying my quickReply channelData:

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"
                          }
                        ]
                    }
                  }

然后您可以创建一个变量,以将后续提示插入频道数据:

You can then create a variable to insert the follow-up prompt into the channel data:

var qnaPrompts = null;
if(qnaResults[0].context != null){
   qnaPrompts = qnaResults[0].context.prompts;
}

接下来,您需要以正确的格式将阵列重新映射到新阵列,以便快速回复:

Next you need to re-map the array to a new array in the correct format for quick replies:

var qnaPromptsArray = qnaPrompts.map(obj =>{
   return {content_type: "text", title: obj.displayText, payload: obj.displayText}
});

现在,如果您按以下方式更新quickReply,它将立即在Facebook Messenger中显示QnA跟进提示作为快速答复:

This now displays the QnA follow-up prompt as a quick reply in Facebook Messenger if you update quickReply as follows:

const quickReply = {
   channelData: {
      text: answer,
      quick_replies: qnaPromptsArray
   }
}

要解决的最后一件事是如何将有效载荷重新格式化回QnA,使其成为可接受的格式.为此,您需要按以下方式将turnContext调整为Activity.Text中的字符串,然后调用QnA:

The final thing to work out is how to re-format the payload back to the QnA into a format that it accepts. To do this you need to adjust your turnContext to a string in Activity.Text as follows and then call the QnA:

turnContext.activity.text = quickReply.payload;
const qnaResults = await this.qnaMaker.getAnswers(turnContext);

这篇关于使用Node.js让Facebook Messenger在BotFramework v4中显示QnA后续提示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆