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.
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.
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:
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.
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.
And to start your bot with nodemon:
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:
When testing your bot, you use the
endpoint URL template. We’re running our bot locally with the default 3978 port so our full endpoint URL is
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
Let’s take a look at our bot.js file again to see how our current bot logic functions.
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.
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.
Let’s look at the code.
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.
Now it’s time to register your bot in Microsoft Azure Portal, deploy it and start using it in your channel.