Getting Started With Prompt Dialog Using Microsoft Bot Framework

Introduction

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 prompt Dialogs that allow the users to model conversations and manage conversation flow. The prompt is used whenever a bot needs input from the user. You can use prompts to ask a user for a series of inputs by chaining the prompts.

In this article, we will understand how to use prompts and how you can use them to collect information from the users. We are creating a sample Demo Bot for our C# Corner Annual Conference 2018 registration process automation.

Microsoft Bot Framework
Prerequisite

 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
  5. Getting Started with Dialog Using Microsoft Bot Framework

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 solution. Add anew AnnualPlanDialog class to the project.

Microsoft Bot Framework

In this solution, we have three main classes - MessagesController.cs, RootDialog.cs, and AnnualPlanDialog class. 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() methond. StartAsync() is called when the dialog becomes active. The method is passed  to the IDialogContext object that's used to manage the conversation.

  1. public async Task StartAsync(IDialogContext context)  
  2.       {  
  3.       } 

Step 3 Design Title and Image for Welcome

Create a method with Hero card and return the attachment. The Hero card is a multi-purpose card having a single title, subtitle, large image, button, and a "tap action “. The following code is added to the C# Corner Annual Conference 2018 registration welcome message using Bot Hero card.

  1. /// <summary>  
  2.      /// Design Title with Image and About US link  
  3.      /// </summary>  
  4.      /// <returns></returns>  
  5.      private static Attachment GetHeroCard()  
  6.      {  
  7.          var heroCard = new HeroCard  
  8.          {  
  9.              Title = "Annual Conference 2018 Registrtion ",  
  10.              Subtitle = "DELHI, 13 - 15 APRIL 2018",  
  11.              Text = "The C# Corner Annual Conference 2018 is a three-day annual event for software professionals and developers. First day is exclusive for C# Corner MVPs only. The second day is open to the public, and includes presentations from many top names in the industry. The third day events are, again, exclusively for C# Corner MVPs",  
  12.              Images = new List<CardImage> { new CardImage("https://lh3.googleusercontent.com/-fnwLMmJTmdk/WaVt5LR2OZI/AAAAAAAAG90/qlltsHiSdZwVdOENv1yB25kuIvDWCMvWACLcBGAs/h120/annuvalevent.PNG") },  
  13.              Buttons = new List<CardAction> { new CardAction(ActionTypes.OpenUrl, "About US", value: "http://conference.c-sharpcorner.com/") }  
  14.          };  
  15.   
  16.          return heroCard.ToAttachment();  
  17.      }  

The "Welcome" banner looks like below.

Microsoft Bot Framework

Step 4 Custom Prompt Dialog

The system prompts a custom dialog for asking the user to select a registration plan, which he/she is interested in. 

Microsoft Bot Framework

Define the enum for different types of entry pass. It’s a prompt list item

  1. public enum AnnuvalConferencePass  
  2.         {  
  3.             EarlyBird,  
  4.             Regular,  
  5.             DelegatePass,  
  6.             CareerandJobAdvice,  
  7.         }  

Create a method ShowAnnuvalConferenceTicket with Prompt Dialog choice like below,

  1. public virtual async Task ShowAnnuvalConferenceTicket(IDialogContext context, IAwaitable<IMessageActivity> activity)  
  2.       {  
  3.           var message = await activity;  
  4.          
  5.           PromptDialog.Choice(  
  6.               context: context,  
  7.               resume: ChoiceReceivedAsync,  
  8.               options: (IEnumerable<AnnuvalConferencePass>)Enum.GetValues(typeof(AnnuvalConferencePass)),  
  9.               prompt: "Hi. Please Select Annuval Conference 2018 Pass :",  
  10.               retry: "Selected plan not avilabel . Please try again.",  
  11.               promptStyle: PromptStyle.Auto  
  12.               );  
  13.       }  

The PropmptDialog choice method has different parameters. You can refer below for parameters and their usage.

  1. Context - user context message
  2. Resume - its Resume handler, what next process
  3. Options - list of prompt item
  4. Retry - What to show on retry.
  5. Attempts -The number of times to retry.
  6. PromptStyle - Style of the prompt Prompt Style
  7. Descriptions - Descriptions to display for choices.

When the user selects an option, the ChoiceReceivedAsync method will be called.

  1. public virtual async Task ChoiceReceivedAsync(IDialogContext context, IAwaitable<AnnuvalConferencePass> activity)  
  2.       {  
  3.           AnnuvalConferencePass response = await activity;  
  4.           context.Call<object>(new AnnualPlanDialog(response.ToString()), ChildDialogComplete);  
  5.   
  6.       }   

If its bot conversation is completed, the ChildDialogComplete method will execute for showing the "Thanks" message.

  1. public virtual async Task ChildDialogComplete(IDialogContext context, IAwaitable<object> response)  
  2.       {  
  3.           await context.PostAsync("Thanks for select C# Corner bot for Annual Conference 2018 Registrion .");  
  4.           context.Done(this);  
  5.       }  

Step 5

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 ShowAnnuvalConferenceTicket () is called, it's passed the dialog context and an IAwaitable of type IMessageActivity. To get the message, await the result.

  1. public async Task StartAsync(IDialogContext context)  
  2.         {  
  3.             //Show the title with background image and Details  
  4.             var message = context.MakeMessage();  
  5.             var attachment = GetHeroCard();  
  6.             message.Attachments.Add(attachment);  
  7.             await context.PostAsync(message);  
  8.   
  9.             // Show the list of plan  
  10.             context.Wait(this.ShowAnnuvalConferenceTicket);  
  11.         } 

AnnualPlanDialog 

Create a new class file for the registration prompt dialog and implement IDialog interface. In resume parameter, we can specify which dialog method to be called next after the user has responded. The response from the user is passed to the subsequent dialog methods and called to the following class.

In this class , Bot will collect all the user information one by one using prompt dialog like below

  1. namespace BotPromptDialog.Dialogs  
  2. {  
  3.     [Serializable]  
  4.     public class AnnualPlanDialog : IDialog<object>  
  5.     {  
  6.         string name;  
  7.         string email;  
  8.         string phone;  
  9.         string plandetails;  
  10.         public AnnualPlanDialog(string plan)  
  11.         {  
  12.             plandetails = plan;  
  13.         }  
  14.         public async Task StartAsync(IDialogContext context)  
  15.         {  
  16.             await context.PostAsync("Thanks for Select "+ plandetails + " Plan , Can I Help for Registrtion ? ");  
  17.              
  18.             context.Wait(MessageReceivedAsync);  
  19.         }  
  20.   
  21.         public virtual async Task MessageReceivedAsync(IDialogContext context, IAwaitable<IMessageActivity> activity)  
  22.         {  
  23.             var response = await activity;  
  24.             if (response.Text.ToLower().Contains("yes"))  
  25.             {  
  26.                 PromptDialog.Text(  
  27.                     context: context,  
  28.                     resume: ResumeGetName,  
  29.                     prompt: "Please share your good name",  
  30.                     retry: "Sorry, I didn't understand that. Please try again."  
  31.                 );  
  32.             }  
  33.             else  
  34.             {  
  35.                 context.Done(this);  
  36.             }  
  37.               
  38.         }  
  39.   
  40.         public virtual async Task ResumeGetName(IDialogContext context, IAwaitable<string> Username)  
  41.         {  
  42.             string response = await Username;  
  43.             name = response; ;  
  44.   
  45.             PromptDialog.Text(  
  46.                 context: context,  
  47.                 resume: ResumeGetEmail,  
  48.                 prompt: "Please share your Email ID",  
  49.                 retry: "Sorry, I didn't understand that. Please try again."  
  50.             );  
  51.         }  
  52.   
  53.         public virtual async Task ResumeGetEmail(IDialogContext context, IAwaitable<string> UserEmail)  
  54.         {  
  55.             string response = await UserEmail;  
  56.             email = response; ;  
  57.   
  58.             PromptDialog.Text(  
  59.                 context: context,  
  60.                 resume: ResumeGetPhone,  
  61.                 prompt: "Please share your Mobile Number",  
  62.                 retry: "Sorry, I didn't understand that. Please try again."  
  63.             );  
  64.         }  
  65.         public virtual async Task ResumeGetPhone(IDialogContext context, IAwaitable<string> mobile)  
  66.         {  
  67.             string response = await mobile;  
  68.             phone = response;  
  69.   
  70.             await context.PostAsync(String.Format("Hello {0} ,Congratulation :) Your  C# Corner Annual Conference 2018 Registrion Successfullly completed with Name = {0} Email = {1} Mobile Number {2} . You will get Confirmation email and SMS", name, email, phone));  
  71.   
  72.             context.Done(this);  
  73.         }  
  74.     }  

After execute above code, the output look like below:

Microsoft Bot Framework

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.      }  
  21.   
  22.      private Activity HandleSystemMessage(Activity message)  
  23.      {  
  24.          if (message.Type == ActivityTypes.DeleteUserData)  
  25.          {  
  26.              // Implement user deletion here  
  27.              // If we handle user deletion, return a real message  
  28.          }  
  29.          else if (message.Type == ActivityTypes.ConversationUpdate)  
  30.          {  
  31.              // Handle conversation state changes, like members being added and removed  
  32.              // Use Activity.MembersAdded and Activity.MembersRemoved and Activity.Action for info  
  33.              // Not available in all channels  
  34.          }  
  35.          else if (message.Type == ActivityTypes.ContactRelationUpdate)  
  36.          {  
  37.              // Handle add/remove from contact lists  
  38.              // Activity.From + Activity.Action represent what happened  
  39.          }  
  40.          else if (message.Type == ActivityTypes.Typing)  
  41.          {  
  42.              // Handle knowing tha the user is typing  
  43.          }  
  44.          else if (message.Type == ActivityTypes.Ping)  
  45.          {  
  46.          }  
  47.   
  48.          return null;  
  49.      }  
  50.  } 

Run Bot Application

The emulator is a desktop application that lets we test and debug our bot on 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 for testing your bot application.

  1. Open Bot Emulator.
  2. Copy the above localhost URL and paste it in emulator e.g. - http://localHost:3979
  3. You can append the /api/messages in the above URL; e.g. - http://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".

    bot demo

Summary

In this article, we saw how to use prompts to collect information from the users. If you have any questions/ feedback/ issues, please write in the comment box.

Up Next
    Ebook Download
    View all
    Learn
    View all