You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Microsoft Bot Framework Node.js中为机器人响应添加延迟以模拟真实交互

Adding Typing Delay to Bot Responses for More Natural Interaction

Absolutely! Adding a slight delay between your bot's messages is a perfect way to mimic human-like typing and make the conversation feel warmer, instead of the "instant robotic" vibe you're seeing right now. Here's how to implement this in your Bot Builder code:

Approach 1: Using async/await (Cleanest Modern Syntax)

We can convert the dialog function to an async function, then use a Promise-wrapped setTimeout to create the delay between messages:

bot.dialog('accountCheck', [
    async function (session, results, next) {
        // Send first success message
        await session.send("Account created successfully");
        
        // Add a 1-second delay (adjust the milliseconds value to your preference)
        await new Promise(resolve => setTimeout(resolve, 1000));
        
        // Send login prompt after the delay
        session.send("Please login");
    }
]).endConversationAction("stop", "", { 
    matches: /^cancel$|^goodbye$|^exit|^stop|^close/i
});

Approach 2: Using setTimeout (Traditional Callback Syntax)

If you prefer not to use async/await, you can wrap the second session.send inside a setTimeout directly:

bot.dialog('accountCheck', [
    function (session, results, next) {
        session.send("Account created successfully");
        
        // Delay the second message by 1 second
        setTimeout(() => {
            session.send("Please login");
        }, 1000); // Adjust this number (ms) to make the delay shorter/longer
    }
]).endConversationAction("stop", "", { 
    matches: /^cancel$|^goodbye$|^exit|^stop|^close/i
});

Key Notes:

  • Adjust the delay value: 800-1500 milliseconds (0.8-1.5 seconds) is usually a natural range for human-like typing delays.
  • Session context: Both approaches preserve the session object correctly, so the second message will be sent to the same conversation without issues.
  • Consistency: You can reuse this pattern across other dialogs in your bot to maintain a consistent, friendly interaction style.

内容的提问来源于stack exchange,提问作者Vigneswaran A

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 06:51:12