您是否需要为Microsoft Bot Framework中的每个“ beginDialog”调用“ endDialog”?什么时候应该调用“ endDialog”?

编辑:

在查看源代码时,我在botbuilder.d.ts中发现了以下内容。似乎在瀑布中您不需要显式调用endDialog

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

我正在学习MS Bot Framework版本3-这是他们在这里使用的版本。

我遵循瀑布工作原理(https://docs.microsoft.com/en-us/azure/bot-service/nodejs/bot-builder-nodejs-dialog-manage-conversation-flow?view=azure-bot-service-3.0)的概念,但是我不明白的是endDialog扮演什么角色。

例如,在我们使用的代码中,有一堆单独的对话框,它们的格式均为

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

基本上,一个对话框正在加载另一个对话框(之间还有其他一些代码,例如在数据存储区中设置数据)。 endDialog无处可调用。但是在MS教程的示例中,每个瀑布都以某种形式的endDialogendDialogendDialogWithResults等结尾)。

使用beginDialog“打开”的每个对话框在完成瀑布之后,即运行在数组中传递给bot.dialog的函数时是否自动“关闭”自身? (在上面的代码中,瀑布只是一步)。

何时需要明确呼叫endDialog

感谢您的帮助!

bendan0663 回答:您是否需要为Microsoft Bot Framework中的每个“ beginDialog”调用“ endDialog”?什么时候应该调用“ endDialog”?

嘿,我本人对此很纳闷,并从MS那里找到了它

  

使用瀑布创建的对话框必须明确结束,否则机器人将无限期重复瀑布。您可以使用以下方法之一结束瀑布:

     

session.endDialog:如果没有数据可返回到调用对话框,请使用此方法结束瀑布。

     

session.endDialogWithResult:如果有数据可返回到调用对话框,请使用此方法结束瀑布。返回的响应参数可以是JSON对象或任何JavaScript基本数据类型。

Bot Framework Waterfall End Dialog

如果要结束所有对话框并停止对话,可以将endConversationAction触发器与可选消息/对象一起使用

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

确实,文档应该说您 应该 结束对话框,而不是您 必须 。如果您未明确调用EndDialog,瀑布将自动结束。这意味着更多地是内置的安全机制,以帮助避免用户卡住,在获得结果后忘记致电enddialog。但是在某些情况下,不添加显式EndDialog调用是可以的。您可以在瀑布here中找到更多内容。

一个有效的用例是对话框调用是一个瀑布,它仅决定要跳转到哪个对话框。一旦分支到给定的对话框,就没有理由添加在该对话框结束之后结束的步骤。

但是,即使在那种情况下,使用ReplaceDialog代替您自己的分支对话框也会更有效。

本文链接:https://www.f2er.com/3168062.html

大家都在问