Botframework-sdk: How to have seperate files for seperate sets of dialogs?

Created on 18 Oct 2016  路  8Comments  路  Source: microsoft/botframework-sdk

I have 20 bot.dialogs which are related to "Car" and another 20 bot.dialogs which are related to "Bike".
And there are a lot more categories.
So now you see where this is going. I dont want to have a large single js file. I want to split them into multiple files.
What is the best way to do it?

help wanted

Most helpful comment

One simple way to do this is have a seperate file for your bots. Lets say for example you want to put this dialog into a separate file

bot.dialog('/cars',function(session){session.send('test')});

Create a new file called cars.js and create a module for this dialog as following

module.exports = function(bot) {
bot.dialog('/cars',function(session){session.send('test')});
}

Now in the original file you can use this by requiring it and passing the bot object as an argument
as such

require('./cars.js')(bot);

All 8 comments

you could create a separate file per category. You can load as many dialogs as you'd like and you can even create them dynamically. There are any number of ways to do this. It's difficult to say what's the "best" way.

One simple way to do this is have a seperate file for your bots. Lets say for example you want to put this dialog into a separate file

bot.dialog('/cars',function(session){session.send('test')});

Create a new file called cars.js and create a module for this dialog as following

module.exports = function(bot) {
bot.dialog('/cars',function(session){session.send('test')});
}

Now in the original file you can use this by requiring it and passing the bot object as an argument
as such

require('./cars.js')(bot);

@GanadiniAkshay Thanks that really helped a lot.
Currently I am doing it like this,
I have a main "server.js" and "car.js"
in server.js

//initializes all the prompts in car
car.createPrompts(bot);
//this is how I call the dialog in car.js
car.beginCarDialog;

and in car.js

// I declare all the prompts here like this which I used earlier to initialize the prompts
exports.createPrompts = function(bot) {
var askCarRegNo = [
function(session) {
},
function(session, results) {
}
];
bot.dialog('/cardialog', askCarRegNo);
}

// I export this function which will call the respective dialog internally
exports.beginCarDialog = function(session, options) {
session.beginDialog('/cardialog', options || {});
};

But your method seems much more promising because its much less code and I think I can call the

module.exports = function(bot) {
bot.dialog('/cars',function(session){session.send('test')});
}
above dialog which is in car.js into my main server.js file by simply session.beginDialog('/car');

I'd say you're on the right path. If you look at the libraries example you might get some inspiration. You don't need to actually use libraries but you'll notice how we put the library in a separate file and expose a create() function to add the library to the bot and a chooseLocale() function to abstract calling beginDialog() like you did.

This is an old post but I am having trouble doing the same in my typescript bot. Anyone who could help with that?

@flinkr in case if you are still having that issue (it's been a while) - you can have something like this:

car.ts

const CarDialog = (bot) => {
  bot.dialog("car", [
    (session, args, next) => {
      .....
    },
    (session, args, next) => {
      .....
    },
]);

export default CarDialog;

original file:

import CarDialog from "./car";
...
CarDialog(bot);

Hi guys! Thanks for your explanations and examples (currently working on separation of dialogs into separate files too).
How do you deal with a situation when the exported dialog uses session.beginDialog(')?
I mean how to correctly do the export of constructions like:

// "Master" dialog
var bot = new builder.UniversalBot(connector, [
    function (session) {
        session.send("Hi");
        session.beginDialog('askFor1');
    },
    function (session, results) {
        session.send([results.response]);
        session.beginDialog('askFor2');
    },
    function (session, results) {
        session.endDialog([results.response]);
    }
])

// Called subdialogs
bot.dialog('askFor1', [
    function (session) {
        builder.Prompts.time(session, "Please provide time");
    },
    function (session, results) {
        session.endDialogWithResult(results);
    }
]);

bot.dialog('askFor2', [
    function (session) {
        builder.Prompts.text(session, "Please provide quantity");
    },
    function (session, results) {
        session.endDialogWithResult(results);
    }
])

P.s. Sorry if dumb question - only learning JS. Thanks in advance

A, it appeared to be very simple:
Dialog being exported to a separate file:

module.exports = (bot, builder) => {
  bot.dialog('aloha', (session) => {
    session.beginDialog('askForName');
  })
    .triggerAction({
      matches: /^aloha$/i,
    });

  // Called dialog
  bot.dialog('askForName', [
    (session) => {
      builder.Prompts.text(session, 'What is your name?');
    },
    (session, results) => {
      session.endDialog(`Nice to meet you, ${results.response}`);
    },
  ]);
};

And the main file, which imports it:

const aloha = require('./dialogs/aloha');
...
bot.dialog('aloha', aloha(bot, builder));
Was this page helpful?
0 / 5 - 0 ratings