Node.js 无法将控制权传递给 botbuilder 对话框中的另一个方法

Unable to pass control to another method within a dialog in botbuilder for Node.js

我正在使用 Node.js 和 MS Bot Framework 创建我的第一个机器人,我正在尝试弄清楚如何在对话框中将控制权从一种方法传递到另一种方法。

在 Bot Framework for C# 中,非常简单:

context.Wait(NextMethodName);

其中 NextMethodName 是机器人收到下一条用户消息后 运行 的方法名称。

我正在尝试在 Node.js 中做类似的事情。我有两个功能。第一个提示用户输入内容或单击按钮,第二个应该处理用户的输入。我正在努力将控制权传递给第二个函数。

bot.dialog('subscribe', [
function (session) {
    var card = new builder.HeroCard(session)
        .title("Subscribe for reminders?")
        .text("It seems you're not enrolled yet. Subscribe for reminders to submit your work hours?")
        .buttons([
            builder.CardAction.imBack(session, "subscribe", "Subscribe")
        ]);

    var msg = new builder.Message(session).attachments([card]);

    session.send(msg);
    //next(); //compile error
},
function (session, results) {
    if (results.response === 'subscribe') {
        session.send('You are now subscribed to reminders through ' + session.message.user.channelId + '.');
    }
    else {
        session.send('You must subscribe to reminders before using this bot.');
    }
}
]);

如何在用户单击按钮或回答任何问题后实现第二个功能 运行?

在 node 的 botbuilder sdk 中,您可以定义瀑布对话框,其中包含所谓的 'steps'。每一步都会导致另一步(如瀑布)。根据文档:

'Waterfalls let you collect input from a user using a sequence of steps. A bot is always in a state of providing a user with information or asking a question and then waiting for input. In the Node version of Bot Builder it's waterfalls that drive this back-n-forth flow'.

一些步骤可以从询问用户信息的提示开始,然后接下来的步骤通过使用 dialogData 保存它来处理响应。然后您可以调用下一个函数参数以将控制传递给下一步。在你的情况下,调用 next() 会给你一个错误,因为该函数不在范围内,你必须将它作为参数提供给你的步骤函数。

在此处检查此示例:

https://github.com/Microsoft/BotBuilder-Samples/tree/master/Node/core-MultiDialogs

在你的第一步代码中我会这样做:

function (session,args,next) {
    var card = new builder.HeroCard(session)
        .title("Subscribe for reminders?")
        .text("It seems you're not enrolled yet. Subscribe for reminders to submit your work hours?")
        .buttons([
            builder.CardAction.imBack(session, "subscribe", "Subscribe")
        ]);

    var msg = new builder.Message(session).attachments([card]);

    session.send(msg);
    next();
}

但这只会引导你进入下一步,所以如果你想等待用户输入(带有文本提示),或者例如使用 HeroCard 操作,就像你在示例中定义的那样:

您的卡片通过按钮触发一个名为 "subscribe" 且参数为 "Subscribe" 的操作。将此视为通过按下卡片上的按钮在对话框中调用的函数。现在要定义该函数,我们这样做:

// An action is essentially a card calling a global dialog method
// with respective parameters. This  dialog action will route the action 
// command to a dialog.
bot.beginDialogAction('subscribe', '/subscribe');

// Create the dialog for the action...
bot.dialog('/subscribe', [
    function (session, args) {
       //do something!
    }
]);