I’m having a lot of fun developing against botbuilder - the Node.js SDK for the bot framework.
When you’re learning to make bots, you study and build a lot of simple bots that do very little. In this case, it makes good sense to simply define the bot’s dialogs in the same file where you do everything else - the file you may call server.js or app.js or index.js. But if you are working on a bot with enough complexity or bulk to the dialogs, you’ll want to settle on a pattern.
Notice in the following code that the main
app.js file configures the dialog by requiring it and then calling the returned function passing in the
bot object. That allows the dialog to use the
bot internally (even though it’s a separate module) to call
bot.dialog() and define the dialog functions.
The result is a much more concise
app.js file and a bit of welcome encapsulation. The dialogs handle themselves and nothing more.
Later, while I was working with Johnson and Johnson on a bot, I developed a pattern for dynamically loading dialogs based simply on a) their presence in the
dialogs project folder and b) their conformation to a simple pattern.
To create a new dialog, then, here’s all I need to do…
getFileNames function is my own, but it simply reads the path you pass in recursively returning all
.map() calls require on the path of each found file and adds the resulting export (in our case here the modules are exporting a function) to the array as a property called
Finally, we call
.forEach() on this and actually execute the function. This configures the dialog for our bot.
The overall result then is the ability to add dialogs to the bot without any wiring. You just create a new dialog, give it a filename that makes good sense in your application, and it should be loaded and ready to be targeted.
You may not get enough context from these snippets to implement this if that’s what you want to do, so check out a fuller sample in the botstarter repo that @danielegan is working on. The botstarter repo is designed to be a good starting point for creating bots.