将条件运算符与 QnAMaker 结合使用 - 运算符未正确路由
Using conditional operators with QnAMaker - operators aren't routing correctly
我很难确定什么最有可能是一个简单的问题,它与我的代码(NodeJS、Bot Framework v4)中的 'if then else' 问题有关。
我不太明白为什么相关卡片没有显示,具体取决于它在 QnAMaker 的响应字符串中找到的分号数量。
使用 Bot Framework 模拟器进行测试时,它只有 returns 一种响应类型,无论响应中有多少分号,无论是纯文本还是一张 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 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;
理想情况下,我希望看到的是,如果我提出的问题在响应中有 3 个分号,它会输出一张英雄卡。如果超过 3 个,则为视频卡,如果两者都没有,则文本回复。
我不是 js 专家,但我对以下内容感到很困惑:
const qnaCard = qnaResults.includes(';');
在Javascript中,includes
如下(source):
The includes() method determines whether an array includes a certain
value among its entries, returning true or false as appropriate.
所以这里你的 qnaCard
是 true
或 false
。但看起来您正在尝试使用它,就好像它包含文本一样:
if (qnaCard.toString().split(';').length < 3) {
...
您必须处理包含答案的对象:qnaResults[0].answer
。
我很难确定什么最有可能是一个简单的问题,它与我的代码(NodeJS、Bot Framework v4)中的 'if then else' 问题有关。
我不太明白为什么相关卡片没有显示,具体取决于它在 QnAMaker 的响应字符串中找到的分号数量。
使用 Bot Framework 模拟器进行测试时,它只有 returns 一种响应类型,无论响应中有多少分号,无论是纯文本还是一张 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 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;
理想情况下,我希望看到的是,如果我提出的问题在响应中有 3 个分号,它会输出一张英雄卡。如果超过 3 个,则为视频卡,如果两者都没有,则文本回复。
我不是 js 专家,但我对以下内容感到很困惑:
const qnaCard = qnaResults.includes(';');
在Javascript中,includes
如下(source):
The includes() method determines whether an array includes a certain value among its entries, returning true or false as appropriate.
所以这里你的 qnaCard
是 true
或 false
。但看起来您正在尝试使用它,就好像它包含文本一样:
if (qnaCard.toString().split(';').length < 3) {
...
您必须处理包含答案的对象:qnaResults[0].answer
。