Build A BOT Using Bot Builder SDK With .NET


In this article I will explain about creating a bot using Bot builder SDK with .NET, The bot builder is an open source SDK with support of .NET ,REST, Node.JS, etc. Bots can be developed using visual studio by managing the NUGET packages and bot builder SDK, the following article will help to build a bot and test with the bot framework emulator.


  • Visual Studio 2017.
  • Bot framework emulator.

Steps to be followed to create a bot

Step 1

Open Visual Studio 2017.

Step 2

Click->new->visual c#->Bot application->provide a name for the bot and click->create.

Bot application 
Bot application 

Step 3

The bot application will be created using the Bot application template, the application will contain all the requirements that are needed to build an bot service. Now we need to update it to the latest version of SDK, using NUGET packages.

Right click->project-> click->Manage NUGET packages.

Switch->Browse tab-> and search for Microsoft.Bot.Builder package and update the package to the latest version of it.

Microsoft.Bot.Builder package 
Microsoft.Bot.Builder package 

Wait till the package gets updated and the project contains all the requirements for building a bot so that we need not write any codings.

Step 4

select->controllers\MessagesController.CS in the right side of the solution explorer and the bot requirements will be available as described below 

  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.RootDialog());  
  10.         } else {  
  11.             HandleSystemMessage(activity);  
  12.         }  
  13.         var response = Request.CreateResponse(HttpStatusCode.OK);  
  14.         return response;  
  15.     }...  
  16. }   

Step 5

The message generation and response generation will be processed by root dialog with using the MessageReceiverdAsync function in dialog\rootdialog.cs, it replies back to the user in texts representing by number of characters in the message. The rootdialog.cs will be coded like described below. 

  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. }   

Step 6

Now we can run bot service using the browser and click->run button in visual studio, after running the bot service using the browser, the application will deploy with localhost default.htm page.

run bot service 

 run bot service

Step 7

The localhost default.htm page is displayed so that now we can start the Bot framework emulator and connect the bot with it. Before running with bot emulator the bot is running locally in the browser.

Step 8

Type http://localhost:port-number/api/messages in the address bar, so that the port number matches with the port number where the bot is running

Step 9

After typing the link in the address bar, just Click->connect. The App ID and the APP password will be obtained if you register the bot with bot framework. By clicking connect button the bot which is running locally will be connected with the emulator and we can test the bot by sending messages to it. The response that is generated will be echoed back to the user. Such as with the total number of characters that we sent.

bot emulator 
bot emulator 

Now we created a bot using the Bot application template and with Bot builder SDK for .NET, Thanks for reading have a nice day.

Similar Articles