Beruflich Dokumente
Kultur Dokumente
Workshop
Abstract:
This Bot Builder SDK with LUIS Lab is prepared to show how to build a Bot with Bot Builder SDK
and add LUIS (Language Understanding Intelligent Service). Deploy the Bot in Bot Framework
developer portal and enable users to access the Bot via different channels (Skype, Facebook
Messenger, Slack, etc.).
You will need setup your dev machine as followed to complete the lab:
Windows 10
Visual Studio 2015 (Latest update)
Azure account with an active subscription
This document is provided "as-is." Information and views expressed in this document, including URL and other
Internet Web site references, may change without notice.
Contents
Lab Objectives........................................................................................................ 3
Section 1: Create a Bot with Bot Builder SDK ......................................................... 3
[Optional] Exercise 0: Setting up development environment.............................. 3
Exercise 1: Creating a C# Bot Application ............................................................ 4
Exercise 2: Adding Dialog .................................................................................... 6
Exercise 3: Adding FormFlow .............................................................................. 9
Exercise 4: Adding Bot State ............................................................................. 12
Section 2: Create a LUIS Application .................................................................... 16
Exercise 1: Creating your first LUIS application ................................................. 16
Exercise 2: Adding Intents ................................................................................. 18
Exercise 3: Adding Entities ................................................................................ 19
Using Bing Entities ................................................................................................................. 19
Exercise 4: Labeling utterances ......................................................................... 20
Exercise 5: Training and Publishing a model ..................................................... 23
Section 3: Smart Bots with LUIS ........................................................................... 25
Exercise 1: Bot Project with LuisDialog ............................................................. 25
Exercise 2: Handle Intents ................................................................................. 28
Exercise 3: Test bot with natural language input .............................................. 28
Exercise 4: Process Entities ............................................................................... 30
Section 4: Publish your Smart Bot ........................................................................ 31
Exercise 1: Publish project to Azure .................................................................. 32
Exercise 2: Register your Bot with Microsoft Bot Framework ........................... 36
Exercise 3: Update Bot MicrosoftAppId and MicrosoftAppPassword ................ 38
Exercise 4: Testing the connection to your bot ................................................. 39
Exercise 5: Configuring Channels ...................................................................... 40
[Optional] Exercise 6: Trying the Skype Channel ............................................... 42
Lab Objectives
This lab aims to demonstrate how to build a Bot with Microsoft Bot Framework. The framework
consists of the Bot Builder SDK, Bot Connector, Developer Portal, and Bot Directory. Theres
also an emulator that you can use to test your bot. First section will demonstrate how to build a
simple conversational bot using the Bot Builder SDK. The second and third sections will
demonstrate how to create a LUIS (language understanding intelligent services) application and
integrate with a Bot. Lastly, this lab will demonstrate how to host a Bot in the Developer Portal
and access the Bot using different channels.
2. In the New Project window, find the Bot Application under Installed > Templates >
Visual C#. Change the name to HotelSearchBot. Click OK.
3. The HotelSearchBot is created from the Bot Application template. This project is based
on .NET web application with Bot Builder SDK. The next step is to build and test the bot
app. Click the run button (Microsoft Edge) to build and start debugging the app.
4. Since the Bot Application is a web application, when the app is running we can debug
through the default browser. You will see a screen like the one below.
5. Open Bot Framework Channel Emulator tool. Make sure URL corresponds to the
endpoint in Edge. Leave the App ID and Password empty for now and click CONNECT.
6. The template is a fully functional Echo Bot that takes the user's text utterance as input
and returns it as output. Try typing in the emulator tool to test the bot. You will see a
screen like the one below.
Exercise 2: Adding Dialog
Dialogs model a conversational process, where the exchange of messages between bot and
user is the primary channel for interaction with the outside world. Each dialog is an abstraction
that encapsulates its own state in a C# class that implements IDialog.
In this example, we will implement a bot that will help users to find hotel. However, there are
many options HotelSearchBot needs to know from the user before it starts searching, such as
Check-in Date, Destination, Room, etc.
Here we use combination of two powerful conversation building blocks known as Dialog and
FormFlow, which give us flexible methods to create a dialog and get a collection of fields that
user provide.
1. Create a new C# class. Right-click on HotelSearchBot project > Add > Class
Enter HotelDialog.cs as the name of the class. Click Add to add the class.
2. We first need to import the required the namespace, add this at the top of
HotelDialog.cs:
using Microsoft.Bot.Connector;
using Microsoft.Bot.Builder.Dialogs;
using System.Threading.Tasks ;
4. Build and run the project. In the Bot Emulator, check the message is processed in the
HotelDialog class.
Exercise 3: Adding FormFlow
FormFlow dialog guides the user through filling in the form while providing guidance along the
way. First, we need to define a FormFlow class, HotelsQueryForm.cs.
1. Create a new C# class. Right-click on HotelSearchBot project > Add > Class
Enter HotelsQueryForm.cs as the name of the class. Click Add to add the class.
2. HotelsQueryForm.cs will define the information that we need from the User to search
for Hotel. In HotelsQueryForm.cs, copy the code below.
using System;
using Microsoft.Bot.Builder.FormFlow;
namespace HotelSearchBot
{
public enum RoomOptions
{
Single, Double, Junior, Queen
};
[Numeric(1, int.MaxValue)]
[Prompt("How many {&} do you want to stay?")]
public int Nights { get; set; }
}
}
3. Next, the HotelDialog needs to build the FormFlow when the user sends a message to
the bot. In HotelDialog.cs, we need to use a FormDialog.FromForm() function to build
the HotelsQueryForm FormDialog.
In HotelDialog.cs, update the MessageReceivedAsync function.
//Received message from user
public async Task MessageReceivedAsync(IDialogContext context,
IAwaitable<IMessageActivity> argument)
{
var message = await argument;
//Creates a FormDialog using FormDialog.FromForm(BuildFormDelegate<T>, ..)
//Calls the BuildHotelForm fuction
var hotelsFormDialog = FormDialog.FromForm(this.BuildHotelsForm,
FormOptions.PromptInStart);
//context.call will call the child dialog, in this case the hotelsFormDialog
//when the dialog complete, will continue to the ResumeAfterHotelsFormDialog
context.Call(hotelsFormDialog, this.ResumeAfterHotelsFormDialog);
}
Also, add the using statement to the top of the HotelDialog.cs:
using Microsoft.Bot.Builder.FormFlow;
- BuildHotelsForm() :
o Returns a FormFlow:IForm object from FormBuilder.Build() by specifying
the form class HotelsQueryForm
- ResumeAfterHotelsFormDialog() :
o This is called when the FormDialog<HotelsQueryForm> completes all its
dialogs.
o After receiving the users complete input, the bot can add feature such as
calling hotel booking 3rd party API.
5. HotelDialog.cs Dialog is fully integrated with the HotelsQueryForm form. Test this in
the Bot Emulator by building and run the app. You will see the result like the one below.
a. First, greet the Bot with any text, Hello or Hi or anything. Upon receiving the
first message, the Bot will start the HotelDialog dialog.
b. Enter any destination; such as, Taipei or Taichung, any city you can think of.
c. Click on the one of the buttons Single, Double, Junior, or Queen.
d. Enter a date in English spoken language, Jan 31st or Feb 2nd.
e. Finally, enter any digit number to represent number of nights.
Exercise 4: Adding Bot State
Now that we have an example of the Bot Builder framework we are going to build on it to add
some dialog state and some commands to control that state.
1. First thing we need to do is add a state variable in the dialog class, HotelDialog.
Add the hotelHistory variable in HotelDialog.cs
public class HotelDialog : IDialog<object> {
In AfterResetAsync we check on the response and perform the action including sending a
message back to the user.
No record response confirms that the dialog state variable has been cleared.
In this section, we have created a Bot using the FormFlow dialog from the Bot Builder SDK.
Section 2 & 3, we will be creating another Bot which will integrate with LUIS for natural
language understanding.
Section 2: Create a LUIS Application
Language Understanding Intelligent Services (LUIS) lets you build your applications by using the
LUIS web interface. A Bot with LUIS enabled will allow the bot to understand and respond to
various hypothetical commands.
LUIS is capable of interpreting a sentence and parsing out its intent and entities. An intent is
something a user want; like an action that they want to perform. An entity is the subject of the
intent. For example, if someone asks "What's the weather in Vancouver? the intent could be
check weather and the entity is Vancouver. In the application, you will bundle together the
intents and entities that are important to your task.
Click on +New App then new Application. You will see a screen like the one below.
Fill in the appropriate fields. Enter application name, WeatherBot and select the other fields
as the screen above and as preferred.
Finally click on Add App to create LUIS application. You will see the LUIS Application Editor,
like the one below.
Exercise 2: Adding Intents
Next, we will add two intents to the application. At the top left of the menu panel, you will see
an area for intents. All applications come with one pre-defined intent, None. This will recognize
user statements that have nothing to do with the application, for example if someone says,
"Get me a great cookie recipe".
Go ahead and click + next to Intents on the horizontal bar. You will see a dialog box appear to
add a new intent. Enter the intent name of "GetWeather", and the example command that
triggers the intent as "Whats the weather in Taipei". This will look like the screenshot below.
The next step is to deploy the model to an HTTP endpoint that will interpret the sentences we
send it. Click the Publish button in the upper left-hand corner, and then Publish web service in
the resulting window. After a couple of moments, you will see the URL that makes your model
available as a web service. You will see a screen like the one below.
The next step is to test your Bot Application and understand the JSON response.
1. In the Timezone drop-down box select (GMT +8:00) Beijing, Perth, Singapore, Hong
Kong.
2. In the Query textbox, enter Show me tomorrows weather in Shanghai, then click
Update published application.
3. Click on the generated URL. (https://api.projectoxford.ai/luis/v2.0/apps/.... )
4. You should see the output:
{
"query": "Show me tomorrow's weather in Shanghai",
"topScoringIntent": {
"intent": "GetWeather",
"score": 0.941986561
},
"intents": [
{
"intent": " Show me tomorrow's weather in Shanghai ",
"score": 0.941986561
},
{
"intent": "None",
"score": 0.04360123
},
{
"intent": "GetSunsetTime",
"score": 4.42665238E-09
}
],
"entities": [
{
"entity": "shanghai",
"type": "Location",
"startIndex": 30,
"endIndex": 37,
"score": 0.711348057
},
{
"entity": "tomorrow",
"type": "builtin.datetime.date",
"startIndex": 8,
"endIndex": 15,
"resolution": {
"date": "2016-12-20"
}
}
]
}
5. From the output we can retrieve the following detail:
"date" string "2016-12-20" [Note: the URL request was sent on 2016-12-19]
3. Create a new C# class. Right-click on WeatherLUISBot project > Add > Class...
Enter WeatherDialog.cs as the name of the class. Click Add to add the class.
4. In WeatherDialog.cs, replace the code with:
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Bot.Builder.Luis.Models;
using Microsoft.Bot.Builder.Luis;
using Microsoft.Bot.Builder.Dialogs;
using System.Threading.Tasks;
namespace WeatherLUISBot
{
[LuisModel("<YOUR_LUIS_APP_ID>", "<YOUR_LUIS_SUBSCRIPTION_KEY>")]
[Serializable]
public class WeatherDialog : LuisDialog<object>
{
//Const string to retrieve Entity based on entity type
public const string Entity_location = "Location";
public const string Entity_builtInDate = "builtin.datetime.date";
public const string Entity_builtInTime = "builtin.datetime.time";
5. Go back to the browser and navigate to Luis.ai. On the top left of the menu panel, click
on Publish and you will see a screen like the one below.
2. Next step is to route user input to the Luis Dialog. To do this you need to change the
code in the Post method in MessagesController to create an instance of our LUIS dialog
and send incoming messages to it. The code below creates an instance of the LuisDialog
(WeatherDialog) and Conversation.SendAsync method will process the incoming message
and pass it along to the LuisDialog.
In Controllers > MessageController.cs change the following code in Post method.
public async Task<HttpResponseMessage> Post([FromBody]Activity activity)
{
if (activity.Type == ActivityTypes.Message)
{
await Conversation.SendAsync(activity, () => new WeatherDialog());
}
else
{
HandleSystemMessage(activity);
}
var response = Request.CreateResponse(HttpStatusCode.OK);
return response;
}
3. Open Bot Framework Channel Emulator tool and try several phrases to check the LUIS
model is consistent.
- None: Hello
- GetWeather: Whats the weather like in Taipei
- GetSunsetTime: Please give me the sunset for tonight
The response from the Bot should be correctly recognized from the LUIS modal and
returned back to the user. You can rephrase each case in natural language input.
Exercise 4: Process Entities
Now that the bot understands what the users intended action is, the bot can also retrieve
entities to handle each intent.
In this example, the GetWeather intent might include the Location entity and Datetime
entity. In WeatherDialog.cs change the following code in GetWeather method.
public async Task GetWeather(IDialogContext context, LuisResult result)
{
//initialize Entity objects
EntityRecommendation locationEntity;
EntityRecommendation dateEntity;
EntityRecommendation timeEntity;
if (ifDate || ifTime)
{
//if date or time entity exists
if (ifDate) message += " " + dateEntity.Entity;
if (ifTime) message += " " + timeEntity.Entity;
message += "'s weather";
} else
{
message += "the weather";
}
if (ifLocation)
{
//if Location entity exists
message += " in " + locationEntity.Entity;
}
Open Bot Framework Channel Emulator tool and try the following phrases:
- Will it snow tomorrow in Vancouver?
- Is it going to rain next week?
- Weather for dec 31st in Taipei
You should see the responses like the ones below.
Intent handlers will include any entities that LUIS recognized and passed along in the args
parameter, LuisResult result. Bot Builder includes a LuisResult class, which has useful
functions and includes the following objects,
- string query
- IList<IntentRecommendation> intents
- IList<EntityRecommendation> entities
In this example, we built a weather bot that is able to understand and respond to various
hypothetical commands. The bot is using LUIS to identify the intent of the user, and reply with
the appropriate prompt. To complete this app, you may wish to hook live weather data in the
C# Bot application; when the user asks for the weather, the Bot can retrieve live weather data
from 3rd party APIs.
5. On the top right of the page, click on the button to open the Notifications panel.
Confirm the Deployment is successful. Click on the Deployments succeeded to open
the web app blade.
6. In the Web App blade, download the publish profile. Publish profiles allow you to
import the deployment credentials into Visual Studio; allowing Visual Studio to deploy
project to this Azure Web App.
Save the file somewhere on the development machine.
7. Back in Visual Studio, right click on the WeatherLUISBot project, and click Publish.
Click on Import. In the Import Publish Settings, click on Browser and go to the publish
profile file. Finally, click OK.
8. By importing the Publish Profile file, Visual Studio can retrieve all the deployment
credentials and settings. Click on Next.
9. Finally, click on Publish.
10. You will see a number of messages displayed in the Visual Studio 2015 "Output"
window.
Once publishing is complete, you will also see the web page for your Bot Application
displayed in your browser, see below.
5. A popup dialog will appear. Copy the password and save it somewhere for later.
6. Back in register bot page, the app ID should be filled in automatically. You can click on
Register at the bottom of the page. Once the bot is created successfully, you will see the
Bot created message. Click on OK.
You can find the BotId, MicrosoftAppId, MicrosoftAppPassword in the developer dashboard for
your bot. In My bots page, the top left section will display the related details of your bot.
Fill in the BotId as Bot handle, and MicrosoftAppId as Microsoft App ID, and
MicrosoftAppPassword as the password you saved in previous exercise.
Build and re-publish your bot to Azure. You can re-publish by right-clicking on the project and
click Publish.
The full embedded code (<iframe>) can be added to any html page or web page
to embed the Bot into any website.
4. That's the end of configuration - your Bot is ready for your users.
Publishing your bot to the Bot Directory will allow everyone to find your bot in the Bot
Directory. Make sure you take a look at our review guidelines before submitting. You may
try this on your own time.