0
votes

EDIT:

In going through the source code, I found the following in botbuilder.d.ts. It would seem that in waterfalls you don't need to call endDialog explicitly?

/* You can terminate a waterfall early by either falling through every step of the waterfall using
 * calls to `skip()` or simply not starting another prompt or dialog.
 *
 * __note:__ Waterfalls have a hidden last step which will automatically end the current dialog if
 * if you call a prompt or dialog from the last step. This is useful where you have a deep stack of
 * dialogs and want a call to [session.endDialog()](/en-us/node/builder/chat-reference/classes/_botbuilder_d_.session.html#enddialog)
 * from the last child on the stack to end the entire stack. The close of the last child will trigger
 * all of its parents to move to this hidden step which will cascade the close all the way up the stack.
 * This is typically a desired behavior but if you want to avoid it or stop it somewhere in the
 * middle you'll need to add a step to the end of your waterfall that either does nothing or calls
 * something like [session.send()](/en-us/node/builder/chat-reference/classes/_botbuilder_d_.session.html#send)
 * which isn't going to advance the waterfall forward.
 * @example
 * <pre><code>
 * var bot = new builder.BotConnectorBot();
 * bot.add('/', [
 *     function (session) {
 *         builder.Prompts.text(session, "Hi! What's your name?");
 *     },
 *     function (session, results) {
 *         if (results && results.response) {
 *             // User answered question.
 *             session.send("Hello %s.", results.response);
 *         } else {
 *             // User said never mind.
 *             session.send("OK. Goodbye.");
 *         }
 *     }
 * ]);
 * </code></pre>
 */

I'm learning the MS Bot Framework version 3 -- that's the version they're working with here.

I follow the concept of how waterfalls work (https://docs.microsoft.com/en-us/azure/bot-service/nodejs/bot-builder-nodejs-dialog-manage-conversation-flow?view=azure-bot-service-3.0), but one thing I don't understand is what role endDialog plays.

For instance, in the code we are working with, there are a bunch of separate dialogs which all have the form

module.exports = function showTickets() {
    this.bot.dialog('/showAllTickets', [
        async function showAllTicketsFn(session, args, next) {
            this.beginDialog.bind(this, '/showTickets')(session, args, next);
        }.bind(this)
    ]);
};

Basically one dialog loading another (with some other code in between, like setting data in the data store). Nowhere is endDialog called. But in the example from the MS tutorial, every waterfall ends with some form of endDialog (endDialog or endDialogWithResults, etc).

Does every dialog 'opened' with beginDialog automatically 'close' itself when its waterfall is done, that is, when the functions that are passed in the array to bot.dialog are run through? (in the code above, the waterfall is only one step).

When does one need to call endDialog explicitly?

Thanks for any help!

2

2 Answers

1
votes

Hey was wondering this myself and found this from MS

A dialog that is created using a waterfall must be explicitly ended, otherwise the bot will repeat the waterfall indefinitely. You can end a waterfall by using one of the following methods:

session.endDialog: Use this method to end the waterfall if there is no data to return to the calling dialog.

session.endDialogWithResult: Use this method to end the waterfall if there is data to return to the calling dialog. The response argument that is returned can be a JSON object or any JavaScript primitive data type.

Bot Framework Waterfall End Dialog

if you want to end all the dialogs and stop the conversation you can use the endConversationAction trigger with an optional message/object

this.bot.dialog('/showAllTickets', [
    async function showAllTicketsFn(session, args, next) {
        this.beginDialog.bind(this, '/showTickets')(session, args, next);
        session.endConversation();
    }.bind(this)
]);
1
votes

Really, the documentation should say that you should end the dialog, not that you must. Waterfalls will end automatically if you don't call EndDialog explicitly. It's meant to be more of a built-in safety mechanism, to help avoid a user get stuck, forgetting to call enddialog after getting result. But there are cases where it is ok not add an explicit EndDialog call. You can find more in waterfall here.

One valid use case is where a dialog call's a waterfall that simply decides which dialog it wants to branch to. Once its branched to a given dialog there's no reason to add a step which ends after that dialog ends.

But likely, even in that case, it's more efficient to just use ReplaceDialog to replace yourself with the dialog you're branching to.