Quick start - Development Of Chat Bot Using Microsoft Bot Framework - For Beginners

In this article, we are going to see all the things we require before starting with the development of both applications using the Microsoft bot framework.

Prerequisite

  • Download Visual Studio 2015/2017 and update all extensions.
  • Download and install the bot application template from here.
  • Download Bot Framework Emulator. The emulator is a desktop application that lets you test a bot application on localhost or running remotely.

Create bot application

Open Visual Studio and create a new C# project. Choose the Bot Application template. If required, also update the NuGet Packages installed in the project.

Bot Application template

Nuget Packages

The template contains all components required to build a bot, as well as the initial code setup.

Code

In Solution Explorer, you will see WebApiConfig.cs file, Controllers, and Dialogs folders. 'MessageController' is inherited from 'System.Web.Http.ApiController'. Bot application is nothing but webapis, which will be called from the chat window to get a response.

Dialogs folders

When the user is going to enters a query in the chat window, the 'post( )' method within the controller will get called. Then, the controller will invoke a specified dialog (in our case, its RootDialog), which will process the query and return the result.

MessageController.cs

[BotAuthentication]
public class MessagesController : ApiController
{
    /// <summary>
    /// POST: api/Messages
    /// Receive a message from a user and reply to it
    /// </summary>
    public async Task<HttpResponseMessage> Post([FromBody] Activity activity)
    {
        if (activity.Type == ActivityTypes.Message)
        {
            await Conversation.SendAsync(activity, () => new Dialogs.AnswerDialog());
        }
        else
        {
            HandleSystemMessage(activity);
        }
        
        var response = Request.CreateResponse(HttpStatusCode.OK);
        return response;
    }

    private Activity HandleSystemMessage(Activity message)
    {
        // Implementation for handling system messages
    }
}

RootDialog.cs

[Serializable]
public class RootDialog : IDialog<object>
{
    public Task StartAsync(IDialogContext context)
    {
        context.Wait(MessageReceivedAsync);
        return Task.CompletedTask;
    }

    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
        await context.PostAsync($"You sent {activity.Text} which was {length} characters");
        context.Wait(MessageReceivedAsync);
    }
}

When the controller invokes RootDialog, control will come to the 'StartAsync( )' method of dialog. It will invoke the MessageReceivedAsync ( )' method, which will return a number of characters in the text entered by the user.

Test

Hit F5 to test a bot application. It will host the application with IIS Express and open the browser. To understand the control flow, insert breakpoints in the Post( ) method of MessageController and StartAsync( ) method of RootDialog.

IIS Express

Now, launch the bot emulator app. Put the URL as 'http://localhost:{{port no. from browser url}}/api/{{controller name}}' to connect the emulator with the bot application. Keep App ID and App Password blank, and click on Connect.

URL

Start chatting with the bot, and the bot will reply with a number of characters in your text. Happy chatting!

Start chatting


Similar Articles