How to detect end of dialog in Botframework v4?

故事扮演 提交于 2020-06-01 07:49:30

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/59955280/how-to-detect-end-of-dialog-in-botframework-v4

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!