Develop ChatBot On NodeJS Platform Using Microsoft Bot Framework (Part Two) - Manage Conversation Using Root Dialog

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

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.

Root dialog (initial dialog) can be specified as second parameter of UniversalBot class constructor. It will be of type array. Use bot from previous article and add a new file to folder named as applesOrderForm.js.
  1. var applesOrder = function(builder) {  
  2.     var welcomePrompt = function(session) {  
  3.         session.send("Hello there, I am here to help you to order you grocery.<br/>");  
  4.         builder.Prompts.text(session, "May I know your good name?");  
  5.     };  
  6.     var numberOfApplesPrompt = function(session, results) {  
  7.         session.dialogData.userName = results.response;  
  8.         builder.Prompts.number(session, "Hey " + session.dialogData.userName + ", How many apples you wanna order?");  
  9.     };  
  10.     var userAddressPrompt = function(session, results) {  
  11.         session.dialogData.noOfApples = results.response;  
  12.         builder.Prompts.text(session, "What will be delivery address?");  
  13.     };  
  14.     var deliveryTimePrompt = function(session, results) {  
  15.         session.dialogData.userAddress = results.response;  
  16.         builder.Prompts.time(session, "What will be your prefered time for delivery? (e.g.: 22nd Oct at 3pm)");  
  17.     };  
  18.     var goodByePrompt = function(session, results) {  
  19.         session.dialogData.deliveryTime = builder.EntityRecognizer.resolveTime([results.response]);  
  20.         session.send("Order has been placed!<br/> It will be delivered to you by %s.", session.dialogData.deliveryTime);  
  21.         session.send("Have a good time, " + session.dialogData.userName + "!");  
  22.     };  
  23.     this.form = [welcomePrompt, numberOfApplesPrompt, userAddressPrompt, deliveryTimePrompt, goodByePrompt];  
  24. };  
  25. module.exports = function(builder) {  
  26.     return new applesOrder(builder);  
  27. };  
In this file, we have created class with name applesOrder. Class contains private methods; welcomePrompt, numberOfApplesPrompt, userAddressPrompt, deliveryTimePrompt, goodByePrompt. And public property as form, which is an array of these functions.

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.
  1. var restify = require('restify');  
  2. var builder = require('botbuilder');  
  3. // Setup Restify Server  
  4. var server = restify.createServer();  
  5. server.listen(process.env.port || process.env.PORT || 3979, function()  
  6. {  
  7.     console.log('Bot Application is avalable at (%s)', server.url);  
  8. });  
  9. // Create chat connector for communicating with the Bot Framework Service  
  10. var connector = new builder.ChatConnector({  
  11.     appId: process.env.MICROSOFT_APP_ID,  
  12.     appPassword: process.env.MICROSOFT_APP_PASSWORD  
  13. });  
  14. // Listen for messages from users  
  15. server.post('/api/order_your_grocery', connector.listen());  
  16. // Load apples order form  
  17. var applesOrder = require('./applesOrderForm.js')(builder);  
  18. // Initialize bot with connector and array of tasks in apples order form  
  19. var bot = new builder.UniversalBot(connector, applesOrder.form);  
We will call a requirement  for applesOrderForm.js as builder object as parameter. Then, the bot object is created calling UniversalBot constructor with applesOrder.form as second parameter. When user starts conversation with bot these functions will be called in sequence, mentioned while defining that array.

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!


Similar Articles