gpsugy
gpsugy

Reputation: 1279

How to Prevent Both beginDialogAction and triggerAction from Executing in Microsoft Bot Builder SDK

Note: I am using Microsoft Bot Builder's SDK in Node.js.

The Desired Task

I am attempting to use beginDialogAction to initiate another sub-dialog so that by the end of the sub-dialog dialog, the stack would go right back to the dialog they left off. At the same time, however, I want to give the user the option of activating this same sub-dialog as a triggerAction in any other conversation.

The Problem

Implementing as follows puts TWO COPIES of the sub-dialog in the stack, by both the triggerAction and beginDialogAction. This is unwanted behavior because once one of those dialogs complete, a duplicate of that dialog runs again.

Here is the code:

// user dialog
bot.dialog('/', [
    function (session, args, next) {
       session.send('Starting root dialog');
    },
    function (session) {
        session.send('Ending root dialog.');
]).beginDialogAction('addUser', 'UserAdd', {
    matches: /^user add$/i
});

// branch dialog
bot.dialog('UserAdd', [
    function (session, args, next) {
        session.send('Adding user.');
}
]).triggerAction({
    matches: /^user add$/i
})

The Question

What is the correct way to enable both a beginDialogAction and triggerAction of a dialog, but only run that dialog ONCE if it is triggered by a beginDialogAction, so that the root dialog can continue where it left off? If this is not traditional way of thinking to use this framework, I welcome other perspectives.

Upvotes: 0

Views: 243

Answers (1)

Grace Feng
Grace Feng

Reputation: 16652

This is unwanted behavior because once one of those dialogs complete, a duplicate of that dialog runs again.

I couldn't reproduce this issue. The problem in your code is that you didn't end the UserAdd dialog after it is triggered whether it is called by beginDialogAction or triggerAction, this makes the dialog stack always stays inside of this UserAdd dialog and never end.

Based on your description, I don't think there would be any conflicts between beginDialogAction and triggerAction, tried to modify your code as below and it works fine on my side:

bot.dialog('/', [
    function (session, args, next) {
       session.send('Starting root dialog');
       builder.Prompts.text(session, "How many people are in your party?");
    },
    function (session, results) {
       builder.Prompts.text(session, "Who will be there?");
    },
    function (session, results) {
        session.send('Ending root dialog.');
        session.endDialog();
    }
])
.beginDialogAction('addUser', 'UserAdd', {
    matches: /^user add$/i
})

//  // branch dialog
bot.dialog('UserAdd', [
    function (session, args, next) {
        session.send('Adding user.');
        session.endDialog();
}
]).triggerAction({
    matches: /^user add$/i
});

If there's any further concern or more problem, please feel free to leave a comment.

Upvotes: 1

Related Questions