How to create your own bot

Minh Dong
Dev Tutorials
Published in
4 min readJun 5, 2017

--

In this post, I’ll walk you through steps that I’ve used to created my own bot that could understand natural language and response accordingly. The bot work with text message, slack, facebook messenger, skype, web chat etc.

The bot was built from Microsoft Bot framework , using microsoft cognitive services to understand the conversations (There are many other cool API from cognitive services too such as Computer Vision, Face API, Speech recognition, translation, recommendation, etc.). For now, I’m just using Language Understanding Intelligent Service to handle users input.

I’m using NodeJS to stand up a quick service for my bot and deploy that to Heroku.

So let’s start building our first bot!

Here’s basic overview of our bot

This tutorial will have 4 main parts:

A. Setup your bot

B. Test your bot

C. Using LUIS to support your bot conversation

C. Deploy your bot

<A> Setup your bot:

  • You will need nodejs and express (or restify), create a bot folder to hold your directories and make it as your working folder.
$ mkdir nodebot
$ cd nodebot
  • Initialize the project and install express (or restify)
$ npm init
$ npm install express --save
$ npm install body-parser --save
  • Install botbuilder
npm install botbuilder --save
  • Setup ESLint (you don’t have too but usually I like to start this for my projects)
$ npm install eslint --save-dev
  • Start your simple service by adding:
const express = require('express');const bodyParser = require('body-parser');
const app = express();app.use(bodyParser.urlencoded({ extended: false }));app.use(bodyParser.json());
// middleware error handling
app.use((err, req, res, next) => {if (res.headersSent) {return next(err);}console.error(err.stack);return res.status(500).json({ message: err.message });});app.listen(process.env.PORT || 3000, () => {console.log('Example app listening on port 3000!');});
  • Now if you go to http://localhost:3000 in your browser, you will get a response back from your server.
  • Move on to more interesting pieces, we will add our bot codes. Now go back to the nodebot folder, add a controllers folder and a news.js file in there. This news.js file will be the heart of our bot. It will listen to requests from users, call newsapi to full fill user’s requests then response back with appropriate messages.
  • You also need have a bot app id and password. Go to https://dev.botframework.com/bots/new and register for your bot. This will provide you those information.
  • Now put this code in the news.js file you created above:
const builder = require(‘botbuilder’);const env = process.env;function init(app) {// Create chat bot and bindingconst connector = new builder.ChatConnector({appId: env.BOT_APP_ID,appPassword: env.BOT_APP_PASSWORD,});app.post(‘/api/news’, connector.listen());const bot = new builder.UniversalBot(connector, (session) => {session.send(‘Sorry, I did not understand \’%s\’. Send \’help\’ if you need assistance.’, session.message.text);});// Print out help messagebot.dialog(‘Help’, (session) => {session.endDialog(‘Hi! this is a help message’);}).triggerAction({matches: ‘Help’,});}module.exports.init = init;

All this does are:

  1. Create a connector with the appID and Password you get from botframework.com.
  2. Create a universal bot using that connector
  3. Our bot will listening to command and response back. So if you send “Help” , our bot will send back “Hi! this is a help message”
  • Now call this news bot in our app.js

Adding the following pieces to your app.js. This basically call init function in the news.js and pass in a app function so we could listen to endpoint /api/news

const newsbot = require(‘./controllers/news’);newsbot.init(app);
  • One note: You can see I used process.env.BOT_APP_ID. To run the application, you need to define this variable in your nodejs environment. Dotenv is a nice way of doing this https://github.com/motdotla/dotenv , later when we deploy our service, we will need to setup this environment variables as well. If you are using VSCode, you could define it in the launch.json like below:
{“version”: “0.2.0”,“configurations”: [{“type”: “node”,“request”: “launch”,“name”: “Launch Program”,“program”: “${workspaceRoot}/app.js”,“env”: {“BOT_APP_ID”: “xxxxxxxxx”,“BOT_APP_PASSWORD”: “xxxxxxxxx”,“PORT”: “3001”}}]}
  • So thats it. You just created your own bot that could listen to Help command and response to it.
  • Don’t forget to push your code to your favorite git repo :)

<B> Test your bot:

  • To try out and test your bot locally, Microsoft provides BotFramework Emulator
  • Once you installed the latest version, you could input URL, Microsoft AppId and App password (you already created this above)
  • Run your service, and input all those information.
  • Now type Help and hit send. You will see your bot reply back with “Hi! this is a help message” :)
  • You could set a breakpoint in the code and try again, you should be able to see the breakpoint get hit.

Thats it. With <A> and <B>, you just created your first simple bot and debug it.

Next time, in <C> and <D> i’ll walk you through how you could add Azure Cognitive API to understand natural languages and then deploy our bot so it could be used in Skype, Slack, email, text, messenger, etc.

Click❤ below to recommend this to other Medium readers interested in AI, chatbots and development.

--

--