Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a delay to bot responses, so it feels more real in Microsoft Bot Framework Node.js?

Is there a way to add a bit of a delay to the responses? So the bot feels more real, like if it was typing? Just a little bit. Right now the reaction from testers has been that it’s too fast. Which is great, but… feels too “cold”. With a little time where it looks like the bot is typing, it would feel a bit more warm and fuzzy: :)

I need to add delay between two lines

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

Below is the full code

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?"
    }
);
like image 324
Vigneswaran A Avatar asked Jan 28 '23 22:01

Vigneswaran A


2 Answers

Bot framework SDK v4:

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

Documentation link SDK v4

Bot Framework SDK v3:

session.sendTyping()

Documentation link SDK v3

like image 32
Ron Avatar answered Jan 31 '23 11:01

Ron


You can use 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?"
    }
);

see the docs: https://docs.botframework.com/en-us/node/builder/chat-reference/classes/_botbuilder_d_.session.html#delay

like image 94
Amit be Avatar answered Jan 31 '23 12:01

Amit be