microsoft / BotBuilder-Samples

Welcome to the Bot Framework samples repository. Here you will find task-focused samples in C#, JavaScript/TypeScript, and Python to help you get started with the Bot Framework SDK!
https://github.com/Microsoft/botframework
MIT License
4.38k stars 4.88k forks source link

Passing QnA Follow-Up prompts through to Facebook Messenger #1611

Closed seanbetts closed 5 years ago

seanbetts commented 5 years ago

Is your feature request related to a problem? Please describe. I am trying to pass QnA Maker follow-up prompts through to Facebook Messenger's Quick Replies.

Describe the solution you'd like To do this I'd like to pull through the prompts array and insert them into the code below, replacing "Prompt 1" with prompts.displayText located at prompts.displayOrder[0] then "Prompt 2" with prompts.displayText located at prompts.displayOrder[1] and so on

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

Describe alternatives you've considered I'm not sure of the correct syntax for doing the above and have tried multiple variations:

Additional context Here is the full code for my onMessage code segment so you can see how I'm constructing the logic to detect Facebook and to detect whether the QnA answer has a follow-up prompt. This works perfectly in WebChat, I'm just struggling to get the prompts to appear in Facebook Messenger as Quick Replies:

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: {
                            text: answer,
                            quick_replies: [
                              {
                                content_type: "text",
                                title: context.prompts.displayText,
                                payload: "Prompt 1"
                              },{
                                content_type: "text",
                                title: "Prompt 2",
                                payload: "Prompt 2"
                              }
                            ]
                        }
                      }

                        reply = quickReply;
                      } else {
                        reply = {
                          channelData: {
                            text: 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();
        });

[enhancement]

jwiley84 commented 5 years ago

I've noticed that you opened the same issue as a Stack Overflow question here. The Microsoft Bot Framework team prefers that how to questions be submitted on Stack Overflow. The official Bot Framework Github repos are the preferred platform for submitting bug fixes and feature requests. I'm going to close this issue so the support personnel and community-at-large can better assist you though the Stack Overflow query.

seanbetts commented 5 years ago

Thanks @jwiley84 - I didn't know whether this was a support request or a new feature request as the support for QnA Maker follow-up prompts is experimental.