Create your first Bot using Visual Studio 2017: Step by step guide

bot17

By looking at how fast the companies adopting the Bots, it is really the best time for you to start learning Bot framework and start adopting Bots for your business.

Some pain in the real world without the Bots:

  • You have to read the whole FAQ to find some specific information for any website or any company
  • You have to wait for next business day to start to get answers to your queries

bot15

  • You have to send emails to get some information to send some information
  • You have to do manual work to answer some repetitive questions
  • More manpower would require if the number of questions increases suddenly:

bot16

This would eventually affect your business. Time to try the Bots.

Let us see what the Bots are:

An Internet bot, also known as web robotWWW robot or simply bot, is a software application that runs automated tasks (scripts) over the Internet. Typically, bots perform tasks that are both simple and structurally repetitive, at a much higher rate than would be possible for a human alone.

In simple words, Bots are something that can be integrated with your website and they can answer the questions posted by the users without the need of the humans

Let us see how to create simple Bot application using Visual Studio 2017.

prerequisite:

Also if you want to have Bot Application as a template then as a workaround just download this  (download would start once you click on the link) project and put the extracted folder into below location:

C:\Users\YourName\Documents\Visual Studio 2015\Templates\ProjectTemplates\Visual C#

Once this is done, you can see Bot Application template as shown below:

bot2

Click on Bot Application and then it will create a sample project which has the structure as below:

bot3

Here MessagesController is created by default and it is the main entry point of the application.

If you open MessagesController, it will look blood red because it cannot find missing Nuget packages:

bot4

You just need to restore those missing Nuget packages by opening Nuget Manager:

bot5

You may encounter an error which would be as below:

CS0117 ‘Task’ does not contain a definition for ‘CompletedTask’ NeelBotDemo c:\users\NeelB\documents\visual studio 2017\Projects\NeelBotDemo\NeelBotDemo\Dialogs\RootDialog.cs 15 Active

This error comes because Task.CompletedTask is a static property added in .NET 4.6 and your application may have .NET 4.5

You need to change your application’s Target framework and make it 4.6.1 as below:

bot6

Your solution should build properly now.

Now we will make changes in the default code and will modify as per our need.

Open RootDialog.cs class which is in the Dialogs folder.

Replace the code of method with below code:

private async Task MessageReceivedAsync(IDialogContext context, IAwaitable<object> result)
{
  var activity = await result as Activity;
  // calculate something for us to return 
  int length = (activity.Text ?? string.Empty).Length;
  // return our reply to the user 
  //test 
  if (activity.Text.Contains("morning"))
  {
     await context.PostAsync("Good Morning , Have a nice Day");
  }
  //test 
  else if (activity.Text.Contains("night"))
  {
     await context.PostAsync("Good night and Sweetest Dreams");
  }
  else if (activity.Text.Contains("who are you"))
  {
     await context.PostAsync("I am a Bot created by Neel");
  }
  else if (activity.Text.Contains("date"))
  {
     await context.PostAsync(DateTime.Now.ToString());
  }
  else
  {
     await context.PostAsync($"You sent {activity.Text} which was {length} characters");
  }
context.Wait(MessageReceivedAsync);
}

Here we are telling Bot, what it should answer when there are some specific keywords are there in the message.

Once you made the changes, just run your Bot application. It will have landing page as below:

bot9

At this point, your bot is ready to be used. We need an emulator to test our bot.

If we want to test our bots locally then Bot emulator is the best option.

The Bot Framework Emulator is a desktop application that allows bot developers to test and debug their bots on localhost or running remotely through a tunnel.

As we mentioned on top of the post, you can download the Bot emulator from here. Or you can click below, it will start the download automatically:

botframework-emulator-Setup-3.5.33.exe

Click on exe, it will start the installation:

bot10

And once the installation is done, it will have a landing page as below, here you need to give the URL of your bot application(http://localhost:3979/api/messages):

bot13

It will ask you for Microsoft App Id and password, but for now, do not give anything there and click on CONNECT.

Now your bot is ready to be tested.

Give something that you have added to your code and the bot will respond as per your input in code:

bot14

As you can see it answered all of those which we have given in above code and for rest of the things, it will answer as: “You sent {input} which was {length} character”

Congratulations, you just created your first Bot 🙂

You can integrate Microsoft Cognitive APIs into your Bot application, I have written a post on the same which you can find here.

In my upcoming posts, I will share my experiments with the Bots.

Important Note – Microsoft team has created Bot Builder SDK for .NET so that it is easy for us to develop the bots. But if you want to know what is happening behind the curtain then you can have a look here: https://github.com/Microsoft/BotBuilder/tree/65d4e985b68e4fce5a5e0ac81de198c0a95ac7bb/CSharp/Library, all the libraries are here.

For example, you may have seen I used BotAuthentication attribute above the action, you can find the code for the same attribute here: https://github.com/Microsoft/BotBuilder/blob/65d4e985b68e4fce5a5e0ac81de198c0a95ac7bb/CSharp/Library/Microsoft.Bot.Connector.AspNetCore/BotAuthentication.cs

Hope it helps.

 

8 thoughts on “Create your first Bot using Visual Studio 2017: Step by step guide

  1. Excellent article to start with. Just need to know what if I have a Question and answer file(Excel,pdf,tsv…) like QnA chat bot and I want to use that file.

    Regards,
    Tarak Bhatt.

    Like

Leave a comment