首页 > 解决方案 > 如何检测 Botframework v4 中的对话结束?

问题描述

在系统中的任何其他对话完成后,我正在尝试启动反馈对话。我发现这个答案说要使用onEndDialog,但这不是 ActivityHandler 中的有效函数,只有onDialog. 我的“主对话框”在扩展 ActivityHandler 的 bot.js 中,这就是我不扩展 ComponentDialog 的原因。鉴于这是如何设置的,有没有办法确定对话何时结束?我试图在 中检查对话框堆栈onDialog,但它显示为没有欢迎消息和来自用户的初始消息的对话框,之后始终显示为对话框正在运行。有没有办法可以修改我的函数/机器人处理程序以检测对话结束事件?这是我尝试过的 onDialog 函数。

        this.onDialog(async (context, next) => {
            const currentDialog = await this.dialogState.get(context, {});
            if (currentDialog.dialogStack) {
                console.log('Dialog is running');
            } else {
                console.log('Dialog is not running');
            }

            // By calling next() you ensure that the next BotHandler is run.
            await next();
        });

我已经考虑在每个对话框的末尾添加一个额外的步骤来调用反馈对话框(可能通过 replaceDialog),但我不确定这是否是最佳实践。

标签: botframework

解决方案


这不能完全做到,因为endDialog不会冒泡到任何可访问的东西ActivityHandler(据我所知)。

但对于一种解决方法,你是如此接近!把它改成这样:

this.onDialog(async (context, next) => {
    const currentDialog = await this.dialogState.get(context);
    if (currentDialog === undefined) {
        console.log('No dialogs started');
    } else if (currentDialog.dialogStack && currentDialog.dialogStack.length > 0) {
        console.log('Dialog is running');
    } else {
        console.log('Dialog is not running');
    }

    // By calling next() you ensure that the next BotHandler is run.
    await next();
});

你的工作不太好,只是因为如果它不存在currentDialog就设置为{},这是真的,所以我们需要检查 dialogStack 中是否有任何currentDialog.dialogStack.length > 0.

currentDialogundefined如果还没有对话开始,所以currentDialog === undefined!currentDialog允许初始和欢迎消息。拥有这三个独立的分支应该可以让您处理每种情况。


关于“最佳实践”,如果您希望在每次对话结束时获得反馈,我会说这是正确的方法。如果您不希望有任何反馈,最好在适当的对话框结束时调用您的 FeedbackDialog。


推荐阅读