Run, test and understand a Bot sample

Rachhek Shrestha
AI for Developers
Published in
4 min readMay 31, 2017

This article will cover how to run your first Bot(Sample) using Microsoft Bot Framework in .NET framework.

Pre-requisites

  1. Microsoft Visual Studio 2017 (Download)
  2. Microsoft Bot Emulator (Download)

Setup Steps

  1. Download the Visual Studio Template for Bot Framework from here
  2. Without unzipping the template, copy the .zip file to this location in your Visual Studio C# templates folder (C:\Users\<<Your User>>\Documents\Visual Studio 2017\Templates\ProjectTemplates\Visual C#)
  3. Now when you try to create a new project in Visual Studio, the Bot Application Template should show up under Template->Visual C#
Bot application Template
  1. Download the Bot Emulator from here
  2. Install the bot emulator.
Bot Emulator

Bot Framework Folder Architecture

After you create a Bot Application in Visual Studio the solution explorer looks like this:

Solution Explorer

This bot application project is basically a ASP.NET Wen API project. When you open up the MessagesController.cs file we see this:

MessagesController Class

MessagesController.cs

This class is inherited from the API Controller interface which implements the POST request. Every message sent by the user to the bot is a POST request.

ActivityType.Message

This part of the code is basically letting the RootDialog Class handle all the messages from the user

This function is basically handling all the activities except ‘Messages’. Events like typing, user being added to the conversation, users leaving the conversation etc.

RootDialog.cs

The RootDialog.cs is inherited from IDialog interface. Dialogs are the basic conversation blocks that make up the bot. Every dialog has to be inherited form IDialog and the class has to be ‘Serializable’. In this sample code the MessageReceivedAsync function is basically echoing whatever the message the user is sending to the bot. You can see that whenever you want the bot to send any message to the user it is using context.PostAync() function.

Running the Bot

Lets run the bot application sample. This will basically host our application in the localhost and our REST API endpoint will be opened and will be listening.

Our bot application hosted at localhost

Testing with Bot Emulator

Open up Bot emulator and on the top, insert the endpoint as below.

Leave the rest of Microsoft App ID and App Password blank for now. This is required only when out bot is hosted somewhere else and our bot is registered in the bot developer portal. Now press Connect.

You can confirm that the bot endpoint is listening when you see the 200 response in the emulator log

log View

Type any message in the text box and press enter.

We get the following response from the bot:

In the log we can see that a couple of GET and POST requests are made. If everything went correctly we will see all of them give 200 Status Ok message. This way we can know the bot response is correct.

You can click on individual request to see the detailed JSON message being communicated.

You can insert breakpoints in the functions to check how messages are being processed by the bot application.

Following blog posts will cover similar bot tutorials and intro to Azure cognitive Services.

--

--