Getting Started With Dialog Using Microsoft Bot Framework


The Bot Framework enables you to build bots that support different types of interactions with users. You can design conversations in your bot to be freeform. Your bot can also have more guided interactions where it provides the user choices or actions. The conversation can use simple text strings or more complex rich cards that contain text, images, and action buttons. And you can add natural language interactions, which let your users interact with your bots in a natural and expressive way.
Bot Builder SDK introduced Dialogs that allows users to model conversations and manage conversation flow. Dialogs can contain waterfall steps and prompts (Options). As the user interacts with the bot, the bot will start, stop, and switch between various dialogs in response based on user messages.
Microsoft Bot Framework
In this article, we will learn about the condition inside the Waterfall Bot Framework.
I have explained about Bot framework installation, deployment, and implementation in the below articles.
  1. Getting Started with Chatbot Using Azure Bot Service
  2. Getting Started with Bots Using Visual Studio 2017
  3. Deploying A Bot to Azure Using Visual Studio 2017
  4. How to Create ChatBot In Xamarin
Create New Bot Service
Let's create a new bot service application using Visual Studio 2017. Open Visual Studio > Select File > Create New Project (Ctrl + Shift +N) > select Bot application.
Microsoft Bot Framework
The Bot application template was created with all the components and all required NuGet references installed in the solutions.
Microsoft Bot Framework
In this solution, we have two main class MessagesController.cs and RootDialog.cs. Let us start discussing here.

RootDialog Class

Step 1
You can Create/Edit the RootDialog class. Create a class that is marked with the [Serializable] attribute (so the dialog can be serialized to state) and implement the IDialog interface.
  1. using System;  
  2. using System.Threading.Tasks;  
  3. using Microsoft.Bot.Builder.Dialogs;  
  4. using Microsoft.Bot.Connector;  
  5. [Serializable]  
  6.     public class RootDialog : IDialog<object>  
  7.     {  
Step 2
IDialog interface has only StartAsync() method. StartAsync() is called when the dialog becomes active. The method is passed the IDialogContext object, used to manage the conversation.
  1. public async Task StartAsync(IDialogContext context)  
  2. {  
Step 3
You can wait for a message from the conversation, call context.Wait(<method name>) and pass it the method you called when the message is received. When MessageReceivedAsync() is called, it passes the dialog context and an IAwaitable of type IMessageActivity. To get the message, await the result.
  1. [Serializable]  
  2.     public class RootDialog : IDialog<object>  
  3.     {  
  4.         public Task StartAsync(IDialogContext context)  
  5.         {  
  6.             context.Wait(MessageReceivedAsync);  
  8.             return Task.CompletedTask;  
  9.         }  
  11.         private async Task MessageReceivedAsync(IDialogContext context, IAwaitable<object> result)  
  12.         {  
  14.         }  
  15.     } 
Step 4
Let's start editing MessageReceivedAsync method as per our requirement or create a new async method. The following code is a welcome message conversation dialog.
  1. [Serializable]  
  2.     public class RootDialog : IDialog<object>  
  3.     {  
  4.         static string username;  
  5.         int count = 0;  
  6.         public async Task StartAsync(IDialogContext context)  
  7.         {  
  8.             await context.PostAsync("Hello, I am Microsoft Bot");  
  9.             context.Wait(MessageReceivedAsync);  
  10.         }  
  12.         private async Task MessageReceivedAsync(IDialogContext context, IAwaitable<object> result)  
  13.         {  
  14.             var message = await result as Activity;  
  16.             if(count >0)  
  17.                 username = message.Text;  
  19.             if (string.IsNullOrEmpty(username))  
  20.             {  
  21.                 await context.PostAsync("what is your good name");  
  22.                 count++;  
  23.             }  
  24.             else  
  25.             {  
  26.                  await context.PostAsync($"Hello {username} , How may help You");  
  28.             }  
  29.             context.Wait(MessageReceivedAsync);  
  31.         }  
  32.     } 
IDialogContext has the following Public Member Functions.
  1. Call< R > (IDialog< R > child, ResumeAfter< R > resume)
    Call a child dialog and add it to the top of the stack.
  1. Done< R > (R value)
    Complete the current dialog and return a result to the parent dialog.
  1. Context .Fail (Exception error)
    Fail the current dialog and return an exception to the parent dialog.
  1. Context .Forward< R, T > (IDialog< R > child, ResumeAfter< R > resume, T item, CancellationToken token)
    Call a child dialog, add it to the top of the stack and post the item to the child dialog.
  1. Context .Post< E > (E @event, ResumeAfter< E > resume)
    Post an internal event to the queue.
  1. Context .Reset ()
    Resets the stack
  1. Context .Wait< R > (ResumeAfter< R > resume)
    Suspend the current dialog until an external event has been sent to the bot.

MessagesController Class

The RootDialog class is added to the conversation in the MessageController class via the Post() method. In the Post() method, the call to Conversation.SendAsync() creates an instance of the RootDialog, adds it to the dialog stack to make it the active dialog, calling the RootDialog.StartAsync() from Post method
  1. [BotAuthentication]  
  2.     public class MessagesController : ApiController  
  3.     {  
  4.         /// <summary>  
  5.         /// POST: api/Messages  
  6.         /// Receive a message from a user and reply to it  
  7.         /// </summary>  
  8.         public async Task<HttpResponseMessage> Post([FromBody]Activity activity)  
  9.         {  
  10.             if (activity.Type == ActivityTypes.Message)  
  11.             {  
  12.                 await Conversation.SendAsync(activity, () => new Dialogs.RootDialog());  
  13.             }  
  14.             else  
  15.             {  
  16.                 HandleSystemMessage(activity);  
  17.             }  
  18.             var response = Request.CreateResponse(HttpStatusCode.OK);  
  19.             return response;  
  20.         } 
Run Bot Application
The emulator is a desktop application that lets us test and debugs our bot on the localhost. Now, you can click on "Run the application" in Visual studio and execute in the browser
Microsoft Bot Framework
Test Application on Bot Emulator
You can follow the below steps to test your bot application.
  1. Open Bot Emulator.
  2. Copy the above localhost url and paste it in emulator e.g. - https://localHost:3979
  3. You can append the /api/messages in the above url; e.g. - https://localHost:3979/api/messages.
  4. You won't need to specify Microsoft App ID and Microsoft App Password for localhost testing, so click on "Connect".
    Microsoft Bot Framework


In this article, you learned how to create a Bot application using Visual Studio 2017 with Bot Dialog API. If you have any questions/feedback/issues, please write in the comment box.

Similar Articles