如何在Botframework v4中检测对话框的结尾? [英] How to detect end of dialog in Botframework v4?

查看:75
本文介绍了如何在Botframework v4中检测对话框的结尾?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在系统中任何其他对话框完成后启动一个反馈对话框.我找到了此答案它说要使用onEndDialog,但这不是ActivityHandler中的有效函数,只有onDialog.我的主对话框"在bot.js中扩展ActivityHandler,这就是为什么我不扩展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),但是我不确定这是最佳实践.

解决方案

此操作无法完全完成,因为endDialog不会冒泡到ActivityHandler可以访问的任何内容(据我所知)./p>

但是,要解决该问题,您非常亲密!将其更改为以下内容:

 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设置为{}(如果不存在),这是事实,因此我们需要使用currentDialog.dialogStack.length > 0检查dialogStack中是否有任何内容.

如果尚未启动任何对话框,则

currentDialogundefined,因此currentDialog === undefined!currentDialog允许输入初始消息和欢迎消息.拥有这三个独立的分支机构应该可以让您处理每种情况.


关于最佳做法",如果您想在每个对话框的末尾提供反馈,那么我说这是正确的方法.如果您不希望有任何反馈,则最好在相应对话框的末尾调用您的FeedbackDialog.

I'm trying to kick off a feedback dialog following the completion of any other dialog in the system. I found this answer which says to use onEndDialog, but that isn't a valid function in ActivityHandler, only onDialog. My "main dialog" is in bot.js extending ActivityHandler, which is why I am not extending ComponentDialog. Given how this is set up, is there any way to determine when a dialog has ended? I tried to check the dialog stack in onDialog, but it reads as no dialog for the welcome message and initial message from the user, and after that always reads as dialog running. Is there a way I can modify my function/bot handler to detect an end of dialog event? Here is the onDialog function I've tried.

        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();
        });

I have considered adding an extra step to the end of every dialog to call the feedback dialog (likely via replaceDialog), but I'm not sure that would be a best practice.

解决方案

This can't exactly be done, since endDialog doesn't bubble up to anything accessible from ActivityHandler (as far as I know).

But for a workaround, you're so close! Change it to something like this:

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();
});

Yours wasn't quite working only because currentDialog gets set to {} if it doesn't exist, which is truthy, so we need to check if there's anything in the dialogStack with currentDialog.dialogStack.length > 0.

currentDialog is undefined if no dialog has started yet, so currentDialog === undefined or !currentDialog allows for initial and welcome messages. Having those three separate branches should allow you to handle each situation.


Regarding "best practices", I'd say this is the right approach if you want feedback at the end of every dialog. If there are any in which you don't want feedback, it might be best to call your FeedbackDialog at the end of the appropriate dialogs.

这篇关于如何在Botframework v4中检测对话框的结尾?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆