Microsoft Bot Framework- How To Create a Bot

What is a Bot?

The Microsoft Bot Framework is a collection of Services and APIs. Bot allows the developers to build high-quality Bots for their users by using existing and well-known conversational platforms. You can create Bots to interact with your users naturally wherever they are including Facebook, text, skype and other popular Services.

Getting started – Its all in Web API

Bot Builder is an open source SDK hosted on GitHub, which provides the developers with the basic building blocks for writing BOTs.

Follow the steps given below to build your first Bot

Step 1

Download Bot Application template from this download link here.

Step 2

Save the .zip file in the templates directory (located at: %USERPROFILE%\Documents\Visual Studio 2015\Templates\ProjectTemplates\Visual C# ).

Step 3

Open Visual Studio 2015 latest version. Open new project.



Select Visual C# from Templates and click Bot Application. Enter the name, which you want to create, followed by clicking OK button.



Step 4

Right click on Solution Explorer and add NuGet packages.



Search Microsoft.Bot.Builder and update the package.

Step 5

Open MessageController under Controllers folder.
  1. using System.Net;  
  2. using System.Net.Http;  
  3. using System.Threading.Tasks;  
  4. using System.Web.Http;  
  5. using Microsoft.Bot.Builder.Dialogs;  
  6. using Microsoft.Bot.Connector;  
  7.   
  8. namespace BotMessages  
  9. {  
  10.     [BotAuthentication]  
  11.     public class MessagesController : ApiController  
  12.     {         
  13.         /// <summary>  
  14.         /// POST: api/Messages  
  15.         /// Receive a message from a user and reply to it  
  16.         /// </summary>  
  17.         public async Task<HttpResponseMessage> Post([FromBody]Activity activity)  
  18.         {  
  19.             if (activity.Type == ActivityTypes.Message)  
  20.             {  
  21.                 await Conversation.SendAsync(activity, () => new Dialogs.RootDialog());  
  22.             }  
  23.             else  
  24.             {  
  25.                 HandleSystemMessage(activity);  
  26.             }  
  27.             var response = Request.CreateResponse(HttpStatusCode.OK);  
  28.             return response;  
  29.         }  
  30.   
  31.         private Activity HandleSystemMessage(Activity message)  
  32.         {  
  33.             if (message.Type == ActivityTypes.DeleteUserData)  
  34.             {  
  35.                 // Implement user deletion here  
  36.                 // If we handle user deletion, return a real message  
  37.             }  
  38.             else if (message.Type == ActivityTypes.ConversationUpdate)  
  39.             {  
  40.                 // Handle conversation state changes, like members being added and removed  
  41.                 // Use Activity.MembersAdded and Activity.MembersRemoved and Activity.Action for info  
  42.                 // Not available in all channels  
  43.             }  
  44.             else if (message.Type == ActivityTypes.ContactRelationUpdate)  
  45.             {  
  46.                 // Handle add/remove from contact lists  
  47.                 // Activity.From + Activity.Action represent what happened  
  48.             }  
  49.             else if (message.Type == ActivityTypes.Typing)  
  50.             {  
  51.                 // Handle knowing tha the user is typing  
  52.             }  
  53.             else if (message.Type == ActivityTypes.Ping)  
  54.             {  
  55.             }  
  56.   
  57.             return null;  
  58.         }  
  59.     }  
  60. }  
Here, Post method represents the functionality of your Bot. This method is used to get the message from user and create reply back to the user. The [BotAuthentication] decoration on the method validates your Bot Connector credentials over HTTPS.

Step 6

Now download and install the Bot Framework Emulator.

Step 7

Now debug your Application.


Step 8

Open Emulator and connect your Bot, as shown below.



Step 9

Paste here your Bot debug URL (http://localhost:port number) + "/api/messages" and hit enter.



Step 10

Now Bot is ready. Here, an end user will send the message, as shown below.



Post method will be called into MessageController, which is responsible for Bot response and check activity type is 'message', it will return pre defined result.

font



Bot response is given below.



Conclusion

In this article, I have explained what Bot is and how to build a Bot Application templete. I have used Bot Framework Emulator to execute Bot Application.


Similar Articles