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.
- using System.Net;
- using System.Net.Http;
- using System.Threading.Tasks;
- using System.Web.Http;
- using Microsoft.Bot.Builder.Dialogs;
- using Microsoft.Bot.Connector;
-
- namespace BotMessages
- {
- [BotAuthentication]
- public class MessagesController : ApiController
- {
-
-
-
-
- public async Task<HttpResponseMessage> Post([FromBody]Activity activity)
- {
- if (activity.Type == ActivityTypes.Message)
- {
- await Conversation.SendAsync(activity, () => new Dialogs.RootDialog());
- }
- else
- {
- HandleSystemMessage(activity);
- }
- var response = Request.CreateResponse(HttpStatusCode.OK);
- return response;
- }
-
- private Activity HandleSystemMessage(Activity message)
- {
- if (message.Type == ActivityTypes.DeleteUserData)
- {
-
-
- }
- else if (message.Type == ActivityTypes.ConversationUpdate)
- {
-
-
-
- }
- else if (message.Type == ActivityTypes.ContactRelationUpdate)
- {
-
-
- }
- else if (message.Type == ActivityTypes.Typing)
- {
-
- }
- else if (message.Type == ActivityTypes.Ping)
- {
- }
-
- return null;
- }
- }
- }
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.