Creating A Simple Bot Application Using Microsoft Bot Framework

Introduction

 
After hearing news from Mr. Amit Diwan that Microsoft Announces Bot Framework For Developers, I learned how we can develop our own simple Bot using Microsoft Bot Framework. So in this article, we will learn how to create a very simple Bot application using Microsoft Bot Framework.
 
What is Microsoft BOT Framework
 
According to dev.botframework.com, The Bot Framework is built to be intelligent and learn from user interaction. User interaction can be possible via Chatting or Text/SMS to Skype, Mail, Telegram, SMS, etc. If you want to learn more about it, you can learn from the following link.
How to build your own simple Bot Application: The following steps will explain to you how to build your own simple Bot Application using Microsoft Bot Framework.
 
Step 1: Firstly, you have to install some prerequisite software,
  • Visual Studio 2015 Update 1. 
  • Also Update your all Visual Studio extensions through tools > Extensions and Updates > Updates.
Step 2: Download the Bot Application Template from the following link: Bot Application Template
 
Step 3:
 
The above link will download a ZIP File, just save that ZIP file in “%USERPROFILE%\Documents\Visual Studio 2015\Templates\ProjectTemplates\Visual C#"
 
Step 4: Now open your Visual Studio. 
 
Step 5: Now create a new Bot project in Visual Studio. To create a new Bot Project, here are the steps:
  1. First of all go to File, New, then Project or press CTRL +SHIFT + N.
  2. Now one dialog box will open. From that select Visual C# Project and you will find an option Bot Application, choose that and give the name of the project.
     
Step 6: After creating a project, you will get some files in solution explorer as you can see in the following figure.
 
 
Step 7: 
 
Open Web.Config File, and give the AppId and AppSecret keys for your project. If you are running this project locally, then you can give any AppId and AppSecret. Here's the figure,
 
 
Note:
 
For the local system, you can provide AppID and AppSecret. But if you host your bot somewhere then you have to register your bot here. And after registration, you will get your bot AppID and AppSecret.
 
Step 8: If you see your web.config then you will get the default document file that will run when your application will run.
 
 
Step 9: If you run your project then the output will look like the following:
 
 
The above output is nothing but your default document code that is written in "default.htm" File
 
Step 10: Now if you expand your Controllers folder you will get only one Controller File that is "MessagesController", so open MessagesController.
 
 
When you open MessagesController, the following code is written by default:
  1. using System;  
  2. using System.Linq;  
  3. using System.Net;  
  4. using System.Net.Http;  
  5. using System.Threading.Tasks;  
  6. using System.Web.Http;  
  7. using System.Web.Http.Description;  
  8. using Microsoft.Bot.Connector;  
  9. using Microsoft.Bot.Connector.Utilities;  
  10. using Newtonsoft.Json;  
  11.   
  12. namespace MySimpleBotApp  
  13. {  
  14.     [BotAuthentication]  
  15.     public class MessagesController : ApiController  
  16.     {  
  17.         /// <summary>  
  18.         /// POST: api/Messages  
  19.         /// Receive a message from a user and reply to it  
  20.         /// </summary>  
  21.         public async Task<Message> Post([FromBody]Message message)  
  22.         {  
  23.             if (message.Type == "Message")  
  24.             {  
  25.                 // calculate something for us to return  
  26.                 int length = (message.Text ?? string.Empty).Length;  
  27.   
  28.                 // return our reply to the user  
  29.                 return message.CreateReplyMessage($"You sent {length} characters");  
  30.             }  
  31.             else  
  32.             {  
  33.                 return HandleSystemMessage(message);  
  34.             }  
  35.         }  
  36.   
  37.         private Message HandleSystemMessage(Message message)  
  38.         {  
  39.             if (message.Type == "Ping")  
  40.             {  
  41.                 Message reply = message.CreateReplyMessage();  
  42.                 reply.Type = "Ping";  
  43.                 return reply;  
  44.             }  
  45.             else if (message.Type == "DeleteUserData")  
  46.             {  
  47.                 // Implement user deletion here  
  48.                 // If we handle user deletion, return a real message  
  49.             }  
  50.             else if (message.Type == "BotAddedToConversation")  
  51.             {  
  52.             }  
  53.             else if (message.Type == "BotRemovedFromConversation")  
  54.             {  
  55.             }  
  56.             else if (message.Type == "UserAddedToConversation")  
  57.             {  
  58.             }  
  59.             else if (message.Type == "UserRemovedFromConversation")  
  60.             {  
  61.             }  
  62.             else if (message.Type == "EndOfConversation")  
  63.             {  
  64.             }  
  65.   
  66.             return null;  
  67.         }  
  68.     }  
  69. }  
Step 11: For a simple example, I am modifying some code in Post Action:
 
 
Step 12: 
 
And you can see there is one more Action Method that is HandleSystemMessage, where I am changing in BotAddedToConversation condition. Changes are as follows:
 
 
Step 11: When you will run your application again the same default page will open. So once again run your application.
 
 
Step 12: 
 
Now to emulate your application you have to open the Bot Framework Emulator. You can download Bot Framework Emulator from the following link.
 
 
Step 13: After providing AppId And AppSecret, you can Send Request for Bot Added To Conversation. 
 
 
Step 14 :
 
After sending the request of Bot Added To Conversation Our Message will return which we have written for BotAddedToConversation in HandleSystemMessage action. 
 
 
Step 15: Now send a message, so post action will call and whatever you are returning from Post Action you have written what will show.
 
 
After Sending the message
 
 
Read more articles on Machine Learning:


Similar Articles