Bots How to's

Bot – A new Trend… How to Create a Simple Bot using Microsoft Bot Framework

Chat Bot TechieHubs

Are you bored with regular customer care service? Looking for a Bot Assistant? But confused which one choose because there are loads of Bot developing platforms are available in the market, only some are in the enterprise version.

To compete in a market, people loves to have robust, flexible, 24/7 support on the developing platforms and to customize all their needs to the core functionality, we chose Microsoft Bot Framework for exploring. You need to have a Basic understanding of Visual Studio, C# / Node js and the Microsoft Bot Framework template.

Also Read about : Dash Bot – Bot Analytics

Video Credit: Video Credit: Skype hero SM Microsoft Bot Framework

Here we will create a simple bot which will reply “How are you, buddy? How can I help you?”

Basic requirements/pre-requisites

  1. Microsoft Visual Studio 2015 – Download Link
  2. Bot Framework template/emulator – Download Link
  3. Basic knowledge on c# / Node js – Download Link

Now we can create our First localhost project using Visual Studio – Bot Framwork

Step 1: Open Visual Studio 2015 or above

Visual Studio - TechieHubs

Step 2 : Create New Project

Visual Studio - TechieHubs

Step 3: Choosing the Template

Select the Bot framework template and click on create project, or you have an option to download templates from online.

Step 4: Click on Solution explorer -> Controllers -> Messagecontroller.cs

Default code is as follows

if (activity.Type == ActivityTypes.Message)
 {
 ConnectorClient connector = new ConnectorClient(new Uri(activity.ServiceUrl));
 // calculate something for us to return
 int length = (activity.Text ?? string.Empty).Length; 
 // return our reply to the user
 Activity reply = activity.CreateReply($"You sent {activity.Text} which was {length} characters"); 
 await connector.Conversations.ReplyToActivityAsync(reply);
 }
 else
 {
 HandleSystemMessage(activity);
 }
 var response = Request.CreateResponse(HttpStatusCode.OK);
 return response;
 }

In the above section the Bot will collect your input and respond as the total length.

We need to Edit the following section which will process the input reply to end user.

 Activity reply = activity.CreateReply("Hello");
 await connector.Conversations.ReplyToActivityAsync(reply);

Here the bot framework will create an async activity to print the text as a message.

if we want to print any value, we need to change the value to as required, for example – “Hello ” to “Hi”

Step 5: Validating the User query and the response from the Bot

Let’s see, how to validate my input and respond back. To achieve this task, we can use a simple if block.

 IF (activity.Text == "Hi" || activity.Text=="Hello")
 {
 Activity reply = activity.CreateReply("How are you, buddy? How can I help you?");
 await connector.Conversations.ReplyToActivityAsync(reply);
 }
 ELSE
 {
 Activity reply = activity.CreateReply("I'm in Learning phase, please teach what should be the correct response");
 await connector.Conversations.ReplyToActivityAsync(reply);
 }

We can build n number of conditions using these simple codes based.

Step-6: Testing the Bot using Emulator

Now coding part is done, we need to check how our bot behaves. For this, we can use Microsoft emulator as the testing tool, compile the code and open the emulator.

In the Emulator type the URL as http://localhost:3979/api/messages

Our ChatBot was tested and ready, now we need to register with Microsoft Azure to play with our bot in Skype, Twilio,Facebook Messenger, Kik and other popular Bot supporting messaging services.

Stay tuned with this site for more complicated/easy to create Bots.

 

About the author

Deepak subramoniam

Deepak Subramoniam is a Database Administrator on Oracle, MSSQL and MYSQL for a MNC Organization. Apart from this he is passionate on Photography and Automotives.

Leave a Reply

%d bloggers like this: