Develop ChatBot On NodeJS platform Using Microsoft Bot Framework (Part One) - Quick Start For Beginners

Microsoft bot framework includes Bot Builder tools which help in bot development. Bot Builder is an open-source SDK. It supports .NET, Node.js, and REST. The Bot Builder SDK for Node.js uses Express & Restify frameworks. In this article, we are going to develop a chatbot on NodeJs platform.

Prerequisite

After installation, open Node.js command prompt (run as administrator). Create a folder for bot files (I have created folder ChatBot_NodeJs) and navigate to that path in command prompt.

Run command npm init. This command will ask a set of questions and create package.json with the options entered. If command is invoked with -f, --force, -y, or --yes, it will use defaults and will not prompt for options.

Here, I have entered the bot name as order_your_grocery. The starting js file for the application will be starter.js. After entering all details, it will print content for package.json and will ask for confirmation. After we confirm with yes, it will create the file and save it to the folder.

Run the following command to install Bot Builder SDK.

  1. npm install --save botbuilder

Run the following command to install restify framework.

  1. npm install --save restify

Now, open bot folder in Visual Studio code and create a JavaScript file in this folder with name starter.js and start coding in this file.

Node.js follows CommonJS module system. To include modules in different files, the require function can be used. Require function reads and executes JavaScript file, and return the exports object. We will load restify and botbuilder modules using this require function.

  1. var restify = require('restify');
  2. var builder = require('botbuilder');

Run the following command to install Restify framework.

  1. npm install --save restify

Now, open bot folder in Visual Studio code and create JavaScript file in this folder with name starter.js. And start coding in this file.

Node.js follows CommonJS module system. To include modules in different files, require function can be used. Require function reads and executes JavaScript file, and returns the export object. We will load restify and botbuilder modules using this require function.

  1. // Setup Restify Server
  2. var server = restify.createServer();
  3. server.listen(process.env.port || process.env.PORT || 4444, function () {
  4. console.log('Bot Application is avalable at (%s)', server.url);
  5. });

Now, we will create connector object with app ID and password. We will install new route to restify server to handle POST request as all messages from user will be sent over post call to server. Then, we will initiate the bot object with connector and message handler function.

  1. // Create chat connector for communicatin
  2. var connector = new builder.ChatConnector({ appId: process.env.MICROSOFT_APP_ID, appPassword: process.env.MICROSOFT_APP_PASSWORD });
  3. // Listen for messages from users
  4. server.post('/api/order_your_grocery', connector.listen());
  5. var messageHandler = function(session){
  6. session.send("Hello there, Let me help you to order your grocery.");
  7. };
  8. // Initiate bot with connextor and message handler
  9. var bot = new builder.UniversalBot(connector, messageHandler);

It’s time to test our bot application. Run the following command to start node.js bot app. It will run the application and show output mentioned in the callback function of server.listen.



Now, launch the bot emulator app. Put URL as http://localhost:4444 /api/order_your_grocery to connect emulator with bot application. Keep App ID and App Password blank, click on Connect. Send "Hello" to bo; it will reply to you with welcome message we have written in messageHandler method.


This is just a quick start on NodeJs platform; in the next article, we will discuss bot communication using dialogs. Till then, keep trying bot apps.

Up Next
    Ebook Download
    View all
    Learn
    View all