Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to have seperate files for seperate sets of dialogs? #1457

Closed
vickylance opened this issue Oct 18, 2016 · 8 comments
Closed

How to have seperate files for seperate sets of dialogs? #1457

vickylance opened this issue Oct 18, 2016 · 8 comments
Assignees

Comments

@vickylance
Copy link

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?

@Stevenic
Copy link
Contributor

Stevenic commented Oct 19, 2016

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.

@GanadiniAkshay
Copy link

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);

@vickylance
Copy link
Author

@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');

@Stevenic
Copy link
Contributor

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.

@tomlm tomlm closed this as completed Nov 4, 2016
@ghost
Copy link

ghost commented Mar 9, 2018

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

@OZhurbenko
Copy link

@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);

@IuriiD
Copy link

IuriiD commented Sep 26, 2018

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

@IuriiD
Copy link

IuriiD commented Sep 27, 2018

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));

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants