Part 2 – Bot Framework

The recently released Bot Framework equips us with the basic plumbing that is required for chat sessions and making connections with services like LUIS. Some of the key features of Bot Builder SDK include

· Support for both C# and Node.js

· Open source on Github

· Conversation support – Prompts, Dialog and Rulesets for form flows

· Chat emulator – a client for testing

· Connector to Cognitive services like LUIS

Once you have the prerequisites discussed in the previous part, you can create a new bot project from Visual Studio by going

File > New > Project > Bot Application

The project setup is based on WebAPI / MVC style routing and you need to implement a message controller. Below is a screenshot of the source code for the bot

clip_image001

Handling messages

The main entry point of the bot framework is the MessagesController as shown below


[BotAuthentication]
public class MessagesController : ApiController
{
[ResponseType(typeof(void))]
public virtual async Task<HttpResponseMessage> Post([FromBody] Activity
activity)
{
// check if activity is of type message
if (activity != null && activity.GetActivityType() ==
ActivityTypes.Message)
{
await Conversation.SendAsync(activity, () => new InsuranceDialog());
}
else
{
HandleSystemMessage(activity);
}
return new HttpResponseMessage(System.Net.HttpStatusCode.Accepted);
}

The controller is secured by the BotAuthentication decoration that secures the bot’s endpoint, then we are checking the incoming message to ensure it is of type message and initiate a dialog called InsuranceDialog. The dialog then passes the message to LUIS to determine the customer’s intent and generates a reply accordingly. We will dig in more details of LUIS in the next blog.

Replies

Replies from the bots are posted back on the chat screen using some of the common methods described below

context.PostAsync("Hi there. Welcome to BestPrice.");

Above line shows how to post a basic message back to the user

PromptDialog.Choice<TypeOfInsuranceOptions>(context, 
ResumeTypeOptionsAsync, options, "Let us know what are you interested in?");

Here we are using a dialog class which not only posts a message with options but also listens to the user’s input i.e. the option they chose.

PromptDialog.Confirm(context, HandleInsuranceOptions,"Do you want to know about 
our insurance?"
,"Didn't get that!");

This is an example of a confirm message where we expect a Yes or No from the user

Using the Channel Emulator

One of most useful application for such projects is the Channel Framework Emulator which is a client you use to unit test your bots. It can connect to both online and locally deployed bot apps. You need to ensure that AppId and Secret you use in this app are the ones your bot app uses i.e. the ones in its web.config. Below is a screenshot of our bot being tested locally. Let us meet in the next blog post where we explore LUIS.

  clip_image003

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s