Vigneswaran A
Vigneswaran A

Reputation: 590

Starting a dialog from conversationUpdate event in Node.js BotBuilder

I want to show the message and call a dialog when chatbot initialize. The below code shows the message. But, can not call a dialog.

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);

Below is the code for dialog. I need to call below dialog when chatbot begins

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?"
});

I tried below methods. But it is not working

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

Upvotes: 3

Views: 953

Answers (2)

Vigneswaran A
Vigneswaran A

Reputation: 590

I fixed my problem by using single line code

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

Upvotes: 0

nwxdev
nwxdev

Reputation: 4635

You are hitting this error because, although they have the same method name, the method signatures differ between session.beginDialog() and <UniversalBot>bot.beginDialog().

This can be a bit confusing since the first argument to session.beginDialog() is the dialogId, but when using bot.beginDialog() the first argument is the address, and the second param is the dialogId.

To solve this, call bot.beginDialog() with the correct input parameters as described in the SDK reference documentation - Eg. bot.beginDialog(activity.address, dialogId);

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

You can also see the full method signature in the botbuilder.d TypeScript definition file here:

/** 
 * 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;

Upvotes: 2

Related Questions