Creating simple multiplatform bot using Microsoft Bot Framework. Part 1

Microsoft Bot Framework


You all know, that bots are really popular now. They allow to do a lot of different things: check your delivery, send money, buy tickets etc. It’s more interesting way than native mobile app – they are crossplatform (you can use bot on mobile, desktop, web in your messenger), human-friendly, easy. But the main difficulty in developing your own bot is that fact, that you need create bot for each platform (like when you create different mobile app for Android and iOS). But Microsoft created a tool for you – Microsoft Bot Framework

Microsoft Bot Framework allows you to create your bot for different target platforms – Telegram, Facebook Messenger, Skype, Slack, Email and even SMS (using Twilio), using C# and Visual Studio!

The idea of my own bot came to me from my current situation – waiting working visa to the USA. There is a website – where user can enter his case number and check status of the process. So, my bot should do the same, but in faster way, because website loads very slow and you need to enter your case number every time. So bot should take a case number, get status and save this case number for next checks.

So, let’s start!

At first you need the latest Visual Studio 2015 with all last updates. Then you should download bot project template from here – and save the zip file to your Visual Studio 2015 templates directory which is traditionally in “%USERPROFILE%\Documents\Visual Studio 2015\Templates\ProjectTemplates\Visual C#\”.

Okay, now we can start with new Visual Studio project using Bot Application template:

Microsoft Bot Framework

Our project was created! Now, let’s go to the class MessengerController – entry point for your bot, because Microsoft Bot Framework project is REST API web service (WEB API project, if you want).

Here we have a POST method, which receives a new message and checks it type (System or regular). System type – Ping, Typing, Update etc. Visual Studio created a default stub for us for handling system messages – in our bot we don’t need them, but you can implement it on your own. So we are interested in usual messages:

It’s simple, yeah? 😉 Here, when we receive new conversation, our bot starts a CaseStatusDialog. There are two types of conversation for bots: Dialog and FormFlow. I selected the first one, due to more flexibility. FormFlow suits for that types of bots, which need to collect some form. For example, credit card info for money send, some order (for example, Pizza constructor) etc. I will show you how to use FormFlow in this bot in the next part, but now – let’s look on the better way 🙂

So, we need to create a CaseStatusDialog and inherit it from the IDialog<object>. This class should be serializable, for saving a state of the dialog:

The main idea of Dialogs in Microsoft Bot framework is to use a chain of bot-user messages. When we receive message – go to the next step and so on.

Here is the code:

At first, StartAsync method will be run on new conversation started. It just calls the ConversationStartedAsync method which starts conversation with bot’s “hi” message. If it’s not a first call to the bot (we have a case number saved in variable) – ask user about what action does he want: check his previously entered case number or enter a new one:

Let’s go to the situation with a new user.  It’s very simple. We only need to prompt a user about his number:

When user will answer back with his case number, ResumeAsync method will be called. This method will check case number by the regex, and if it’s correct – send request to the USCIS service and return user result or error:

It’s pretty clear: if number isn’t correct – show error message and ask about case number once more. Else – send a request to check status, print it and ask user about his wish to check another case or his previous one. As you can see, IAwaitable<string> argument is the message that we receive from the user. Here type is string, cause we expect only text, but it also can be something else (image or video).

CheckStatus method is also pretty simple: using regular WebClient object, send a request to website, parse return and print status:

Here is code of UscisService class:

And here is AskForPreviousOrAnother method, which sets two buttons: “Previous” and “Another”, so user can just click on them to answer what does he want. If he types something else, “I didn’t understand. Please try again.” text will be shown:

That’s it!

Note: If you were reading carefully, you can see, that we don’t check for saved case number in the first step, because we will always ask user after the check about checking previous or another case number.

Now we can test our bot locally using Microsoft Bot Emulator. You can download it here.

First of all, start your project in Visual Studio and you will see something like this in your browser:

Microsoft Bot Framework

Start your emulator and set your address to the bot, http://localhost:3979/api/messages i.e.:

Microsoft Bot Framework

And now you can start your conversation with your bot by sending him “hi”:

post5-4 Microsoft Bot Framework

That’s it!

In next articles I will describe how to use FormFlow and how to publish your bot.


P.S. As always, all code is on the GitHub:

Leave a Reply