Cookie CSS

Saturday, October 29, 2016

Building a Bot with Microsoft Bot Framework - Part I

The Microsoft Bot Framework is a set a tools that allows you to provide conversation experience to users.  The conversations can interact with users to provide technical support, create orders, or disseminate other data of your choosing.  The framework allows your bot to interact in a number of different ways out of the box, like text/sms, Skype, Slack, Facebook Messenger, Kik, Office 365 mail, and other services.  You can develop various different types of bots.  They can be simple text conversations.  They can provide dialogs for forms to the users, which give them a nested menu of choices to select.  They can also integrate to LUIS which can provide clould based AI dialogs based on the most likely result the users is trying to find.

There are several pieces to this framework we will need to understand before we can build our bot.

Bot Builder SDK

The Bot Builder SDK is available on GitHub.  It provides samples of different dialogs that can be used with Node.js or .NET which is what I will be using.

Bot Framework Developer Portal

The Bot Framework Developer Portal lets you connect your bot(s) seamlessly text/sms to Skype, Slack, Facebook Messenger, Kik, Office 365 mail and other popular services. Simply register your bot, configure desired channels and publish in the Bot Directory. All bots registered with Bot Framework are auto-configured to work with Skype and the Web.

Bot Directory

The Bot Directory is a public directory of all reviewed bots registered through the Developer Portal. Users will be able to discover, try, and add bots to their favorite conversation experiences from the Bot Directory.  If you want users to be able to download and integrate their bot into apps like Skype, this is where you will list your bot.

Getting Started with .NET

Before we begin development there are a couple of things we will need to get started.  First I would recommend you download and install the Bot Framework Channel Emulator which will let you test and debug your application.  Next you want to get the Connector for visual studio.

Once you have these 2 things in place you should be able to create a new blank application using the Bot Application Template.

Create a new C\# project using the new %Bot Application template.


You should get some base code that looks like this.

[BotAuthentication]
public class MessagesController : ApiController
{
<summary>
POST: api/Messages
Receive a message from a user and reply to it
</summary>
public async Task<HttpResponseMessage> Post([FromBody]Activity activity)
{
ConnectorClient connector = new ConnectorClient(new Uri(activity.ServiceUrl));
if (activity.Type == ActivityTypes.Message)
{
// calculate something for us to return
int length = (activity.Text ?? string.Empty).Length;
// return our reply to the user
Activity reply = activity.CreateReply($"You sent {activity.Text} which was {length} characters");
await connector.Conversations.ReplyToActivityAsync(reply);
}
else
{
HandleSystemMessage(activity);
}
var response = Request.CreateResponse(HttpStatusCode.OK);
return response;
}
}

Next week will dive in and begin examining different ways to interact with our users.


Doug Routledge, C# Lync, Skype for Business, SQL, Exchange, UC, 
Full Stack Developer  BridgeOC Bridge Operator Console
Twitter - @droutledge @ndbridge