[以前,我在node.js中使用BotFramework V3进行了大量工作,但是BotFramework V4却大不相同,看来我做对了。
在识别Luis意图后,我一直在为对话而奋斗几天。
我收到以下错误:
[onTurnError]: TypeError: context.beginDialog is not a function
我已经在Stackoverflow上检查过类似的问题,但尚无明确答案。
请在下面查看我的机器人的示例:
const { ActivityHandler } = require('botbuilder');
const { LuisRecognizer } = require('botbuilder-ai');
const WelcomeCard = require('./resources/welcomeCard.json');
const { CardFactory, ActionTypes, ActivityTypes } = require('botbuilder-core');
const {DatavisualisatieDialog} = require('./Dialogs/DatavisualisatieDialog');
class TestBot extends ActivityHandler {
constructor(luisRecognizer) {
super('TestBot');
this.luisRecognizer = luisRecognizer;
this.onMessage(async (context, next) => {
const luisResult = await this.luisRecognizer.executeLuisQuery(context);
switch (LuisRecognizer.topIntent(luisResult)) {
case 'Datavisualisatie': {
await context.beginDialog(DatavisualisatieDialog);
}
break;
default:
// Catch all for unhandled intents
const didntUnderstandMessageText = `text`;
await context.sendActivity(didntUnderstandMessageText, didntUnderstandMessageText);
}
});
this.onMembersAdded(async (context, next) => {
const membersAdded = context.activity.membersAdded;
for (let cnt = 0; cnt < membersAdded.length; ++cnt) {
if (membersAdded[cnt].id !== context.activity.recipient.id) {
// activation of the welcomecard
const welcomeCard = CardFactory.adaptiveCard(WelcomeCard);
await context.sendActivity({ attachments: [welcomeCard] });
await context.sendActivity('text');
}
}
// By calling next() you ensure that the next BotHandler is run.
await next();
});
}
}
module.exports.TestBot = TestBot;
beginDialog
存在于DialogContext
,但不存在TurnContext
。您可以使用Dialog.run(context, dialogState)
从ActivityHandler.onMessage
开始新对话框。
因此,您需要实例化DatavisualisatieDialog
,然后从run
中调用onMessage
。
此示例的完整e2e示例正在运行:https://github.com/microsoft/BotBuilder-Samples/blob/master/samples/javascript_nodejs/19.custom-dialogs/bots/dialogBot.js#L24-L32
(如果使用的是VS Code,则可以将// @ts-check
添加到JS文件的顶部,并获得红色字型错误,可能会在这里捕获。)