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 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:
- First of all go to File, New, then Project or press CTRL +SHIFT + N.
- 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:
- using System;
- using System.Linq;
- using System.Net;
- using System.Net.Http;
- using System.Threading.Tasks;
- using System.Web.Http;
- using System.Web.Http.Description;
- using Microsoft.Bot.Connector;
- using Microsoft.Bot.Connector.Utilities;
- using Newtonsoft.Json;
-
- namespace MySimpleBotApp
- {
- [BotAuthentication]
- public class MessagesController : ApiController
- {
-
-
-
-
- public async Task<Message> Post([FromBody]Message message)
- {
- if (message.Type == "Message")
- {
-
- int length = (message.Text ?? string.Empty).Length;
-
-
- return message.CreateReplyMessage($"You sent {length} characters");
- }
- else
- {
- return HandleSystemMessage(message);
- }
- }
-
- private Message HandleSystemMessage(Message message)
- {
- if (message.Type == "Ping")
- {
- Message reply = message.CreateReplyMessage();
- reply.Type = "Ping";
- return reply;
- }
- else if (message.Type == "DeleteUserData")
- {
-
-
- }
- else if (message.Type == "BotAddedToConversation")
- {
- }
- else if (message.Type == "BotRemovedFromConversation")
- {
- }
- else if (message.Type == "UserAddedToConversation")
- {
- }
- else if (message.Type == "UserRemovedFromConversation")
- {
- }
- else if (message.Type == "EndOfConversation")
- {
- }
-
- return null;
- }
- }
- }
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: