beginAgain
beginAgain

Reputation: 211

Use Node-Schedule to begin a BotBuilder Dialog

I have working code for a BotBuilder dialog. I now want to have the dialog started at 8:30 am each Monday-Friday using node-schedule like below.

var rule = new schedule.RecurrenceRule();
// Run the task Mondays-Fridays
rule.dayOfWeek = [0, new schedule.Range(1, 5)];
rule.hour = 8;
rule.minute = 30;

schedule.scheduleJob(rule, beginStatusDialog);

console.log('Schedule initialzed.');

When running this "Schedule initialized" is written as expected. So, I have wrapped my dialog code in the function beginStatusDialog like below.

function beginStatusDialog() {


// Begin dialog - This is a question bot that uses a waterfall technique to prompt users for input.
const bot = new builder.UniversalBot(connector, [
    function (session) {
        session.send("Welcome to the daily status check " + session.message.user.name + ".");
        builder.Prompts.text(session, "What did you do yesterday?");
    },
    function (session, results) {
        session.dialogData.yesterday = session_yesterday = results.response;
        builder.Prompts.text(session, "What will you do today?");
    },
    function (session, results) {
        session.dialogData.today = session_today = results.response;
        builder.Prompts.text(session, "Are there any obstacles holding you up? Note: An email will be sent with your responses.");
    },
    function (session, results) {
        session.dialogData.obstacles = session_obstacles = results.response;

        session_username = session.message.user.name;

        // Write responses to DB
        executeStatement(session_username, session_yesterday, session_today, session_obstacles);

        //Process request and display details
        session.send(`Daily status details: <br/>Yesterday: ${session.dialogData.yesterday} <br/>Today: ${session.dialogData.today} <br/>Obstacles: ${session.dialogData.obstacles}`);
        session.dialogData = {};
        session.endDialog();

    }
]).set('storage', inMemoryStorage); // Register in-memory storage

}

When I run this in the botframework-emulator I receive the following error:

enter image description here

Is it wrong to wrap the dialog in a function? If so what is the correct way for the scheduler to call the dialog? Anyone else have experience with this particular scenario?

Any help/pointers will be greatly appreciated. :)

Thank you.

Edit:

Gary Liu's comment got me to thinking. So I instantiated the bot ouside of the function like the below and it no longer throws an error but it doesn't do anything at the scheduled time.

var bot = new builder.UniversalBot(connector).set('storage', inMemoryStorage);

Then I start it up inside the function with bot.dialog - or at least that is my intention:

function beginStatusDialog() {
// Begin dialog - This is a question bot that uses a waterfall technique to prompt users for input.
//const bot = new builder.UniversalBot(connector, [
bot.dialog([
    function (session) {
        session.send("Welcome to the daily status check " + session.message.user.name + ".");
        builder.Prompts.text(session, "What did you do yesterday?");

Anyway I am looking at this further.

As always any help/pointers will be appreciated - thank you.

Upvotes: 4

Views: 519

Answers (1)

Steven Kanberg
Steven Kanberg

Reputation: 6393

I was able to get the scheduler to work by placing it in conversationUpdate. Following the example for Reccurance Rule Scheduling, I placed the scheduleJob in an anonymous function which then called bot.beginDialog(). beginDialog creates the dialog stack and begins the dialog flow. Passing message.address is necessary as it assigns the current user information to the stack.

In my test I had it running every minute (see the log timestamps).

I tried creating this using a named function in place of the anonymous function to call bot.beginDialog() - the scheduler doesn't seem to like this (within the context of starting up a bot, at least). It doesn't respond.

Hope of help!

var rule = new schedule.RecurrenceRule();
// Run the task Mondays-Fridays
rule.dayOfWeek = [0, new schedule.Range(1, 5)];
rule.hour = 13;
// rule.minute = 08;

// schedule.scheduleJob(rule, beginStatusDialog);

console.log('Schedule initialzed.');

var bot = new builder.UniversalBot(connector);
bot.set('storage', new builder.MemoryBotStorage());

bot.on('conversationUpdate', function (message) {
    if (message.membersAdded) {
        message.membersAdded.forEach(function (identity) {
            if (identity.id === message.address.bot.id) {
                schedule.scheduleJob(rule, function () {
                    bot.beginDialog(message.address, '/');
                })
            }
        });
    }
});

bot.dialog('/', [
    function (session) {
        builder.Prompts.text(session, "What is your name?");
    },
    function (session) {
    session.send("You said %s", session.message.text);
    }
]);

enter image description here

Upvotes: 3

Related Questions