Microsoft Bot Framework

Microsoft released their new Bot Framework early this year at the Build Conference. So, naturally, I wanted to create my own; eventually integrating it into a game. In this post I talk about some of my learnings and what the bot framework provides.

I decided to work with the Microsoft Bot Connector, part of the Microsoft Bot Framework, as a way to get my bot up and running on the most platforms as quickly as possible. I haven’t worked with bots in the past so this was my first dive into the territory. My bot was built in C# however, Microsoft’s Bot Framework can also be built in Node.js. My colleague Sarah wrote a post about getting started with Node here:

The bot I wanted to create was a simple chat bot that I could build upon for interactivity with users. If your familar with The 100, you’ll figure out what my bot does. All the code for what I did can be looked at here:

What I used

Microsoft Bot Framework is super powerful and makes it easy to create a bot of your own. You can use any of the following to get started:

  • Bot Connector
  • Bot Builder C#
  • Bot Builder Node.js

I used the Bot Connector, an easy way to create a single back-end and then publish to a bunch of different platforms called Channels.

I started out by following the steps in the getting started section of the docs and downloaded the Bot Template for Visual Studio here:

** Note: It’s really important for Visual Studio to be updated in order to use this, as well as download the web tools in the Visual Studio Setup when you download.**

** Another Note: if you have never downloaded a template for Visual Studio before here are some instructions: You’ll have to save the zip into the %USERPROFILE% folder on your computer. **

Set Up

Open a new project with the Bot Template, and install the nuget package for the Microsoft’s Bot Builder: install-package Microsoft.Bot.Builder

Message Controller

The file that dictates the flow of responses is the MessageController.cs in the “Controllers” folder. The class handles systems messages and allows you to control what happens when a message comes through.

Adding the following conditional statement to the Post function allows you to cater the response to the user.

Let’s create a simple response:

public async Task Post([FromBody]Message message)
    if (message.Type == "Message")
        return message.CreateReplyMessage($"You said:{message.Text}");
        return HandleSystemMessage(message);

Now you can stick with this model and add in bits of functionality but I like to add a more powerful messaging system with Dialogs.


** Now there are slight differences between the BotConnector Dialogs for Node vs C#. Everything in this post pertains to the C# verison.**

Bot Builder uses dialogs to manage a bots conversations with a user. The great thing about dialogs is that they can be composed with other dialogs to maximize reuse, and a dialog context maintains a stack of dialogs active in the conversation.

To use dialogs all you need to do is add the [Serializable] tag and extend the IDialog<> from Microsoft.Bot.Connector;

Dialogs handle asynchronus communication with the user. Because of this the MessageController will instead use the Conversation class to create an async call to a Dialog Task that uses the context to create a ReplyMessage with more functionality. What does that all mean? It means that with dialogs, you can implement a conversation with a user asynchronously when certain keywords are triggered. For example if the user types in the keyword “reset” we can have a PromptDialog to add the confirmation. One of the most powerful ways of creating a an actual dialog with the user and the bot is to add Chain Dialogs.

Chain Dialogs

Explicit management of the stack of active dialogs is possible through IDialogStack.Call and IDialogStack.Done, explicitly composing dialogs into a larger conversation. It is also possible to implicitly manage the stack of active dialogs through the fluent Chain methods.To look at all the possible ways to respond to a user with Dialogs you can look at the EchoBot Sample in Github.

Publishing your Bot

Okay now that you have tested your bot and got it to respond to your user, how do we publish? Well steps to getting your bot on the bot connector are here:

TIP 1: Update Visual Studio and tools

As I said earlier make sure all of your tools are on the latest update. My web tooling was not on the latest version when I first tried to publish my bot, so the directions were slightly different than the tutorial and caused issues later.

TIP 2: Don’t skip any of the steps

The first time I published my bot it didn’t work. I still have no idea why, but I believe it was because I missed a minor step in the creation process.

TIP 3: It should work immediately

Your bot should work immediately after you activate the web channel. If it doesn’t check your code again. My first bot was not working immediatley and I ended up just registering a new one with the same code. That worked.

TIP 4: Web disabled

If you look at my channel picture you can see that the web channel is registered but it’s status says “diabled”

Don’t worry about this. Your Bot web should still work.

TIP 5: Registering your bot

You don’t need to register your bot for it to work. Registering your bot will allow it to be in the publish gallery later. Make sure your bot does something useful before submitting as well. Simple chat bots do not count.

That’s it! You should have a bot published and all ready to Chat with.

Next Steps – LUIS

Okay so there are many ways that your bot can respond to your user. However specific keywords are needed and it’s less user friendly, and conversational than we would like. In order to make our bot respond to natural language that users will most likely be using to integrate LUIS. Which I’ll talk about in the next post with part 2.

Reading the Bot Framework Docs was extremely helpful when getting started, so if you haven’t looked at them I recommend you take a look here:

The Microsoft Bot Framework is opensource so you can help contribute to the project here: Microsoft/BotBuilder. They also have more samples included in their source code.

Happy Hacking!


Leave a Comment

Your email address will not be published. Required fields are marked *