从Node.js中的conversationUpdate事件启动一个对话框BotBuilder

时间:2017-12-28 12:46:01

标签: node.js botframework

我想在chatbot初始化时显示消息并调用对话框。以下代码显示了该消息。但是,不能打电话给对话。

bot.on('conversationUpdate', function (activity) {
// when user joins conversation, send welcome message
if (activity.membersAdded) {
    activity.membersAdded.forEach(function (identity) {
        if (identity.id === activity.address.bot.id) {
            var reply = new builder.Message()
                .address(activity.address)
                .text("Hi, Welcome ");
            bot.send(reply);
            // bot.beginDialog("initialize", '/');
            // session.beginDialog("initialize");
        }
    });
}});bot.dialog('/', intents);

以下是对话框的代码。当chatbot开始时,我需要在下面的对话框中调用

bot.dialog('initialize', [
function (session, args, next) {
  builder.Prompts.choice(session, "Do you have account?", "Yes|No", { listStyle: builder.ListStyle.button });
}, function (session, args, next) {
    if (args.response.entity.toLowerCase() === 'yes') {
        //session.beginDialog("lousyspeed");
        session.send("No pressed");
    } else if (args.response.entity.toLowerCase() === 'no') {
        session.send("Yes pressed");
        session.endConversation();
    }
}]).endConversationAction("stop",
"",
{
    matches: /^cancel$|^goodbye$|^exit|^stop|^close/i
    // confirmPrompt: "This will cancel your order. Are you sure?"
});

我尝试了以下方法。但它不起作用

        1. bot.beginDialog("initialize", '/');
        2. session.beginDialog("initialize");

2 个答案:

答案 0 :(得分:2)

您遇到此错误,因为虽然它们具有相同的 name 方法,但方法签名在session.beginDialog()<UniversalBot>bot.beginDialog()之间有所不同。

这可能有点令人困惑,因为session.beginDialog()的第一个参数是dialogId,但是当使用bot.beginDialog()时,第一个参数是address,第二个参数是第二个参数是dialogId

要解决此问题,请使用SDK参考文档中所述的正确输入参数调用bot.beginDialog() - 例如。 bot.beginDialog(activity.address, dialogId);

https://docs.botframework.com/en-us/node/builder/chat-reference/classes/_botbuilder_d_.universalbot.html#begindialog

您还可以在botbuilder.d TypeScript definition file此处查看完整的方法签名:

/** 
 * Proactively starts a new dialog with the user. Any current conversation between the bot and user will be replaced with a new dialog stack.
 * @param address Address of the user to start a new conversation with. This should be saved during a previous conversation with the user. Any existing conversation or dialog will be immediately terminated.
 * @param dialogId ID of the dialog to begin.
 * @param dialogArgs (Optional) arguments to pass to dialog.
 * @param done (Optional) function to invoke once the operation is completed. 
 */
beginDialog(address: IAddress, dialogId: string, dialogArgs?: any, done?: (err: Error) => void): void;

答案 1 :(得分:0)

我通过使用单行代码修复了我的问题

bot.beginDialog(activity.address, 'initialize');