Getting started with Bots

Introducing the Bot Framework

One of the most common phrases when I’m talking about technology for end users is “meet them where they’re at.” A big reason applications fail to be adopted is they require too large of a change in behavior from the users in question, having to open yet another tool, access another application, etc. We as humans have a tendency to revert to our previously learned behaviors. As a result, if we want to get our users using a new process or application we need to minimize the ask as much as possible.

This is one of the biggest places where bots can shine: they can be placed where our users already are. Users are already using Office, Slack, Skype, etc. A bot can then provide information to the user in the environment they’re already in, without having to open another application. Or, if they want to open an application, the bot can make that easier as well. In addition, the user can interact with the bot in a natural language, reducing the learning curve, making it seem more human, and maybe even fun.

At //build 2016 Microsoft announced the Microsoft Bot Framework, a set of APIs available for .NET and Node.js, to make it easier for you to create bots. In addition, we also announced Language Understanding Intelligent Service (LUIS), which helps break down natural speech into intents and parameters your bot can easily understand.

What I’d like to do over a handful of posts is help get you up and running with a bot of your own. We’ll use Node.js to create a simple “Hello, world!” bot, and then add functionality, allowing it to look up user information in GitHub, and then integrate it with various chat services.

Important notice

The Bot Framework is currently under development. As a result, things are changing. While many of the concepts we’ll talk about will likely remain the same, there may be breaking code changes in the future. You have been warned. ;-)

Getting started

Couple of prerequisites to take care of right up front. We are going to be using Node.js, so you will need to be familiar with JavaScript, and have some understanding of Node. There is an MVA on Node if you’re interested. I’m going to assume knowledge of npm as well. Finally, I’ll be using ES6 syntax as appropriate.

With that in mind, let’s create a folder in which to store our code, and install botbuilder.

1
2
npm init
npm install --save botbuilder

As for the initialization, I’m not overly concerned with the settings you choose there, as we really just need the package.json file; you can just choose all of the defaults.

Hello, bot

Let’s start with the stock, standard, “Hello, world!”, or, in this case, “Hello, bot!”

Creating an interactive bot requires creating two items, the bot itself, which houses the logic, and the connector, which allows the bot to interact with users through various mechanisms, such as Skype, Slack and Facebook.

In regards to the connector, there’s two connectors provided in the framework - a ConsoleConnector, perfect for testing and proof of concept as you simply use a Windows console window to interact with your bot, and the ChatConnector, which allows for communication with other clients, such as Slack, Skype, etc. You’ll start with the console connector, as it doesn’t require any other client than the standard Windows console.

As for the bot, you’ll create a simple bot that will send “Hello, bot” as a message. To create the bot, you will pass in the connector you create.

Create a file named text.js, and add the following code:

1
2
3
4
5
6
7
8
9
10
11
12
// text.js
const builder = require('botbuilder');
const connector = new builder.ConsoleConnector();
const bot = new builder.UniversalBot(connector);
bot.dialog('/', (session) => {
session.send('Hello');
});
connector.listen();

Let’s start from the top. The first line is the import of botbuilder, which will act as the factory for many objects we’ll be using, including ConsoleConnector, as you see in the second line.

To create a bot, you need to specify its connector, which is why you’ll create that to start. The connector is used to allow the bot to communicate with the outside world. In our case we’ll be interacting with the bot using the command line, thus ConsoleConnector. Once you’ve created the connector, you can then pass that into the bot’s constructor.

The design of a bot is to interactively communicate with a human through what are known as dialogs. The next line adds in a dialog named /. Dialogs are named similar to folders, so / will be our starting point or root dialog. You can of course add additional dialogs by calling dialog yet again, but more on that in a later post. The second parameter is the callback, which, for now, will accept session. session manages the discussions for the user, and knows the current state. You’ll either use session directly to communicate with the user, or pass it into helper functions to communicate on your behalf.

The simplest method on session is send which, as you might imagine, will send a message to the user. If you run text.js, and type in anything and hit enter (make sure you type something in to activate the bot!), you’ll see the message.

1
2
3
4
node text.js
// Type: Yo
// Output: Hello, bot!

Interaction note

You need to send a signal to the bot first in order to “wake it up.” When you’re using the command line for initial testing this can be a bit confusing, as you’ll run the application and notice that nothing is displayed on the screen. When you run your bot, just make sure you send it a message to get things started.

Retrieving user input

Obviously, displaying a static message isn’t much of a bot. We want to interact with our user. The first step to doing this is to retrieve the message the user sent us. Conveniently enough, the message is a property of session. The message will allow us to access where it was sent from, the type, and, key to what we’re doing, the text.

Let’s upgrade our simple bot to an echo bot, displaying the message the user sent to us.

1
2
3
4
5
6
7
8
9
10
11
12
// text.js (full code)
const builder = require('botbuilder');
const connector = new builder.ConsoleConnector();
const bot = new builder.UniversalBot(connector);
bot.dialog('/', (session) => {
session.send(session.message.text);
});
connector.listen();

You’ll notice we updated the session.send call to retrieve text from message, which contains the user input. Now if we run it we’ll see the information the user typed in.

1
2
3
node text.js
Hello from the user
// Output: Message: Hello from the user

Wrapup

Bots are a way for users to interact with services in a language that’s natural, and in applications they’re already using. You can integrate a bot with an existing application, such as a web app, or with a tool users are already invested in, such as Slack or Skype. We got started in this blog post with bots by first obtaining the SDK, and then creating a simple bot echo service. From here we can continue to build on what we’ve learned to create truly interactive bots.