javascript - 机器人框架 : How to stop a ChoicePrompt from RePrompting

标签 javascript node.js botframework web-chat

我通过 React 开发了一个网页,其中 Webchat-Bot 和 React App 进行交互。 React 应用程序包含一个计时器,它应该停止在那一刻运行的任何对话框。它通过 Direct-Line 发送事件来执行此操作。 无论如何,我得到了不需要的行为,如果机器人逻辑位于 waterfall 中,其中步骤是(选择-)提示,则会重新提示问题。 (因为它没有收到用户所需的答案)。我的问题是:

如何停止 ChoicePrompt 重新提示?

当机器人收到“超时”事件时,它会使用 await endDialog() 停止当前对话框,并开始发送新消息的新对话框。下次用户输入后,会弹出之前的提示。所以我假设,后台的提示仍然等待答案之一,并且因为它没有收到答案,所以它会再次启动或重新提示。

我尝试将 maxRetries 设置为 0:

var options = {maxRetries: '0'};
await step.prompt(CONFIRM_PROMPT, `text abc..`, ['sure', 'not sure'], options);

弹出事件的代码片段..

async onTurn(turnContext) {
const dc = await this.dialogs.createContext(turnContext);
..
if (turnContext.activity.name === 'time-feedback') {
    ..
    await dc.endDialog(); // when a choice-prompt is active, it keeps reprompting, before the next line gets activated. I wish to end the reprompt and current (waterfall dialog) immediately
    await dc.beginDialog(PromptForStartingNew); // start a new waterfall dialog

}

最佳答案

replaceDialog函数正是您想要做的事情。它结束事件对话框,然后开始一个新对话框,但不会恢复堆栈上第二高的任何对话框。

This method is conceptually equivalent to calling endDialog() and then immediately calling beginDialog(). The difference is that the parent dialog is not resumed or otherwise notified that the dialog it started has been replaced.

我怀疑您遇到的一些问题是因为在开始新对话框之前为 waterfall 调用了 resumeDialog 。然而,对于我来说, waterfall 会停留在当前步骤并再次调用相同的选择提示是没有意义的。我期望发生的是 waterfall 继续进行下一步并启动新的提示,然后您的中断对话框将放在下一步提示之上的堆栈上。然后,当您的中断对话框结束时,下一步的提示将再次提示,而不是第一步的提示。如果不查看代码的其余部分,很难知道到底发生了什么,但如果我的解决方案适合您,这种区别可能无关紧要。

既然您已经说过要结束整个 waterfall 而不仅仅是提示,那么您需要的不仅仅是replaceDialog。您想要结束事件对话框,然后替换其下方的对话框,但前提是事件对话框是提示,下一个对话框是 waterfall 。你可以尝试这样的事情:

// Check if there are at least 2 dialogs on the stack
if (dc.stack.length > 1) {
    const activeInstance = dc.activeDialog;
    const parentInstance = dc.stack[dc.stack.length - 2];
    const activeDialog = dc.findDialog(activeInstance.id);
    const parentDialog = dc.findDialog(parentInstance.id);

    if (activeDialog instanceof Prompt && parentDialog instanceof WaterfallDialog)
    {
        // Calling endDialog is probably unnecessary for prompts
        // but it's good practice to do it anyway
        await activeDialog.endDialog(turnContext, activeInstance, DialogReason.endCalled);
        // Remove the prompt from the stack without resuming the waterfall
        dc.stack.pop();
    }
}

// Replace the waterfall (or any active dialog) with your interrupting dialog
await dc.replaceDialog(PromptForStartingNew);

关于javascript - 机器人框架 : How to stop a ChoicePrompt from RePrompting,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56759397/

相关文章:

c# - 在 Microsoft Bot Framework V3 中触发系统消息

javascript - 是否可以使用nodejs向特定Skype帐户发送消息?

javascript - 使用 x-editable 使不同的元素在单击时可编辑

javascript - Javascript 中 window.requestFileSystem 的数组范围

node.js - 如何使用 Node.JS 的 restify 框架解析/读取多个参数

javascript - 如何将 Node.js 流的内容读入字符串变量?

javascript - 异步队列,文件流结束如何知道两者何时完成

javascript - JAVASCRIPT中如何通过变量传递参数

javascript - 按类名和值过滤元素-jquery

botframework - 单击确认对话框中的后退按钮后,如何返回到上一个用户对话?