0

I'm having difficulty figuring out what most likely is a simple issue, which relates to a 'if then else' problem in my code (NodeJS, Bot Framework v4).

I can't quite figure out why the relevant card isn't being shown depending on the number of semi-colons it finds in the response string from QnAMaker.

When testing with the Bot Framework emulator, it only returns one response type, whether that's plain text or one Rich Card no matter how many semi-colons are in the response.

I've tried to see if it's the length of the string it's having problems with by parsing the number value in the length statement. Didn't make a difference sadly. Notably if I use any other conditional operator such as '===' for example, it breaks the response completely.

 const { ActivityTypes, CardFactory } = require('botbuilder');
 const { WelcomeCard } = require('./dialogs/welcome');
 // const { HeroCard } = require('./dialogs/welcome');
 // const { VideoCard } = require('./dialogs/welcome');

class MyBot {
/**
 *
 * @param {TurnContext} on turn context object.
 */

constructor(qnaServices) {
    this.qnaServices = qnaServices;
}

async onTurn(turnContext) {
    if (turnContext.activity.type === ActivityTypes.Message) {
        for (let i = 0; i < this.qnaServices.length; i++) {
            // Perform a call to the QnA Maker service to retrieve matching Question and Answer pairs.
            const qnaResults = await this.qnaServices[i].getAnswers(turnContext);
            const qnaCard = qnaResults.includes(';');

            // If an answer was received from QnA Maker, send the answer back to the user and exit.
            if (qnaCard.toString().split(';').length < 3) {
                await turnContext.sendActivity(qnaResults[0].answer);
                await turnContext.sendActivity({
                    text: 'Hero Card',
                    attachments: [CardFactory.heroCard(HeroCard)]
                });

            } else if (qnaCard.toString().split(';').length > 3) {
                await turnContext.sendActivity(qnaResults[0].answer);
                await turnContext.sendActivity({
                    text: 'Video Card',
                    attachments: [CardFactory.videoCard(VideoCard)]
                });

            } else if (qnaCard.toString().split(';').length === 0) { 
                    await turnContext.sendActivity(qnaResults[0].answer);
             return;
            }
        }
        // If no answers were returned from QnA Maker, reply with help.
        await turnContext.sendActivity('No QnA Maker answers were found.');
    } else {
        await turnContext.sendActivity(`[${ turnContext.activity.type } event detected]`);
    } if (turnContext.activity.type === ActivityTypes.ConversationUpdate) {
        // Handle ConversationUpdate activity type, which is used to indicates new members add to
        // the conversation.
        // See https://aka.ms/about-bot-activity-message to learn more about the message and other activity types

        // Do we have any new members added to the conversation?
        if (turnContext.activity.membersAdded.length !== 0) {
            // Iterate over all new members added to the conversation
            for (var idx in turnContext.activity.membersAdded) {
                // Greet anyone that was not the target (recipient) of this message
                // the 'bot' is the recipient for events from the channel,
                // context.activity.membersAdded == context.activity.recipient.Id indicates the
                // bot was added to the conversation.
                if (turnContext.activity.membersAdded[idx].id !== turnContext.activity.recipient.id) {
                    // Welcome user.
                    // When activity type is "conversationUpdate" and the member joining the conversation is the bot
                    // we will send our Welcome Adaptive Card.  This will only be sent once, when the Bot joins conversation
                    // To learn more about Adaptive Cards, see https://aka.ms/msbot-adaptivecards for more details.
                    const welcomeCard = CardFactory.adaptiveCard(WelcomeCard);
                    await turnContext.sendActivity({ attachments: [welcomeCard] });
                }
            }
        }
    }
}
}

module.exports.MyBot = MyBot;

Ideally, what I'm hoping to see is if I ask a question which has 3 semi-colons in the response, it outputs a Hero Card. If it has more than 3, then a Video Card and if it doesn't have either, a text response.

Steve Johnson
  • 405
  • 3
  • 8

1 Answers1

2

I'm not a js specialist, but I'm quite confused by the following:

const qnaCard = qnaResults.includes(';');

In Javascript, includes is the following (source):

The includes() method determines whether an array includes a certain value among its entries, returning true or false as appropriate.

So here your qnaCard is true or false. But it looks like you are trying to use it as if it was containing the text:

if (qnaCard.toString().split(';').length < 3) {
    ...

You have to work on the object containing the answer: qnaResults[0].answer.

Nicolas R
  • 13,812
  • 2
  • 28
  • 57