【问题标题】:How to close TextPrompt after a certain time if the enduser doesn't reply如果最终用户没有回复,如何在一段时间后关闭 TextPrompt
【发布时间】:2019-06-13 10:48:43
【问题描述】:

在 Microsoft 聊天机器人 SDK v4 中,我想知道如何以编程方式关闭提示“任何类型的 TextPrompt、ConfirmPrompt ... 等”,如果用户没有回复,则在一段时间后结束对话框。

我已经尝试使用 settimeout 并使用 sc.endDialog 但它不起作用并给了我

private promptForNameStep = async (step: WaterfallStepContext<UserProfile>) => {

setTimeout(async () => {
   console.log('I am here!');
   await step.endDialog();
}, 5000);

        const userProfile = await this.userProfileAccessor.get(step.context);

        if (userProfile.name === undefined) {
            // prompt for name, if missing
            return await step.prompt(NAME_PROMPT, i18n.__('salutation.your_name'));
        }
        return await step.next();
    }
(node:21084) UnhandledPromiseRejectionWarning: TypeError: Cannot perform 'get' on a proxy that has been revoked
    at UserState.load (/Users/macbook/Workspace/bot/chatbot/templates/Enterprise-Template/src/typescript/enterprise-bot/node_modules/botbuilder-core/src/botState.ts:84:48)
    at BotStatePropertyAccessor.get (/Users/macbook/Workspace/bot/chatbot/templates/Enterprise-Template/src/typescript/enterprise-bot/node_modules/botbuilder-core/src/botStatePropertyAccessor.ts:97:43)
    at SkillDialog.getStateFromAccessor (/Users/macbook/Workspace/bot/chatbot/templates/Enterprise-Template/src/typescript/enterprise-bot/src/dialogs/skill/skillDialog.ts:164:68)
    at SkillDialog.executeStep (/Users/macbook/Workspace/bot/chatbot/templates/Enterprise-Template/src/typescript/enterprise-bot/src/dialogs/skill/skillDialog.ts:91:47)
    at Array.stepsMethods.push (/Users/macbook/Workspace/bot/chatbot/templates/Enterprise-Template/src/typescript/enterprise-bot/src/dialogs/skill/skillDialog.ts:149:29)
    at WaterfallDialog.onStep (/Users/macbook/Workspace/bot/chatbot/templates/Enterprise-Template/src/typescript/enterprise-bot/node_modules/botbuilder-dialogs/src/waterfallDialog.ts:198:44)
    at WaterfallDialog.runStep (/Users/macbook/Workspace/bot/chatbot/templates/Enterprise-Template/src/typescript/enterprise-bot/node_modules/botbuilder-dialogs/src/waterfallDialog.ts:225:31)
    at WaterfallDialog.resumeDialog (/Users/macbook/Workspace/bot/chatbot/templates/Enterprise-Template/src/typescript/enterprise-bot/node_modules/botbuilder-dialogs/src/waterfallDialog.ts:166:27)
    at WaterfallStepContext.endDialog (/Users/macbook/Workspace/bot/chatbot/templates/Enterprise-Template/src/typescript/enterprise-bot/node_modules/botbuilder-dialogs/src/dialogContext.ts:269:33)
    at <anonymous>
(node:21084) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 4)
(node:21084) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

我希望对话框关闭,但出现上述错误。

【问题讨论】:

    标签: javascript node.js typescript botframework


    【解决方案1】:

    使用超时结束对话不是一个好习惯,因为当您向外扩展时,消息可能会被路由到机器人的不同实例,并且超时不会被正确取消。更好的方法是在 userState 中保存用户上次向机器人发送消息的时间,并在下次用户向机器人发送消息时在响应之前检查时间差。看看下面的代码sn-ps。

    const TIMEOUT = 5000;
    
    ...
    
     // Prompts
      async promptForName(step) {
        this.profileAccessor.set(step.context, { lastMessage: new Date() });    
        return await step.prompt(NAME_PROMPT, "What is your name?");
      }
    
      async captureName(step) {
        const profile = await this.profileAccessor.get(step.context);
        if (new Date().getTime() - new Date(profile.lastMessage).getTime() < TIMEOUT) {
          profile.name = step.result;
          profile.lastMessage = new Date();
          this.profileAccessor.set(step.context, profile);
    
          await this.userState.saveChanges(step.context);
    
          return await step.next();
        } else {
          await step.context.sendActivity("Sorry, you took too long to respond");
          return await step.endDialog();
        }
      }
    

    希望这会有所帮助!

    【讨论】:

    • 这是一个好主意,以防我只依赖最终用户的输入来响应,因此它将进入下一步,但在其他一些情况下,用户永远不会回复并且我想要 - 例如 -在对话事件结束时更新一些报告或将数据发送到分析工具,所以我想找到一种以编程方式关闭它的方法。
    • 你有没有给我评论@tdurnford
    • 看看这个question。我是带着这个来回答的。
    猜你喜欢
    • 1970-01-01
    • 2015-02-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-24
    • 2011-05-10
    • 1970-01-01
    相关资源
    最近更新 更多