1

This is my first try with Bot Framework (Nodejs). I want to test delayed messages, for example, my bot must answer after a 5 seconds after receiving the message. So I tried with this code:

var builder = require('botbuilder');

var connector = new builder.consoleconnector().listen();
var bot = new builder.universalbot(connector);

bot.dialog('/', function (session) {
    if (!session.userData.TimeoutStarted) {
        session.send("I'll answer in 5 seconds");
        session.userData.TimeoutStarted = true;

        setTimeout(function() {
            session.send("Answer after 5 seconds");
            session.userData.TimeoutStarted = false;
        }, 5000);
    } else {
        session.send("Bot is busy");
    }
});

But this doesn't work. Callback function inside setTimeout fires, but all operations with session doesn't work at all.

So, I find possible solution here: How to send message later in bot framework and rewrite my code:

var builder = require('botbuilder');

var connector = new builder.ConsoleConnector().listen();
var bot = new builder.UniversalBot(connector);

bot.dialog('/', function (session) {
    if (session.userData.Timeout > 0 && Date.now() - session.userData.Timeout > 5000)
        session.userData.Timeout = 0;

    if (!session.userData.Timeout) {
        session.send("I'll answer in 5 seconds");

        var reply = session.message;

        setTimeout(function() {
           reply.text = "Answer after 5 seconds";
           bot.send(reply);
        }, 5000);

        session.userData.Timeout = Date.now();
    } else {
        session.send("Bot is busy");
    }
});

This code works, but looks terrible with so many checks. So I have a few questions:

  1. Why first code example doesn't work? I guess problem in the session lifetime and then what is session lifetime?
  2. How to set session.userData in this examples? So In first code example I want to set it inside callback function inside setTimeout but it doesn't work too.
  3. What is the best way to create delayed answers?

2 Answers 2

2

I just investigated this issue. Looks like there's a bug in ConsoleConnector that makes it impossible to send two messages using the same session object (above a given interval between messages, due to internal batching). As the state is also persisted during send, your delayed state update will also not work. If you added a call to session.save to your callback in setTimeout, it would persist the new state (but would still not send the message).

I believe your first example should work with ChatConnector (haven't had the chance to try though). I'll create a pull request with the fix to ConsoleConnector.

I hope this answers all your questions.

UPDATE

See this issue and the related pull request for more details.

UPDATE2

It works for me with ChatConnector, using this code:

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

var connector = new builder.ChatConnector({
    appId: '',
    appPassword: ''
});

var bot = new builder.UniversalBot(connector);

server.post('/api/messages', connector.listen());


bot.dialog('/', function (session) {
    if (!session.userData.TimeoutStarted) {
        session.send("I'll answer in 5 seconds");
        session.userData.TimeoutStarted = true;

        setTimeout(function() {
            session.send("Answer after 5 seconds");
            session.userData.TimeoutStarted = false;
        }, 5000);
    } else {
        session.send("Bot is busy");
    }
});
3
  • Thank you for your participating! I test it with ChatConnector and here is the same problem: it neither sends messages nor saves state inside the callback. But session could work with more then one send. So, for example, if I just receive the message and try sequence: session.send("a"); session.send("b"); - it works;
    – Ceridan
    Commented Aug 18, 2016 at 6:29
  • Are you sure? It works with me using ChatConnector, see my update. For the sequencing: I was not precise, but you can see the explanation in the GitHub issue. Basically, the session object does batching by default, i.e. it waits 250ms for additional messages before sending the current ones. That's why it works with your sequencing but it doesn't work if you wait 5000ms after the first message.
    – thegaram
    Commented Aug 18, 2016 at 18:02
  • Sorry for late answer. I test it again and you are absolutely right. It really works with ChatConnector. Thank you for your help!
    – Ceridan
    Commented Aug 23, 2016 at 7:27
2

For people who are interested in sending a delayed message from bot, you could use session.delay(<ms>)

For example,

session.send('msg')
session.delay(5000) // delay 5 seconds 
session.endDialog()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.