Dialog In Microsoft Bot Framework

What is a dialog in Bot framework?

Basically dialog is a class, which allows Bot developer to logically separate various areas of Bot functionality and guide conversational flow.

As in a Website or an app, we used single or multiple screens to exchange the information with the user, which is called UI (user interface), we can say the screens are user interfaces. Similarly Bot has UI but it is made up of dialogs.

Important point
  • They may be in the form of text, button, speech based or other elements.
  • Thay may or may not have graphical interfaces.
  • They are used to invoke other dialogs or processing user input. In Bot, everything begins with the root dialog.
Why we use dialog?
  1. Bot and user can have conversations through dialog. Thease dialogs are component classes of the logical pieces.
  2. Break up conversations into smaller pieces.
  3. It means they are more portable.
Use the namespace to access the relevent class of dialog.
  1. using Microsoft.Bot.Builder.Dialogs;
Here you can see root dialogW
  1. using System;  
  2. using System.Threading.Tasks;  
  3. using Microsoft.Bot.Builder.Dialogs;  
  4. using Microsoft.Bot.Connector;  
  5.   
  6. namespace BotDialogsSample.Dialogs  
  7. {  
  8.     [Serializable]  
  9.     public class RootDialog : IDialog<object>  
  10.     {  
  11.         public Task StartAsync(IDialogContext context)  
  12.         {  
  13.             // Root dialog initiates and waits for the next message from the user.      
  14.             context.PostAsync("Hi I am Root dialog.");  
  15.             context.Wait(MessageReceivedAsync);  
  16.             return Task.CompletedTask;             
  17.         }  
  18.   
  19.         private async Task MessageReceivedAsync(IDialogContext context, IAwaitable<object> result)  
  20.         {  
  21.             var activity = await result as Activity;  
  22.   
  23.             // calculate something for us to return  
  24.             int length = (activity.Text ?? string.Empty).Length;  
  25.   
  26.             // return our reply to the user  
  27.             await context.PostAsync($"You sent {activity.Text} which was {length} characters");  
  28.   
  29.             context.Wait(MessageReceivedAsync);  
  30.         }  
  31.     }  
  32. }  
The IDialog Interface
  1. Lives in Microsoft.Bot.Builder.Dialogs namespace.
  2. Must use the serializeable attribute.
  3. Has one method StartAsync.
  4. Utilizes Async and Await.
  5. Remember a Bot is really a Web Service.
  6. If you want to build a Bot dialog, you can see IDialog interface
In Bot example given below, you can see how we use dialogs to exchange messages with the user and Bot. Invoke the root dialog. Here, you can see how root dialog is invoked when post method invokes into messageController of Bot.

MessagesController
  1. using System.Net;  
  2. using System.Net.Http;  
  3. using System.Threading.Tasks;  
  4. using System.Web.Http;  
  5. using Microsoft.Bot.Builder.Dialogs;  
  6. using Microsoft.Bot.Connector;  
  7. using BotDialogsSample.Dialogs;  
  8.   
  9. namespace BotDialogsSample  
  10. {  
  11.     [BotAuthentication]  
  12.     public class MessagesController : ApiController  
  13.     {  
  14.         /// <summary>  
  15.         /// POST: api/Messages  
  16.         /// Receive a message from a user and reply to it  
  17.         /// </summary>  
  18.         public async Task<HttpResponseMessage> Post([FromBody]Activity activity)  
  19.         {  
  20.             if (activity.Type == ActivityTypes.Message)  
  21.             {  
  22.                 await Conversation.SendAsync(activity, () => new Dialogs.RootDialog());                
  23.             }  
  24.             else  
  25.             {  
  26.                // HandleSystemMessage(activity);  
  27.             }  
  28.             var response = Request.CreateResponse(HttpStatusCode.OK);  
  29.             return response;  
  30.         }       
  31.     }  
  32. }  
Now, create new dialog as MyDialog, as shown below.
  1. using Microsoft.Bot.Builder.Dialogs;  
  2. using System;  
  3. using System.Collections.Generic;  
  4. using System.Linq;  
  5. using System.Web;  
  6. using System.Threading.Tasks;  
  7. using Microsoft.Bot.Connector;  
  8.   
  9. namespace BotDialogsSample.Dialogs  
  10. {  
  11.     [Serializable]  
  12.     public class MyDialog : IDialog  
  13.     {  
  14.         public async Task StartAsync(IDialogContext context)  
  15.         {  
  16.             await context.PostAsync("Hi I am Puru.");  
  17.             context.Wait(MessageReceivedAnync);  
  18.         }  
  19.   
  20.         public virtual async Task MessageReceivedAnync(IDialogContext context, IAwaitable<IMessageActivity> result)  
  21.         {  
  22.             var message = await result;  
  23.             var userName = String.Empty;  
  24.   
  25.             //  
  26.             var getName = false;  
  27.             context.UserData.TryGetValue<string>("Name"out userName);  
  28.             context.UserData.TryGetValue<bool>("GetName"out getName);  
  29.             if (getName)  
  30.             {  
  31.                 userName = message.Text;  
  32.                 context.UserData.SetValue<string>("Name",  userName);  
  33.                 context.UserData.SetValue<bool>("GetName",  false);  
  34.             }  
  35.   
  36.             if (string.IsNullOrEmpty(userName))  
  37.             {  
  38.                 await context.PostAsync("What is your name");  
  39.                 context.UserData.SetValue<bool>("GetName"true);  
  40.   
  41.             }  
  42.             
  43.             else  
  44.             {  
  45.                 await context.PostAsync(String.Format("Hi {0}. How can I help you taday?", userName));  
  46.             }  
  47.             context.Wait(MessageReceivedAnync);  
  48.         }  
  49.     }  
  50. }  
Now, invoke MyDialog when post method invokes into messageController of Bot.
  1. using System.Net;  
  2. using System.Net.Http;  
  3. using System.Threading.Tasks;  
  4. using System.Web.Http;  
  5. using Microsoft.Bot.Builder.Dialogs;  
  6. using Microsoft.Bot.Connector;  
  7. using BotDialogsSample.Dialogs;  
  8.   
  9. namespace BotDialogsSample  
  10. {  
  11.     [BotAuthentication]  
  12.     public class MessagesController : ApiController  
  13.     {  
  14.         /// <summary>  
  15.         /// POST: api/Messages  
  16.         /// Receive a message from a user and reply to it  
  17.         /// </summary>  
  18.         public async Task<HttpResponseMessage> Post([FromBody]Activity activity)  
  19.         {  
  20.             if (activity.Type == ActivityTypes.Message)  
  21.             {  
  22.                 await Conversation.SendAsync(activity, () => new MyDialog());  
  23.             }  
  24.             else  
  25.             {  
  26.                 //HandleSystemMessage(activity);  
  27.             }  
  28.             var response = Request.CreateResponse(HttpStatusCode.OK);  
  29.             return response;  
  30.         } 
  31.     }  
  32. }  

Output A - Output comes when RootDialog is invoked

Please follow step 6 to 10 of my previous article for which link is given below.
Now, type your message and see how rootmessage is invoked.



Output B

Output comes when MyDialog is invoked.



Stay tuned for the next article. Thanks.

Up Next
    Ebook Download
    View all
    Learn
    View all