Create Your First Bot Using Visual Studio 2017 - Step By Step Guide

Bot Framework
Observing how fast the companies are adopting 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 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 the answers to your queries.

    Bot Framework
  • 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 be required if the number of questions increases suddenly.

    Bot Framework

This would eventually affect your business. Thus, it is a good time to try  Bots.

Let us see what Bots are.

An Internet Bot, also known as web robot, WWW 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 for human interaction.

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

Prerequisites

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.

Bot Framework

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

Bot Framework

Here, MessagesController is created by default which is the main entry point of the application.

If you open MessagesController, it will look blood red because it cannot find the missing NuGet packages,

Bot Framework

You just need to restore those missing NuGet packages by opening NuGet Manager.

Bot Framework

You may encounter an error like this.

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.

Bot Framework

Your solution should be built 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 the method with below code.

  1. private async Task MessageReceivedAsync(IDialogContext context, IAwaitable<object> result)   
  2. {    
  3.   var activity = await result as Activity;   // calculate something for us to return     
  4.   int length = (activity.Text ?? string.Empty).Length;   // return our reply to the user    //test    
  5.   if (activity.Text.Contains("morning"))    
  6.   {       
  7.     await context.PostAsync("Good Morning , Have a nice Day");    
  8.   }   //test      
  9.   else if (activity.Text.Contains("night"))    
  10.   {       
  11.     await context.PostAsync("Good night and Sweetest Dreams");    
  12.   }    
  13.   else if (activity.Text.Contains("who are you"))    
  14.   {       
  15.     await context.PostAsync("I am a Bot created by Neel");    
  16.   }    
  17.   else if (activity.Text.Contains("date"))    
  18.   {       
  19.     await context.PostAsync(DateTime.Now.ToString());    
  20.   }     
  21.   else   
  22.   {      
  23.     await context.PostAsync($"You sent {activity.Text} which was {length} characters");   
  24.   }   
  25.   context.Wait(MessageReceivedAsync);  
  26. }  

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,

Bot Framework

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.

Click on exe, it will start the installation.

Bot Framework

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),

Bot Framework

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.

Bot Framework

As you can see, it answered all of those which we are 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 Bots.

Important Note

The 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. 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.

Hope it helps.

Up Next
    Ebook Download
    View all
    Learn
    View all