Introduction
In this article I will explain about creating a bot using Bot builder SDK with .NET, The bot builder is an open source SDK with support of .NET ,REST, Node.JS, etc. Bots can be developed using visual studio by managing the NUGET packages and bot builder SDK for.net, the following article will help to build a bot and test with the bot framework emulator.
Prerequisites
- Visual Studio 2017.
- Bot framework emulator.
Steps to be followed to create a bot
Step 1
Open Visual Studio 2017.
Step 2
Click->new->visual c#->Bot application->provide a name for the bot and click->create.
Step 3
The bot application will be created using the Bot application template, the application will contain all the requirements that are needed to build an bot service. Now we need to update it to the latest version of SDK, using NUGET packages.
Right click->project-> click->Manage NUGET packages.
Switch->Browse tab-> and search for Microsoft.Bot.Builder package and update the package to the latest version of it.
Wait till the package gets updated and the project contains all the requirements for building a bot so that we need not write any codings.
Step 4
select->controllers\MessagesController.CS in the right side of the solution explorer and the bot requirements will be available as described below
- [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;
- }...
- }
Step 5
The message generation and response generation will be processed by root dialog with using the MessageReceiverdAsync function in dialog\rootdialog.cs, it replies back to the user in texts representing by number of characters in the message. The rootdialog.cs will be coded like described below.
- [Serializable]
- public class RootDialog: IDialog < object > {
- public Task StartAsync(IDialogContext context) {
- context.Wait(MessageReceivedAsync);
- return Task.CompletedTask;
- }
- private async Task MessageReceivedAsync(IDialogContext context, IAwaitable < object > result) {
- var activity = await result as Activity;
-
- int length = (activity.Text ? ? string.Empty).Length;
-
- await context.PostAsync($ "You sent {activity.Text} which was {length} characters");
- context.Wait(MessageReceivedAsync);
- }
- }
Step 6
Now we can run bot service using the browser and click->run button in visual studio, after running the bot service using the browser, the application will deploy with localhost default.htm page.
Step 7
The localhost default.htm page is displayed so that now we can start the Bot framework emulator and connect the bot with it. Before running with bot emulator the bot is running locally in the browser.
Step 8
Type http://localhost:port-number/api/messages in the address bar, so that the port number matches with the port number where the bot is running
Step 9
After typing the link in the address bar, just Click->connect. The App ID and the APP password will be obtained if you register the bot with bot framework. By clicking connect button the bot which is running locally will be connected with the emulator and we can test the bot by sending messages to it. The response that is generated will be echoed back to the user. Such as with the total number of characters that we sent.
Now we created a bot using the Bot application template and with Bot builder SDK for .NET, Thanks for reading have a nice day.