Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

Bot Development: Step by step guide prepared by

Mr. Junaid Aziz

Getting started with the Connector


The Microsoft Bot Connector is a communication service that helps you connect your Bot with
many different communication channels (Skype, SMS, email, and others). If you write a
conversational Bot or agent and expose a Microsoft Bot Framework-compatible API on the
Internet, the Bot Framework Connector service will forward messages from your Bot to a user, and
will send user messages back to your Bot.

To use the Microsoft Bot Framework Connector, you must have:

1. Microsoft Account (Hotmail, Live, Outlook.com) to log into the Bot Framework developer
portal, which you will use to register your Bot.
2. An Azure-accessible REST endpoint exposing a callback for the Connector service.
3. Developer accounts on one or more communication services (such as Skype) where your
Bot will communicate.

In addition you may wish to have an Azure App Insights account so you can capture telemetry from
your Bot. There are different ways to go about building a Bot; from scratch, coded directly to the
Bot Connector API, the Bot Builder SDK's for Node.JS & .NET, and the Bot Connector .NET
template which is what this QuickStart guide demonstrates .

Getting started in .NET


This is a step-by-step guide to writing an Bot in C# using the Bot Framework Connector SDK .NET
template.

1. Install prerequisite software


o Visual Studio 2015 (latest update) - you can download the community version here
for free: www.visualstudio.com
o Important: Please update all VS extensions to their latest versions Tools->Extensions
and Updates->Updates
2. Download and install the Bot Application template
o Download the file from the direct download link: http://aka.ms/bf-bc-vstemplate)
o Save the zip file to your Visual Studio 2015 templates directory which is traditionally
in "%USERPROFILE%\Documents\Visual Studio
2015\Templates\ProjectTemplates\Visual C#\"
3. Open Visual Studio
4. Create a new C# project using the new Bot Application template.
5. The template is a fully functional Echo Bot that takes the user's text utterance as input and
returns it as output. In order to run however,
o The bot has to be registered with Bot Connector
o The AppId and AppPassword from the Bot Framework registration page have to be
recorded in the project's web.config
o The project needs to be published to the web

Building your Bot


The core functionality of the Bot Template is all in the Post function within
Controllers\MessagesController.cs. In this case the code takes the message text for the user, then
creates a reply message using the CreateReplyMessage function. The BotAuthentication
decoration on the method is used to validate your Bot Connector credentials over HTTPS. Replace
the post method code with the following:-

public async Task<HttpResponseMessage> Post([FromBody]Activity activity)


{

if (activity.Type == ActivityTypes.Message)
{

ConnectorClient connector = new ConnectorClient(new Uri(activity.ServiceUrl));

// calculate something for us to return


// int length = (activity.Text ?? string.Empty).Length;

switch (activity.Text.ToUpper())
{
case "PIZZA":
{
// return our reply to the user

Activity reply = activity.CreateReply("You ordered PIZZA");


await connector.Conversations.ReplyToActivityAsync(reply);
Activity reply1 = activity.CreateReply("Hint - Type checkout to finish order and exit");
await connector.Conversations.ReplyToActivityAsync(reply1);
break;
}

case "TOPPING":
{

Activity reply = activity.CreateReply("You ordered Toppings");


await connector.Conversations.ReplyToActivityAsync(reply);
Activity reply1 = activity.CreateReply("Hint - Type checkout to finish order and exit");
await connector.Conversations.ReplyToActivityAsync(reply1);
break;
}

case "CHECKOUT":
{
Activity reply = activity.CreateReply("Thank you for the order. Good bye");
await connector.Conversations.ReplyToActivityAsync(reply);

break;
}

default:

Activity reply = activity.CreateReply("Welcome to LibertyPizza");


await connector.Conversations.ReplyToActivityAsync(reply);
Activity reply1 = activity.CreateReply("Type pizza to order pizza");
await connector.Conversations.ReplyToActivityAsync(reply1);
Activity reply2 = activity.CreateReply("Type topping to add cheez topping on it");
await connector.Conversations.ReplyToActivityAsync(reply2);
Activity reply3 = activity.CreateReply("Type checkout to finish order and exit");
await connector.Conversations.ReplyToActivityAsync(reply3);
break;
}

}
else
{
HandleSystemMessage(activity);
}
var response = Request.CreateResponse(HttpStatusCode.OK);
return response;
}

Emulator
Use the Bot Framework Emulator to test your Bot application
The Bot Framework provides a channel emulator that lets you test calls to your Bot as if it were
being called by the Bot Framework cloud service. To install the Bot Framework Emulator,
download it from https://aka.ms/bf-bc-emulator.

When the application is built and deployed the web browser will open and display the application
Default.htm file (which is part of the Bot Application project). Feel free to modify the Default.html
file to match the name and description of your Bot Application.

Here's the Bot Application Default.htm file in Google Chrome

When using the emulator to test your Bot application, make note of the port that the application is
running on, which in this example is port 3978. You will need this information to run the Bot
Framework Emulator.

Now open the Bot Framework Emulator. There are a few items that you will need to configure in
the tool before you can interact with your Bot Application.

When working with the emulator with a bot running locally, you need:

 The Url for your bot set the localhost:<port> pulled from the last step. > Note: will need to
add the path "/api/messages" to your URL when using the Bot Application template.
 Empty out the MicrosoftAppId field
 Empty out the MicrosoftAppPassword field

This will only work with the emulator running locally; in the cloud you would instead have to specify
the appropriate URL and authentication values. For more about the emulator, read here.

Now that everything is configured you can interact with your service. The bottom of the Bot
Framework Emulator application has a Text Box that you can use to enter a message to interact
with the bot you just created.
If we take a look at the code in the Bot Application that was generated by the Visual Studio 2015
Bot Application Template, specifically the file called MessageController.cs we can see how the
message entered by a user is converted into the reply Activity.

Congratulations, you just finished building your first bot.

You might also like