Easily create conversational experiences for users with Waterfall Dialogs in Microsoft Bot Framework. Streamline user interactions with step-by-step dialog flows.
What is Waterfall Dialog in Microsoft Bot Framework?
The Waterfall dialog is a way to build a conversational flow in the Microsoft Bot Framework. It allows you to prompt the user with a series of questions and collect input from them, step-by-step, until the conversation is complete. Each step in the Waterfall dialog is called a “prompt”, and the user’s response to each prompt is passed to the next step in the dialog. This makes it easy to manage a multi-step conversation in your bot. The Waterfall dialog is a built-in dialog in the Microsoft Bot Framework and can be easily added to your bot’s code.
What are the different prompts in Waterfall Dialog?
The Microsoft Bot Framework provides several built-in prompts for use in a Waterfall dialog, including:
TextPrompt
– Prompts the user for text input.NumberPrompt
– Prompts the user for a number.ConfirmPrompt
– Prompts the user for confirmation (yes/no).ChoicePrompt
– Prompts the user to select from a list of options.DateTimePrompt
– Prompts the user for a date or time.AttachmentPrompt
– Prompts the user for an attachment or file.
You can also create custom prompts to fit specific needs. The prompt simply needs to inherit from the Prompt
class and provide the necessary logic to receive and validate the user’s input.
C# Code example for using Prompts in Waterfall Dialog
Here’s an example of each of the built-in prompts in C# using the Microsoft Bot Framework:
TextPrompt
:
private async Task<DialogTurnResult> AskForNameStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
{
return await stepContext.PromptAsync(
nameof(TextPrompt),
new PromptOptions
{
Prompt = MessageFactory.Text("What's your name?"),
},
cancellationToken);
}
NumberPrompt
:
private async Task<DialogTurnResult> AskForAgeStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
{
return await stepContext.PromptAsync(
nameof(NumberPrompt),
new PromptOptions
{
Prompt = MessageFactory.Text("What's your age?"),
},
cancellationToken);
}
ConfirmPrompt
:
private async Task<DialogTurnResult> AskForConfirmationStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
{
return await stepContext.PromptAsync(
nameof(ConfirmPrompt),
new PromptOptions
{
Prompt = MessageFactory.Text("Are you sure you want to continue?"),
},
cancellationToken);
}
ChoicePrompt
:
private async Task<DialogTurnResult> AskForColorStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
{
return await stepContext.PromptAsync(
nameof(ChoicePrompt),
new PromptOptions
{
Prompt = MessageFactory.Text("What's your favorite color?"),
Choices = ChoiceFactory.ToChoices(new List<string>
{
"Red",
"Green",
"Blue",
"Yellow",
}),
},
cancellationToken);
}
AttachmentPrompt
:
private async Task<DialogTurnResult> AskForAttachmentStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
{
return await stepContext.PromptAsync(
nameof(AttachmentPrompt),
new PromptOptions
{
Prompt = MessageFactory.Text("Please upload an image."),
},
cancellationToken);
}
C# Code example for retrieving the user input in Waterfall Dialog
Here’s an example of how to retrieve the user input from each of the built-in prompts in a subsequent step in the Waterfall dialog:
TextPrompt
:
private async Task<DialogTurnResult> ShowNameStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
{
var name = (string)stepContext.Result;
await stepContext.Context.SendActivityAsync(MessageFactory.Text($"Your name is {name}."), cancellationToken);
return await stepContext.NextAsync(cancellationToken: cancellationToken);
}
NumberPrompt
:
private async Task<DialogTurnResult> ShowAgeStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
{
var age = (int)stepContext.Result;
await stepContext.Context.SendActivityAsync(MessageFactory.Text($"Your age is {age}."), cancellationToken);
return await stepContext.NextAsync(cancellationToken: cancellationToken);
}
ConfirmPrompt
:
private async Task<DialogTurnResult> ShowConfirmationStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
{
var confirmed = (bool)stepContext.Result;
await stepContext.Context.SendActivityAsync(MessageFactory.Text($"You confirmed: {confirmed}."), cancellationToken);
return await stepContext.NextAsync(cancellationToken: cancellationToken);
}
ChoicePrompt
:
private async Task<DialogTurnResult> ShowColorStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
{
var color = ((FoundChoice)stepContext.Result).Value;
await stepContext.Context.SendActivityAsync(MessageFactory.Text($"Your favorite color is {color}."), cancellationToken);
return await stepContext.NextAsync(cancellationToken: cancellationToken);
}
AttachmentPrompt
:
private async Task<DialogTurnResult> ShowAttachmentStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
{
var attachments = (IList<Attachment>)stepContext.Result;
await stepContext.Context.SendActivityAsync(MessageFactory.Text($"You uploaded {attachments.Count} attachments."), cancellationToken);
return await stepContext.NextAsync(cancellationToken: cancellationToken);
}