如何在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
sessionobject 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
相关产品推荐
相关产品推荐

