4

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.

beginAgain
  • 211
  • 4
  • 17
  • It looks a little odd from your code. It seems that, when your scheduler triggerred, you create a bot application via botbuilder, then you manually start a conversation with your bot? Is this your requirement? Or you just need to restrict the bot accessible time range – Gary Liu May 09 '18 at 01:34
  • Thank you for responding. No - I might have done it incorrectly and you got me thinking. What I want to happen is when the schedule code kicks off at 8:30 am the bot sends the message Welcome to the daily status check ... and the 3 questions. I don't want the user to manually start a conversation. Based on what you said I will look more closely at the way dialogs work. Do you have any suggestions? Anything would be greatly appreciated. – beginAgain May 09 '18 at 03:16
  • Please consider to send proactive messages, refer to https://learn.microsoft.com/en-us/azure/bot-service/nodejs/bot-builder-nodejs-proactive-messages?view=azure-bot-service-3.0, or answer https://stackoverflow.com/questions/49176318/bot-framework-node-js-ad-hoc-message-to-a-specific-user/49251924#49251924 – Gary Liu May 09 '18 at 07:20
  • Ty - Looking in to proactive messages now... – beginAgain May 10 '18 at 17:44
  • I was looking at this the other day after you mentioned it and I saw this - "Don't send proactive messages to users who have not previously interacted with the bot or solicited contact with the bot through another means such as e-mail or SMS." I take this to mean that it won't work in my particular scenario. The bot will be sitting there "inactive" until 8:30 am, at which time it pops up with the 1st question. Am I Looking at this incorrectly? – beginAgain May 10 '18 at 17:50
  • Actually, Bot applications do not have the "inactive" status, because it's based on normal web applications, and which will not spontaneously contact user, unless with proactive messages. And that sentence you have concern, i think it because you don't have the address if user never interact with the bot before. – Gary Liu May 11 '18 at 01:32
  • Ok I will look at proactive messages even closer - again :). – beginAgain May 11 '18 at 01:57

1 Answers1

3

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

Steven Kanberg
  • 6,078
  • 2
  • 16
  • 35