【问题标题】:How to add a delay to bot responses, so it feels more real in Microsoft Bot Framework Node.js?如何为机器人响应添加延迟,以便在 Microsoft Bot Framework Node.js 中感觉更真实?
【发布时间】:2018-01-02 14:21:24
【问题描述】:

有没有办法给响应增加一点延迟?所以机器人感觉更真实,就像它在打字一样?只是一点点。 目前,测试人员的反应是太快了。 这很好,但是……感觉太“冷”了。稍有一点时间,看起来机器人正在打字,它会感觉更温暖和模糊::)

我需要在两行之间添加延迟

    session.send("Account created successfully");
    session.send("Please login");

下面是完整代码

var restify = require('restify');
var builder = require('botbuilder');
var botbuilder_azure = require("botbuilder-azure");

// Setup Restify Server
var server = restify.createServer();
server.listen(process.env.port || process.env.PORT || 3978, function () {
console.log('%s listening to %s', server.name, server.url); 
});

// Create chat connector for communicating with the Bot Framework Service
var connector = new builder.ChatConnector({
    appId: process.env.MicrosoftAppId,
    appPassword: process.env.MicrosoftAppPassword,
    openIdMetadata: process.env.BotOpenIdMetadata 
});

// Listen for messages from users 
server.post('/api/messages', connector.listen());

var bot = new builder.UniversalBot(connector);

bot.on('conversationUpdate', (message) => {
    if (message.membersAdded) {
        message.membersAdded.forEach(function (identity) {
            if (identity.id === message.address.bot.id) {
                bot.beginDialog(message.address, 'accountCheck');
            }
        });
    }
});

bot.dialog('accountCheck', [
    function (session, results, next) {

         session.send("Account created successfully");
         session.send("Please login");

    }
]).endConversationAction("stop",
    "",
    {
        matches: /^cancel$|^goodbye$|^exit|^stop|^close/i
        // confirmPrompt: "This will cancel your order. Are you sure?"
    }
);

【问题讨论】:

  • 使用session.sendTyping()。根据我的经验,人为的延迟也会导致用户认为存在性能问题。
  • 我需要 2 秒延迟。所以,这不是问题。 session.sendTyping() 对我不起作用

标签: node.js botframework azure-bot-service


【解决方案1】:

你可以使用session.delay()

bot.dialog('accountCheck', [
    function (session, results, next) {

         session.send("Account created successfully");
         // 0.5 sec delay between messages
         session.delay(500)
         session.send("Please login");

    }
]).endConversationAction("stop",
    "",
    {
        matches: /^cancel$|^goodbye$|^exit|^stop|^close/i
        // confirmPrompt: "This will cancel your order. Are you sure?"
    }
);

查看文档: https://docs.botframework.com/en-us/node/builder/chat-reference/classes/_botbuilder_d_.session.html#delay

【讨论】:

    【解决方案2】:

    机器人框架 SDK v4:

    await turnContext.sendActivity({ type: ActivityTypes.Typing })
    

    Documentation link SDK v4

    Bot Framework SDK v3:

    session.sendTyping()
    

    Documentation link SDK v3

    【讨论】:

      【解决方案3】:

      我使用 nodejs SDK 4.0 实现这一点的方式是在 SendActivities 中使用ActivityTypes.Typing,如下所示

      await context.sendActivities([
      {type: ActivityTypes.Typing},
      {type: 'delay', value:2000},
      {type: ActivityTypes.Message, text: 'Bot Response goes here is only text else send attachments as reply'}])
      

      尝试在Change SendActivity to SendActivities with dealy中添加以上代码 尝试在模拟器中测试。

      【讨论】:

        【解决方案4】:

        您可以在客户端或后端使用timeout。

        function (session, results, next) {
        
            session.send("Account created successfully. Wait 10 seconds...");
            setTimeout(function() {
                    session.send("Please login");
            }, 10000); //10 seconds
        }
        

        【讨论】:

        • @VigneswaranA 你能告诉我们你在 node.js 上的结构和代码吗?
        • @VigneswaranA 我已经改变了。可以试试吗?
        • @VigneswaranA 你可以申请吗?
        【解决方案5】:

        您可以发送一个打字指示器,而不是增加延迟

        session.sendTyping();
        

        更多信息https://docs.microsoft.com/en-us/bot-framework/nodejs/bot-builder-nodejs-send-typing-indicator

        【讨论】:

        • 不确定这意味着什么...打字指示器工作良好,它还取决于您接下来要执行的操作。正如我在 cmets 中指出并指出的那样,添加延迟不是一个好习惯
        • 请注意 session.sendTyping() 并不适用于每个频道。也许这就是为什么它不适用于@VigneswaranA
        猜你喜欢
        • 2023-03-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-10-25
        • 1970-01-01
        相关资源
        最近更新 更多