Bot Development : Getting Started

By | December 28, 2018

Bot, the new interesting term in the computing market. Everyone needs one for their requirement, in this post we will try to first understand what is a computer bot and how should we approach in creating one. Bot is a modern term used for robots, and robot is generally used for the mechanical arms and machines which reduces human efforts. Computer robots commonly referred as Bots are basically a web service which interacts with users and get things done.

Traditional vs Modern Day Bot
Traditional vs Modern Day Bot

Traditional robots were programmed for certain set of instructions, however the new bots are capable of learning over the period of time and interact more efficiently with the user with use of some tools like natural language processing. There are several frameworks available which helps the developer to build these new generation bots in a most controlled manner, in this series we will be using Microsoft Bot Builder Framework to build our bot.

Prerequisites

  • Visual Studio 2017
  • Bot Builder SDK v4 template for C#
  • Bot Framework Emulator

As mentioned above we will be using Visual Studio as our IDE, and Microsoft Bot Builder SDK templates for getting started. Bot Framework Emulator will be used for testing our bots. So lets get started with creation of our first Bot.

Step 1 : Setting Up Project

Assuming the prerequisites are already installed, we will start creating the basic project and will try to understand the structure of the project which will help us in customizing and enhancing our bot further. In the bot SDK v4 there are two templates available:

  • EmptyBot : This is a basic project type with just the skeleton of the bot. This template is good for learning purpose as you can take the sample code from documentation and start experimenting
  • EchoBot : This template is bit more advanced than the EmptyBot, as the name suggests it echos back whatever the user has entered. In this template you can learn about processing the messages sent by user.

In this tutorial we will be using EmptyBot template.

Step 1 - Create EmptyBot
Step 1 – Create EmptyBot

Step 2 : Understanding the template files

Once we create a template project, there are certain files created which have basic functionality of the bot. In this section we will try to understand these files.

Step 2 - Template Files
Step 2 – Template Files
  • Dependencies : This section contains all the dependencies of the project i.e. all the NuGet packages and other SDK dependencies
  • Properties : This is common across almost all visual studio projects and has general project properties. It also has a file launchSettings.json which contains launch settings like application URL
  • wwwroot : This contains a default HTML file containing links documentation for various sections. This will not be of much relevance as the bot will be tested in Emulator and not browser
  • appsettings.json : This is used to store other application level settings required e.g. app secret, app id
  • Program.cs & Startup.cs : Program.cs is the file which builds the web host for the project using configurations mentioned in Startup.cs
  • ShubhamSaxena_DemoBot.bot : This is the bot config file which contains configurations for different environments. It also contains endpoint URL which will be used to accessing bot in emulator
  • ShubhamSaxena_DemoBotBot.cs : This is the main file where the logic for your bot is written, lets have a look at the sample file in next step

Step 3 : Understanding Sample Bot Class

Below is the sample bot class, lets understand the different segments of it.

using System.Threading;
using System.Threading.Tasks;
using Microsoft.Bot.Builder;
using Microsoft.Bot.Schema;

namespace ShubhamSaxena_DemoBot
{
    public class ShubhamSaxena_DemoBotBot : IBot
    {
        public ShubhamSaxena_DemoBotBot()
        {

        }

        public async Task OnTurnAsync(ITurnContext turnContext, CancellationToken cancellationToken = default(CancellationToken))
        {
            if (turnContext.Activity.Type == ActivityTypes.Message)
            {
                await turnContext.SendActivityAsync("Hello World", cancellationToken: cancellationToken);
            }
        }
    }
}

This is a normal class which implements the interface IBot. This interface has only one method i.e. OnTurnAsync this is the method responsible for all the activities within the bot. OnTurnAsync takes two parameters as input, one is turnContext which holds all the information of the current context/message i.e. sender information, message text etc. CancellationToken contains all the parameters required for cancelling the current context. It returns a Task/Activity, in the above example the bot always returns “Hello World”. If you have noticed SendActivityAsync is called only when the activity type is Message. There is a whole bunch of activity types, which we can discuss in separate article.

Step 4 : Testing the Bot on Emulator

As you have seen the sample code is already part of the template, before we go ahead and make some customization let’s see how we can test our bot on emulator. Follow below steps to launch your bot on emulator :

  • Run you project in visual studio by pressing F5 key
  • Get the endpoint URL from ShubhamSaxena_DemoBot.bot
"services": [
    {
      "type": "endpoint",
      "name": "development",
      "endpoint": "http://localhost:3978/api/messages",
      "appId": "",
      "appPassword": "",
      "id": "1"
    }
  • Launch emulator and add endpoint to get started
Adding Endpoint in Emulator
Adding Endpoint in Emulator

Once your endpoint is added you can start interacting with your bot. For now whatever you type in bot will always reply “Hello World”, below is the screen demonstrating the same.

Sample Bot Chat
Sample Bot Chat

That’s it we have our first bot up and running. Though we have not made any customization yet, we have learned how to setup things and understand various aspects of bot development. In the coming articles we will learn about adding more functionality to our bot until then keep learning and keep sharing.

3 thoughts on “Bot Development : Getting Started

  1. Sneha

    Liked how everything is explained in simple terms for a beginner like me. Looking forward to see more posts!

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.