Articles → MICROSOFT BOT FRAMEWORK → Prompts In Microsoft Bot Framework

Prompts In Microsoft Bot Framework






What Are Prompts?




  1. Text Prompt - In this prompt, the bot asks the input from the user in the text format.
  2. Number Prompt - In this prompt, the bot asks the input from the user in the number format.
  3. Choice Prompt - In this prompt, the bot gives a list of choices to the user to select from.
  4. Confirm prompt - In this prompt, the bot gives 2 options to the user i.e. yes and no.

Example




using Karan_EchoBot.Models;
using Microsoft.Bot.Builder;
using Microsoft.Bot.Builder.Dialogs;
using Microsoft.Bot.Builder.Dialogs.Choices;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;

namespace Karan_EchoBot.Dialogs {
  //C# we need to make it as Dialog
  public class RootDialog: ComponentDialog {
    private IStatePropertyAccessor < UserData > _userData;
    public RootDialog(UserState userState, ConversationState conversationState): base(nameof(RootDialog)) {

      _userData = userState.CreateProperty < UserData > ("UserData");

      var steps = new WaterfallStep[] {

        AskUserNameAsync,
        AskOfficeTypeAsync,
        AskPhoneNumberAsync,
        AskUserConfirmationAsync,
      };

      AddDialog(new WaterfallDialog("UserDetailsWaterfall", steps));
      AddDialog(new TextPrompt("AskUserNamePrompt", null));
      AddDialog(new ChoicePrompt("AskTypeOfAdressPrompt"));
      AddDialog(new ConfirmPrompt("ConfirmUserDetialsPrompt"));
      AddDialog(new NumberPrompt < long > ("AskPhoneNumberPrompt", ValidatePhoneNumberAsync));
    }

    private async Task < DialogTurnResult > AskUserNameAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken) {
      return await stepContext.PromptAsync("AskUserNamePrompt", new PromptOptions {
        Prompt = MessageFactory.Text("What's your Name?"), RetryPrompt = MessageFactory.Text("Can I know your name?")
      }, cancellationToken);
    }

    private async Task < bool > ValidatePhoneNumberAsync(PromptValidatorContext < long > promptContext, CancellationToken cancellationToken) {
      long phoneNumber;

      long.TryParse(promptContext.Recognized.Value.ToString(), out phoneNumber);

      if (phoneNumber.ToString().Length == 10) {
        return await Task.FromResult(true);
      } else {
        return await Task.FromResult(false);
      }
    }

    private async Task < DialogTurnResult > AskPhoneNumberAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken) {
      var userData = await _userData.GetAsync(stepContext.Context, () => new UserData(), cancellationToken);

      //setting the Address.
      userData.Address = stepContext.Context.Activity.Text;

      await _userData.SetAsync(stepContext.Context, userData, cancellationToken);

      return await stepContext.PromptAsync("AskPhoneNumberPrompt", new PromptOptions {
        Prompt = MessageFactory.Text("What is your phone number?"), RetryPrompt = MessageFactory.Text("Incorrect Phone Number! Please provide your valid number..")
      }, cancellationToken);
    }

    private async Task < DialogTurnResult > AskUserConfirmationAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken) {
      //you need to save all answers using bot state and summarize the user information in the confirm state and show information in confirmStatement using C# string interpolation.
      var confirmStatement = $ "";

      return await stepContext.PromptAsync("ConfirmUserDetialsPrompt", new PromptOptions {
        Prompt = MessageFactory.Text(confirmStatement)
      }, cancellationToken);
    }

    private async Task < DialogTurnResult > AskOfficeTypeAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken) {
      var officeTypes = new List < Choice > ();

      officeTypes.Add(new Choice() {

        Value = "Home",

      });

      officeTypes.Add(new Choice() {
        Value = "Office",
      });

      return await stepContext.PromptAsync("AskTypeOfAdressPrompt", new PromptOptions {
        Prompt = MessageFactory.Text("Ok, What type of address is this?"), RetryPrompt = MessageFactory.Text("Sorry, Which type of address is this?"), Choices = officeTypes
      }, cancellationToken);
    }
  }
}



Output


Picture showing the output of prompts in microsoft bot framework
Click to Enlarge

Picture showing the output of prompts in microsoft bot framework
Click to Enlarge


Posted By  -  Karan Gupta
 
Posted On  -  Wednesday, December 30, 2020

Query/Feedback


Your Email Id  
 
Subject 
 
Query/FeedbackCharacters remaining 250