Upgrade Bot Framework V1 to V3

I built a bot several weeks ago. But when I open and ran the bot emulator today, I saw a notification like this:

upgrade_notification

Then I followed the instruction and installed the new version of bot framework emulator. What happed then was then the bot application didn’t work anymore. When I typed message in the emulator, it just returned an exception.

After visiting Bot Framework offical site, I realized that Microsoft had just released a new version of Bot Framework: V3.

upgrade_officialsite.png

On this page, we can see the majoy change made for V3. But in order to convert our v1 bot to V3 bot, here are some things we need to look at:

  1.  In the new Bot Framework version, “BotBuilder and Connector are now one SDK”. We need to first remove the Bot Connector package reference and update the Microsoft.Bot.Builder package to latest version.
  2. Another big change is that Message object has been replaced by a new object called Activity. So the MessageController will be different. You can download the new Bot Application template from this link to create a V3 Hello World Application and see the new MessageController. NewMessageController
  3. Web.config got some change also. The appsettins used to look like this:oldsetting.pngNow it becomes this:newsetting.pngSo we get a new setting  for Bot ID.
  4. Previously when testing the bot locally, we need to put our AppId and AppSecret in the emulator. oldemulatorsetting.pngNow we don’t need to do this anymore. Just keep them empty like this:newemulator.pngOtherwise, you will get internal exception when typing a message.

There are other code change you may need to make when converting to V3 bot.

For example, my bot is using :

context.PerUserInConversationData.SetValue(“SomeState”, 0);

To save/access conversation state. Now I should use this instead:

context.ConversationData.SetValue(“SomeState”, 0);

Please check the Bot Framework offical documentation.They all have been updated for V3.

 

 

 

Get started with Microsoft Bot Framework and LUIS

Microsoft Bot Framework makes it much esier for developers to create our own bots.

A bot system developed using Bot Framework will look like this:(source)

bot_connector_diagram

Bot Connector mainly help us to handle the basic I/O like receiving and sending messages from/to users, connect to different channels which users directly interact with.  With help of Bot Connector, developers can focus on building the bot that has the actual logic about how to response to users.

When building the bot, one of the main challenges is understanding what the user wants. Normally, you need people to have expertise in areas like machine learning, nautural lanuges processing to work on this part. But now Microsoft provides Language Understanding Intelligent Service (LUIS) to help us do this hard work. It’s another great contribution from Microsoft to democratize advanced technology and empower developers.

Step1: Set up the development environemt

You can follow this offical tutorial to set up the development environment and create a Hello World bot.

http://docs.botframework.com/connector/getstarted/

After you have done this, you should have a simple Bot application running with Bot Framework Emulator. When you type something in the Emulator, you should get simple response from the bot.

Step2: Bot Builder SDK and Dialog

In order to make bot smarter and can handle more complex conversion with user, we need to use Bot Builder SDK. You can install it simply from Nuget Package Manager.

The core concept in Bot Builder SDK is Dialogs. Dialogs are abstraction of a conversation process. Each dialog encapsulates its own state in a C# class that implents IDialog. A conversaction state is then composed of a stack of active dialogs that is stored in the messages exhanged with the Bot Connector.

In order to understand this concept, you can follow the first two examples in the documentation:

http://docs.botframework.com/sdkreference/csharp/dialogs.html

The first example changes the code in the Bot Framework template (our hello word bot) to use dialogs from the Bot Builder. The second example, Echo Bot with State builds on that to add some simple state.

After you have done the first two examples, you should get basic understanding of Dialog. By adding more complex logic, you can have bot that is able to handle simple conversion with users.

Step3: Set up LUIS 

In the documentation above about Dialog, it provies a example of using LuisDialog to integrate LUIS service. I find the example is not so straightforward. So from this step, we will look at how to set up LUIS and integrate it into our bot.

You can follow this very good introducation video from Microsft to create a simple LUIS application called TravelAgent step by step.

Introduction to Language Understanding Intelligent Service (LUIS) – Microsoft Cognitive Services

After following the video , we should create the LUIS application TravelAgent.

luisApp

The key things we have created in this LUIS application are two intents (GetWeather and BookFlight) and three entities (Location and its two child entities FromLocation and ToLocation).

Step4: Integrate LUIS application with Bot using LuisDialog

LuisDialog is a dialog specialized to handle intents and entities from LUIS. What we need to do is binding our intents and entities in LuisDialog with our LUIS application.

  1. Bind our LuisDialog with LUIS application. We need to find the modelID and subscription key in LUIS application.

20160605121009

You can go to LUIS appllication, Click the Publish buton. The URL shown in the popup contains these two keys.

20160605121341

2. Bind the intent to the method using LuisIntent atribute.

In our LuisDialog, we create a method to handle each intent. The way to bind the method to LUIS intent is using LuisIntent attribute. The parameter of the attribute should be the exactly same intent name in LUIS application.

20160605121655

3. Use LUIS entity by enity name.

When using the LUIS entity, we just need to use the same entity name in LUIS application. Normally, you can create a const to store the entity name to in order to easily reuse and maintain it.

20160605122141

For full example code, check out on my github