最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

在QnAMaker中使用条件运算符

运维笔记admin14浏览0评论

在QnAMaker中使用条件运算符

在QnAMaker中使用条件运算符

我很难搞清楚最有可能是一个简单的问题,这与我的代码中的“if then else”问题有关(NodeJS,Bot Framework v4)。

我无法弄清楚为什么没有显示相关的卡,这取决于它在QnAMaker的响应字符串中找到的分号的数量。

在使用Bot Framework模拟器进行测试时,它只返回一种响应类型,无论是纯文本还是一张Rich Card,无论响应中有多少个分号。

我试图通过解析长度语句中的数字值来查看它是否存在问题的字符串的长度。遗憾地没有什么区别。值得注意的是,如果我使用任何其他条件运算符,例如'===',它会完全破坏响应。

 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  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  for more details.
                    const welcomeCard = CardFactory.adaptiveCard(WelcomeCard);
                    await turnContext.sendActivity({ attachments: [welcomeCard] });
                }
            }
        }
    }
}
}

module.exports.MyBot = MyBot;

理想情况下,我希望看到的是,如果我在回答中提出一个有3个分号的问题,它会输出一张英雄卡。如果它超过3,则视频卡,如果它没有,则为文本响应。

回答如下:

我不是js专家,但我对以下内容感到困惑:

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

在Javascript中,includes是以下(source):

includes()方法确定数组是否在其条目中包含某个值,并在适当时返回true或false。

所以这里你的qnaCardtruefalse。但看起来你正试图使用​​它,好像它包含文本:

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

你必须处理包含答案的对象:qnaResults[0].answer

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论