botframework - 如何检测 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),但我不确定这是否是最佳实践。
解决方案
这不能完全做到,因为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
.
currentDialog
是undefined
如果还没有对话开始,所以currentDialog === undefined
或!currentDialog
允许初始和欢迎消息。拥有这三个独立的分支应该可以让您处理每种情况。
关于“最佳实践”,如果您希望在每次对话结束时获得反馈,我会说这是正确的方法。如果您不希望有任何反馈,最好在适当的对话框结束时调用您的 FeedbackDialog。
推荐阅读
- android - 为什么 ADB Over WIFI 无法在 Android 10 上运行
- jquery - 如何按类别分隔行中的特定项目,如果有一个项目具有不同的类别,则追加到新行中
- javascript - 角度路由,包括身份验证保护和重定向
- javascript - 防止重新渲染数组映射组件
- typescript - 如何将声明的条件推断类型与另一个类似的条件推断类型匹配
- c# - 检查两个具有半径的地理位置是否在彼此的半径内
- python - 如何在 unittest 测试期间添加计数器?
- python - 第三方包的 mock.patch() 的 Pytest 路径
- django - Django - 将二进制流转换为图像
- javascript - 使用钩子时如何为 panResponder 设置偏移量?