In article Develop ChatBot on NodeJs platform Using Microsoft Bot Framework (Part One) - Quick start for beginners, we have discussed where and how to start with development of chatbot on NodeJs platform using Microsoft Bot Framework. In this article, we are going to see the usage of Dialogs which will help us to manage conversation flow.
Prerequisite
- Node.js installation.
- Visual Studio Code installation
- Download Bot Framework Emulator. The emulator is a desktop application that lets you test the bot application on localhost or running remotely.
- Go through the article Chat Bot on NodeJs platform Using Microsoft Bot Framework (Part One) - Quick start
We are going to extend the example we have created in the previous article. We are going to create a bot which will help the user to order apples.
You can pull code from GitHub or file attached with the article.
Root dialog
The purpose of bot is to guide the user throug a sequence of steps and collect required data from user. We can implement a series of tasks that the user needs to perform with the waterfall concept. These tasks can be mentioned as array of functions. These functions will call next one after getting data from user.
- var applesOrder = function(builder) {
- var welcomePrompt = function(session) {
- session.send("Hello there, I am here to help you to order you grocery.<br/>");
- builder.Prompts.text(session, "May I know your good name?");
- };
- var numberOfApplesPrompt = function(session, results) {
- session.dialogData.userName = results.response;
- builder.Prompts.number(session, "Hey " + session.dialogData.userName + ", How many apples you wanna order?");
- };
- var userAddressPrompt = function(session, results) {
- session.dialogData.noOfApples = results.response;
- builder.Prompts.text(session, "What will be delivery address?");
- };
- var deliveryTimePrompt = function(session, results) {
- session.dialogData.userAddress = results.response;
- builder.Prompts.time(session, "What will be your prefered time for delivery? (e.g.: 22nd Oct at 3pm)");
- };
- var goodByePrompt = function(session, results) {
- session.dialogData.deliveryTime = builder.EntityRecognizer.resolveTime([results.response]);
- session.send("Order has been placed!<br/> It will be delivered to you by %s.", session.dialogData.deliveryTime);
- session.send("Have a good time, " + session.dialogData.userName + "!");
- };
- this.form = [welcomePrompt, numberOfApplesPrompt, userAddressPrompt, deliveryTimePrompt, goodByePrompt];
- };
- module.exports = function(builder) {
- return new applesOrder(builder);
- };
We are going to export new object of this form. Export function will take builder object as parameter and call constructor of applesOrder with builder parameter.
Now, we will modify starter.js to call applesOrderForm.
- var restify = require('restify');
- var builder = require('botbuilder');
- // Setup Restify Server
- var server = restify.createServer();
- server.listen(process.env.port || process.env.PORT || 3979, function()
- {
- console.log('Bot Application is avalable at (%s)', server.url);
- });
- // Create chat connector for communicating with the Bot Framework Service
- var connector = new builder.ChatConnector({
- appId: process.env.MICROSOFT_APP_ID,
- appPassword: process.env.MICROSOFT_APP_PASSWORD
- });
- // Listen for messages from users
- server.post('/api/order_your_grocery', connector.listen());
- // Load apples order form
- var applesOrder = require('./applesOrderForm.js')(builder);
- // Initialize bot with connector and array of tasks in apples order form
- var bot = new builder.UniversalBot(connector, applesOrder.form);
Let's debug the app. Open node.js command prompt and run node starter.js command. It will host the application on port 3979 and start listening. Now launch bot emulator app. Put URL ashttp://localhost:3979/api/order_your_grocery to connect emulator with bot application. Keep App ID and App Password blank, click on connect. Start a conversation with bot.
welcomePrompt
It will greet the user with a welcome message and prompt for the name of the user. Wait for user input; after that, call next function.
numberOfApplesPrompt
Get the user name from results parameter and save it to dialogData of the session. Prompt for the number of apples. Wait for user input which should be of type number, after that call next function.
userAddressPrompt
Get the number of apples from results parameter and save it to dialogData of the session. Prompt for the address for delivery. Wait for user input, after that call next function.
deliveryTimePrompt
Get the address from results parameter and save it to dialogData of the session. Prompt for the time for delivery. Wait for user input which should be of type DateTime, after that call next function.
goodByePrompt
Get the time for delivery from results parameter and save it to dialogData of the session. Say goodbye to the user.
This is how we can manage conversation using default (root) dialog. In the next article, we will discuss managing conversations with multiple dialogs. Till then, keep developing bots!

Join the conversation! Your thoughts help the community grow.