Scorpio
Scorpio

Reputation: 23

c# chat bot | RetryPrompt message dynamically

c# chat bot : is there any way that we can control choice prompt's RetryPrompt message dynamically? I am using bot framework 4.0.

Upvotes: 1

Views: 323

Answers (1)

mdrichardson
mdrichardson

Reputation: 7241

There's a couple of different ways to do this, depending on how I'm interpreting your question.

The easiest is to just add a separate RetryPrompt. For example, if we want to do this to the Multi-Turn-Prompt sample, we just add the RetryPrompt property:

private static async Task<DialogTurnResult> TransportStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
        {
            // WaterfallStep always finishes with the end of the Waterfall or with another dialog; here it is a Prompt Dialog.
            // Running a prompt here means the next WaterfallStep will be run when the users response is received.
            return await stepContext.PromptAsync(nameof(ChoicePrompt),
                new PromptOptions
                {
                    Prompt = MessageFactory.Text("Please enter your mode of transport."),
                    Choices = ChoiceFactory.ToChoices(new List<string> { "Car", "Bus", "Bicycle" }),
                    RetryPrompt = MessageFactory.Text("That wasn't a valid option. Try again.")
                }, cancellationToken);
        }

This produces:

enter image description here

The other alternative would be to do something like what @pkr2000 said (although a little different), and use a custom validator to dynamically add the RetryPrompt. Something like:

AddDialog(new ChoicePrompt(nameof(ChoicePrompt), ValidateChoicesAsync));

[...]

private static Task<bool> ValidateChoicesAsync(PromptValidatorContext<FoundChoice> promptContext, CancellationToken cancellationToken)
{
    if (!promptContext.Recognized.Succeeded)
    {
        promptContext.Options.RetryPrompt = MessageFactory.Text($"You said \"{ promptContext.Context.Activity.Text},\" which is invalid. Please try again.");
        return Task.FromResult(false);
    }
    return Task.FromResult(true);
}

This produces:

enter image description here

You can do just about anything you want within the validator. Instead of using MessageFactory.Text(), you can pass in a completely different Activity like an Adaptive Card or something. You could also not set a RetryPrompt, instead changing the Prompt to whatever text/activity you want, return false, and then the user gets re-prompted with the new Prompt. It's really pretty limitless what you can do with a custom validator.

Upvotes: 1

Related Questions