【问题标题】:Connecting a Dialog with Luis in Microsoft BotFramework v4 node.js在 Microsoft BotFramework v4 node.js 中使用 Luis 连接对话框
【发布时间】:2019-10-18 09:05:28
【问题描述】:

以前我在 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;

【问题讨论】:

    标签: node.js azure botframework chatbot azure-language-understanding


    【解决方案1】:

    beginDialog 存在于DialogContext,但不存在于TurnContext。你可以使用Dialog.run(context, dialogState)ActivityHandler.onMessage 开始一个新的对话。

    所以您需要实例化您的DatavisualisatieDialog,然后从onMessage 中调用run

    此示例具有完整的 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 文件的顶部,并为类型错误获得红色曲线,它可能会在此处捕获。)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多