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. namespace BotMessages
  8. {
  9. [BotAuthentication]
  10. public class MessagesController : ApiController
  11. {
  12. /// <summary>
  13. /// POST: api/Messages
  14. /// Receive a message from a user and reply to it
  15. /// </summary>
  16. public async Task<HttpResponseMessage> Post([FromBody]Activity activity)
  17. {
  18. if (activity.Type == ActivityTypes.Message)
  19. {
  20. await Conversation.SendAsync(activity, () => new Dialogs.RootDialog());
  21. }
  22. else
  23. {
  24. HandleSystemMessage(activity);
  25. }
  26. var response = Request.CreateResponse(HttpStatusCode.OK);
  27. return response;
  28. }
  29. private Activity HandleSystemMessage(Activity message)
  30. {
  31. if (message.Type == ActivityTypes.DeleteUserData)
  32. {
  33. // Implement user deletion here
  34. // If we handle user deletion, return a real message
  35. }
  36. else if (message.Type == ActivityTypes.ConversationUpdate)
  37. {
  38. // Handle conversation state changes, like members being added and removed
  39. // Use Activity.MembersAdded and Activity.MembersRemoved and Activity.Action for info
  40. // Not available in all channels
  41. }
  42. else if (message.Type == ActivityTypes.ContactRelationUpdate)
  43. {
  44. // Handle add/remove from contact lists
  45. // Activity.From + Activity.Action represent what happened
  46. }
  47. else if (message.Type == ActivityTypes.Typing)
  48. {
  49. // Handle knowing tha the user is typing
  50. }
  51. else if (message.Type == ActivityTypes.Ping)
  52. {
  53. }
  54. return null;
  55. }
  56. }
  57. }
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.