Ian O'Reilly
Ian O'Reilly

Reputation: 27

Bot Framework Scope beginDialogAction

I'm having some trouble with the beginDialogAction method for controlling conversation flow and would love some advice. I'm pretty new to JavaScript.

What I would like to do is scope the matching of a keyword to the context of my main dialog. My understanding is that the beginDialogAction method is the best way to do this. My problem is that once my dialog has been triggered with beginDialogAction, the new dialog is added to the stack, and beginDialogAction continues to listen for another match. This means that if my user happens to match on my trigger word farther in the flow, it will change the topic of conversation.

What I want is to clear to clear the dialog stack before starting the new dialog specified in beginDialogAction, but i have been unable to figure out how thus far. Any advice would be greatly appreciated!

Code:

var bot = new builder.UniversalBot(connector, function (session) {
    session.send("Hi, good to meet you! I'm here to help you request services. \n\n Please select an action from below.");
    session.beginDialog('main');
});
bot.dialog('main', [
    function(session){
    var msg = new builder.Message(session);
    msg.attachmentLayout(builder.AttachmentLayout.carousel)
    msg.attachments([
        new builder.HeroCard(session)
            .title("Request Design Services")
            .subtitle("I can send out a request for any design services you need, right from here!")
            .buttons([
                builder.CardAction.imBack(session, "I'd like to request design services.", "New Request")
            ]),
        new builder.HeroCard(session)
            .title("Project Finacials Request")
            .subtitle("I can pull finance information from any project within your organization.")
            .buttons([
                builder.CardAction.imBack(session, "I'd like to request financials.", "New Request")
            ])
    ]);
    session.send(msg)
}]).beginDialogAction('begindesign', 'design',{ matches: /design/i }).beginDialogAction('beginfinance', 'finance',{ matches: /financials/i });

bot.dialog('design', [
...
]);

bot.dialog('finance', [
...
]);

Upvotes: 2

Views: 73

Answers (1)

Gary Liu
Gary Liu

Reputation: 13918

You can leverage onSelectAction property for your requirement. You can refer to the source code for the description:

/** * (Optional) custom handler that's invoked whenever the action is triggered. This lets you * customize the behavior of an action. For instance you could clear the dialog stack before * the new dialog is started, changing the default behavior which is to just push the new * dialog onto the end of the stack. * * It's important to note that this is not a waterfall and you should call next() if you * would like the actions default behavior to run. */

And please consider the following code snippet:

bot.dialog('mainMenu', [
    (session, args, next) => {
        builder.Prompts.text(session, 'Hi there! What can I do for you today?', {
            retryPrompt: 'Hi there! What can I do for you today?'
        });
    },
    (session, results) => {
        session.endConversation('Goodbye!');
    }
])
.beginDialogAction('sportsAction', 'Sports', {
    matches: /^sports$/i,
})
.beginDialogAction('cookingAction', 'Cooking', {
    matches: /^cooking$/i,
    onSelectAction: (session, args, next) => {
        session.clearDialogStack();
        next();
    }
})
bot.dialog('Sports', [
    (session, args, next) => {
        session.send(`current dialog length: ${session.sessionState.callstack.length}`);
        session.endDialog('Sports here');
    }
]);
bot.dialog('Cooking', [
    (session, args, next) => {
        session.send(`current dialog length: ${session.sessionState.callstack.length}`);
        session.endDialog('Cooking here');
    }
])

Upvotes: 1

Related Questions