【问题标题】:Getting Facebook Messenger to show QnA Follow-Up Prompts in BotFramework v4 using Node.js让 Facebook Messenger 使用 Node.js 在 BotFramework v4 中显示 QnA 后续提示
【发布时间】:2019-06-29 22:37:16
【问题描述】:

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

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

在听从了 Matt Stannett 在这个帖子中的好建议之后,我做到了这一点: How to implement cards in a QnA question which has follow up prompts and uses them in the cards

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

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

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 快速回复的代码。

谁能帮忙?

提前致谢!

【问题讨论】:

    标签: node.js botframework facebook-messenger qnamaker


    【解决方案1】:

    通过修改我的 quickReply 通道数据,我设法让一些通用的快速回复正常工作:

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

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

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

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

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

    如果您按如下方式更新 quickReply,现在将 QnA 后续提示显示为 Facebook Messenger 中的快速回复:

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

    最后要解决的是如何将有效负载重新格式化回 QnA 以使其接受的格式。为此,您需要将 turnContext 调整为 Activity.Text 中的字符串,如下所示,然后调用 QnA:

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

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-18
    • 1970-01-01
    • 2020-03-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多