Intermediate

8 minute read

You should read this first.

Build a bot with Bot Framework SDK

ChatBot development

First off, we’ll show you how to start your bot. Then, you can start your bot development with Bot Framework SDK and all its capabilities. Any previous knowledge on JavaScript, or any programming language for that matter, will greatly help you in the process.

Bot files

In the last article we took a brief look at the file structure and now we should look a bit deeper. We won’t explain everything, just the important lines that you might need to change or use later in your development.

index.js

Copy to Clipboard

This is the file you run in order to run your bot. This isn’t everything that’s in this file, but this is the most important stuff. So, we can see the configuration for .env (dotenv) file which stores our environment variables. Here we import our restify module, our bot.js file and “BotFrameworkAdapter”. We create a server with restify and make it listen on a PORT that we define here or in our .env. Now, we are ready to run our bot. The bot needs to ne initialized, and after that we run it with await myBot.run(context); command. You can see that we pass our context on to our bot when we start it.

bot.js

Copy to Clipboard

In bot.js, we have just a few stuff going on, apart from importing classes and creating a class and a constructor, we have just 2 methods. Those methods aren’t really important and you can just delete them if you want, but they are good for testing. The first one, this.onMessage, echoes the message, you wrote, back and the second one, this.onMembersAdded, sends greetings when a new member joins the chat. You’ll see how they work later in this article. Understanding those 2 simple scripts helps you quickly write one yourself.

Now that we understand what our bot consists of, we’re gonna start it and show you how to test it and write some new scripts.

Start your bot

You can start your bot by entering:

Copy to Clipboard

command in console. We are using Visual Studio Code and its own console because it’s great to have a working environment and console for running commands and debugging in the same place. When you start your bot, you should se something like this in your console.

Bot development console

Alternatively you can install and use nodemon. Nodemon automatically restarts your bot when you make changes in bot files, so you can test your bot more easily.

Copy to Clipboard

And to start your bot with nodemon:

Copy to Clipboard

Test your bot

To test your bot, you need to download Bot Framework Emulator. Once installed, you can run your bot in the background and start testing it with Bot Framework Emulator. See the pictures bellow:

Bot Framework SDK development test setup

When testing your bot, you use the

  • https://IPaddress:PORT/api/messages

endpoint URL template. We’re running our bot locally with the default 3978 port so our  full endpoint URL is

  • http://localhost:3978/api/messages
Bot Framework Emulator test

So, you talk to your bot and he talks back, simple enough.

NOTE: Your bot must receive your message within 15 seconds otherwise it’ll timeout, it doesn’t have to respond within 15 seconds.

Bot Framework SDK development

Bot logic

Let’s take a look at our bot.js file again to see how our current bot logic functions.

bot framework sdk bot logic

If you look at the test conversation in Bot Framework Emulator and look at the code, you can see what’s happening. “onMembersAdded” method sends a messsage for each member added only once, when you’ve been added to the conversation. “onMessage” method sends your message back with the prefix “echo:”.

We want to get rid of echoes because they are annoying and they don’t really have a purpose, so let’s replace them with something better. We’ll write a simple method that detects the first word as a keyword to run the action, and process the rest of the message for that action. Specifically, when you write “stack *something*”, it’s gonna search StackOverflow for “something” and send you a search link. See the picture bellow.

Bot Framework SDK simple script

And when you click the link you get a stack overflow search. It’s a very simple method that can be expanded to W3schools, Wikipedia and other websites. Of course getting links isn’t the best way to do it, getting data, parsing it and showing it in chat would be much better. But, that’s for another time.

Bot Framework SDK simple script result

Let’s look at the code.

Copy to Clipboard

First we check if the message starts with “stack”, if not, nothing happens, If it starts with “stack”, it’s gonna execute following code. We need to strip the word “stack” from the search and replace all the spaces with “+” to make a proper search. After that it’ll return the link and the search page opens as though as we opened it ourselves and searched it ourselves.

You can see that we’ve put 10 repeats in the for loop. That’s because you really wouldn’t search for a keyphrase with more than 10 words. For example, “How to create properties in javascript” contains 6 words. This script very simple and can be improved in many ways, we just wanted to show you how to write a simple script how the bot executes the code. You are free to develop your own scripts as you see fit.

Now it’s time to register your bot in Microsoft Azure Portal, deploy it and start using it in your channel.

Up next.

Azure bot registration