Quick start - Development Of Chat Bot Using Microsoft Bot Framework - For Beginners

In this article, we are going to see what all things we require before starting with development of bot applications using Microsoft bot framework.

Prerequisite

  • Download Visual Studio 2015/2017 and update all extensions.
  • Download and install bot application template from here.
  • Download Bot Framework Emulator. The emulator is a desktop application that lets you test a bot application on localhost or running remotely.

Create bot application

Open Visual Studio and create a new C# project. Choose the Bot Application template. If required, also update NuGet Packages installed in project.

Microsoft bot framework

Microsoft bot framework

Template contains all components required to build a bot as well as initial code setup.

Code

In solution explorer, you will see WebApiConfig.cs file, Controllers and Dialogs folders. 'MessageController' is inherited from 'System.Web.Http.ApiController'. Bot application is nothing but webapis which will be called from chat window to get response.

Microsoft bot framework

When user is going to enter query in chat window, 'post( )' method within controller will get called. Then controller will invoke specified dialog (in our case, its RootDialog), which will process the query and return the result.

MessageController.cs

  1. [BotAuthentication]  
  2. public class MessagesController: ApiController {  
  3.     /// <summary>  
  4.     /// POST: api/Messages  
  5.     /// Receive a message from a user and reply to it  
  6.     /// </summary>  
  7.     public async Task < HttpResponseMessage > Post([FromBody] Activity activity) {  
  8.         if (activity.Type == ActivityTypes.Message) {  
  9.             await Conversation.SendAsync(activity, () => new Dialogs.AnswerDialog());  
  10.         } else {  
  11.             HandleSystemMessage(activity);  
  12.         }  
  13.         var response = Request.CreateResponse(HttpStatusCode.OK);  
  14.         return response;  
  15.     }  
  16.     private Activity HandleSystemMessage(Activity message) { ...  
  17.     }  
  18. }  

RootDialog.cs

  1. [Serializable]  
  2. public class RootDialog: IDialog < object > {  
  3.     public Task StartAsync(IDialogContext context) {  
  4.         context.Wait(MessageReceivedAsync);  
  5.         return Task.CompletedTask;  
  6.     }  
  7.     private async Task MessageReceivedAsync(IDialogContext context, IAwaitable < object > result) {  
  8.         var activity = await result as Activity;  
  9.         // calculate something for us to return  
  10.         int length = (activity.Text ? ? string.Empty).Length;  
  11.         // return our reply to the user  
  12.         await context.PostAsync($ "You sent {activity.Text} which was {length} characters");  
  13.         context.Wait(MessageReceivedAsync);  
  14.     }  
  15. }  

When controller invokes RootDialog, control will come to 'StartAsync( )' method of dialog. It will invoke MessageReceivedAsync ( )' method which will return number of characters in text entered by user.

Test

Hit F5 to test a bot application. It will host application with IIS Express and open browser. To understand the control flow, insert breakpoints in Post( ) method of MessageController and StartAsync( ) method of RootDialog.

Microsoft bot framework

Now launch bot emulator app. Put URL as 'http://localhost:{{port no. from browser url}}/api/{{controller name}}' to connect emulator with bot application. Keep App ID and App Password blank, click on connect.

Microsoft bot framework

Start chatting with bot and bot will reply with number of characters in your text. Happy chatting! :)

Microsoft bot framework

Up Next
    Ebook Download
    View all
    Learn
    View all