First steps to create your first Bot

Bot Framework

In this article, we will learn how to create a bot using the new Microsoft Bot Framework announced at the Build 2016 event.

All what we need to create our first bot is a text editor (VS Code recommended), NodeJS installed, if not you can download NodeJS from here, and Microsoft Bot Framework SDK for NodeJS.

First of all, let’s look at how we can install the Bot Framework SDK for NodeJS.

We need to use NodeJS Package Manager (the npm).

  1. Create a new folder, then add a new file named package.json1
  2. Open the command line  (or Terminal) and move to your current folder (to open the Terminal in VS Code : Ctrl + ù)
  3. Tap npm install botbuilder --save
  4. Now you notice that the package.json file has changed.2

And here we are, ready to start the most existing part “coding“.

We’ll kick off by building a very basic bot to understand how it work. The main components are :

  • builder
  • connector
  • bot

First, we need to create a new file server.js. Then, we make a builder using this line of code :

var builder = require('botbuilder');

Now, the connector that we need to create the bot object:

var connector = new builder.ConsoleConnector().listen();

You may have noticed that we will need the console to communicate with the bot, yes you are right.

Last but not least, the bot object :

var bot = new builder.UniversalBot(connector);

Now all the components are ready for creating the logic of our bot.

bot.dialog('/', function(session){

 session.send('Hello, I am a bot');

}

with this piece of code we have completed building our first Bot.

5

Let’s try to understand it:

Dialog is a method to initiate a conversation between the bot and the user. It has as for arguments the root path and the function that’s contains the logic of the conversation. This last one has a session as an argument. The session object contains information about the conversation. We can read the user message by session.Message.text attribute.

And we are ready to run the bot. On the console (or Terminal) tap node server.js and send a message to the bot through the console, voilà!

All what we did is creating a simple bot using Bot Framework. For the next step, we will try to do something better. So stay tuned for the next article, don’t hesitate to let your question in the comment section!

Download the source code.