Create A Bot With Microsoft Bot Framework Using Node.js

In this article, you will learn how to create a Bot with Microsoft Bot Framework. We will create a bot that can visually identify Men's and Women's outfits using Custom Vision Service. But first, we are going to create an echo bot.

Prerequisites

Installs needed

    • Node.js (and npm)
    • git
    • VS Code

Signups needed

    • MSID
    • botframework.com
    • Azure Account

So, let's get started.

First of all, let's create a Git repository and set it gitignore for ignoring Node Modules and the usual Readme.

It will look like this,

The reason we are doing this is that we want to implement Continuous Integration and Continuous Deployment process to Azure later.

Now, log into Azure Management portal.

Click New (+ Icon) > Web + Mobile > Web App.

Fill the required fields.

 Go to your Web App > Deployment Option > Choose Source > GitHub.

Authorise with your GitHub account and select the repository.


Check if your web app is working or not. visit http://YOUR_APP_NAME.azurewebsites.net. Whoa! It works! Don't worry if it shows Permission error.

Now visit: https://dev.botframework.com/

Sign in with your Microsoft account and click on "Create a Bot" option. You will get a page similar to this.

Fill in the following details

Bot Name (Name for your Bot)

Bot Handle (Used in the URL of your Bot)

Description (Your bot's long description which may appear in bot directories.)

Configuration

Messaging Endpoint:

Enter your Azure Website URL with https and append /api/messages

Example -

https://YOUR_APP_NAME.azurewebsites.net/api/messages

Click on “Create Microsoft App ID and Password”.

In New tab, click on "Generate Password".

Note - Save your password and App ID in a separate file. It will be shown only once.

Now, hit Return.

Once you are back to “Tell us About your bot” page, leave everything as it is, then accept the T&C and click "Register". 

Now, let’s write code!

Open Command Prompt and clone the repository:

Take the URL from GitHub and run.

  1. git clone REPOSITORY_URL  
  2. cd FOLDER_NAME  
  3. git status  
  4. code README.md  
  5. npm init ::(Set the package.json attributes set entrypoint app.js)  
  6. npm install --save botbuilder  
  7. npm install --save restify   

Open app.js and paste this code,

  1. var restify = require('restify');  
  2. var builder = require('botbuilder');  
  3.   
  4. var server = restify.createServer();  
  5.   
  6. server.listen(process.env.port || process.env.PORT || 3978, function () { });  
  7.   
  8.   
  9.   
  10. // Create the chat connector for communicating with the Bot Framework Service  
  11. var connector = new builder.ChatConnector({  
  12.     appId: 'MICROSOFT_APP_ID’ ,  
  13.     appPassword: 'MICROSOFT_APP_PASSWORD’   
  14. });  
  15.   
  16.   
  17. // Listen for messages from users   
  18. server.post('/api/messages', connector.listen());  
  19.   
  20. server.get('/', restify.plugins.serveStatic({  
  21.  directory: __dirname,  
  22.  default'/index.html'  
  23. }));  
  24.   
  25.   
  26. var bot = new builder.UniversalBot(connector, function (session) {  
  27.     var msg = session.message;  
  28.     if (msg.attachments && msg.attachments.length > 0) {  
  29.      // Echo back attachment  
  30.      var attachment = msg.attachments[0];  
  31.         session.send({  
  32.             text: "You sent:",  
  33.             attachments: [  
  34.                 {  
  35.                     contentType: attachment.contentType,  
  36.                     contentUrl: attachment.contentUrl,  
  37.                     name: attachment.name  
  38.                 }  
  39.             ]  
  40.         });  
  41.     } else {  
  42.         // Echo back users text  
  43.         session.send("You said: %s", session.message.text);  
  44.     }  
  45. });  
Replace ‘MICROSOFT_APP_ID’ and ‘MICROSOFT_APP_PASSWORD’ with your app id and password. Then "Save" the code.

Now, go to http://dev.botbuilder.com and click on your bot followied by selection of web chat.

Copy the Iframe code, create an index.html file, and paste the code into that:

  1. <iframe src='https://webchat.botframework.com/embed/botforartical?s=YOUR_SECRET_HERE' height="90%" width="90%"></iframe>  

Replace YOUR_SECRET_HERE with your own Secret key.  You will get that on the same page.

And finally, run these commands.

  1. git add .  
  2. git commit –m “Added all requirements”  
  3. git push origin master   

Your echo bot is ready and running. Visit your Azure website URL.

Conclusion

In this article, we learned how to create an echo bot and deploy it on Azure.

What's Coming

In the next post, I will show you how to create a custom image classifier using Microsoft Custom Vision Service and connect it to the bot we created today.

Up Next
    Ebook Download
    View all
    Learn
    View all