In the article Develop ChatBot On NodeJS Platform Using Microsoft Bot Framework (Part Two) - Manage Conversation Using Root Dialog, we discuss how to handle conversation using root dialog in chatbot developed using Microsoft Bot Framework Node.js SDK. In this article, we are going to handle the conversation flow with multiple dialogs instead of the root dialog. We are also going to use a trigger for dialogs.
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 Chat Bot on NodeJs platform Using Microsoft Bot Framework (Part One) - Quick start For Beginners.
- Go through Develop ChatBot On NodeJS Platform Using Microsoft Bot Framework (Part Two) - Manage Conversation Using Root Dialog.
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 grocery items.
Grocery Order Form - Multiple dialogs
We will add a new file to our solution as ‘groceryOrderForm.js’.
- var groceryOrder = function (builder) {
- var groceryMenuItems = ["Rice", "Soap", "Corn Flakes"];
- ...
- };
- module.exports = function (builder) {
- return new groceryOrder(builder);
- };
- this.welcomeDialog = {
- title: "Welcome",
- functions: [
- function (session) {
- builder.Prompts.text(session, "Welcome, may I know your good name?");
- },
- function (session, results) {
- session.userData.User = results.response;
- session.send("Hello %(User)s,<br/>I am here to take your order!", session.userData);
- session.beginDialog("Menu");
- }
- ]
- };
- this.menuDialog = {
- title: "Menu",
- functions: [
- function (session, flags) {
- if (flags && flags.IsMoreOrder) {
- builder.Prompts.choice(session, "Select more..", groceryMenuItems, { listStyle: builder.ListStyle.button });
- }
- else {
- session.userData.groceryItems = [];
- builder.Prompts.choice(session, "Please select grocery item from list:", groceryMenuItems, { listStyle: builder.ListStyle.button });
- }
- },
- function (session, results) {
- session.userData.groceryItems.push(results.response.entity);
- session.replaceDialog("Menu", { IsMoreOrder: true });
- }
- ]
- };
- this.checkoutDialog = {
- title: "Checkout",
- functions: [
- function (session) {
- builder.Prompts.text(session, "Where should we deliver your order?");
- },
- function (session, results) {
- session.userData.Address = results.response;
- builder.Prompts.time(session, "What time will you prefer?");
- },
- function (session, results) {
- session.userData.deliveryTime = builder.EntityRecognizer.resolveTime([results.response]);
- session.send("Thanks for your order %(User)s.", session.userData);
- console.log(session.userData.groceryItems);
- session.send("Your order of %(groceryItems)s will be delivered by %(deliveryTime)s", session.userData);
- session.endDialog();
- }
- ],
- };
At the last, we will add checkoutDialog for handling checkout functionality. First function will prompt the user for address of delivery, while the second function will save that address to the user data and prompt for preferred time for delivery. Third function will save the delivery time to the user data and prompt the user with Thank you and list of selected groceries.
- 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 grocery order form
- var groceryOrderForm = require('./groceryOrderForm.js')(builder);
- // Initialize bot with connector and default dialog
- bot = new builder.UniversalBot(connector, [
- function (session) {
- session.beginDialog(groceryOrderForm.welcomeDialog.title);
- }
- ]);
- bot.dialog(groceryOrderForm.welcomeDialog.title, groceryOrderForm.welcomeDialog.functions);
- bot.dialog(groceryOrderForm.menuDialog.title, groceryOrderForm.menuDialog.functions);
- bot.dialog(groceryOrderForm.checkoutDialog.title, groceryOrderForm.checkoutDialog.functions).triggerAction({
- matches: /^check out$/i,
- confirmPrompt: "This will cancel your request. Are you sure?"
- });
Here, we called the "require" function for groceryOrdeForm.js. We will initialize the universal bot with a root function which will begin a welcome dialog. Then, we will add all dialogs from groceryorderForm to our bot object by calling bot.dialog function.
While adding checkout dialog, we have added trigger action. It will take two parameters ‘matches’ and ‘confirmPrompt’. Whenever a user writes checkout to chat window, it will remove all the dialogs from the stack and add checkout dialog to stack for execution.
Test it
Now, open the terminal and run node starter.js command. It will start our bot application and host it on a specified port. Open emulator app and connect with the URL ‘http://localhost:3979/api/order_your_grocery’ to start a conversation.

This is how we can manage a conversation using multiple dialogs. In the next articles, we will discuss LUIS integration and mood detection in chatbot application. Till then, keep developing bots!

Join the conversation! Your thoughts help the community grow.