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 the next business day to start to get the answers to your queries.
    Specific information
  • 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.Send emails

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 a 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 the below location.

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

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

Bot Application template

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

Sample project

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.

MessagesController

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

NuGet packages

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.

CompletedTask

Your solution should be built properly now. Now, we will make changes in the default code and modify it as per our needs.

Open RootDialog.cs class which is in the Dialogs folder. Replace the code of the method with the 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 a landing page as below,

 Bot application

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 a 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 run 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.

Start installation

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

Landing page

It will ask you for a 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 input

As you can see, it answered all of those which we are given in the above code, and for the 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 the BotAuthentication attribute above the action. You can find the code for the same attribute here.

Hope it helps.


Similar Articles