0
votes

Hi I am implementing a a bot using .net core. I am using the waterfall dialog in order to have my conversation. The thing is that between one step and the other I need to validate the information received with my db. I try using validations in the prompt options but I am not able to do it works. can someone help me. Here is my code.

public class DetectProblemStep : ComponentDialog
{
    // Define a "done" response for the company selection prompt.
    private const string DoneOption = "done";

    // Define value names for values tracked inside the dialogs.
    private const string UserInfo = "value-userInfo";

    public DetectProblemStep()
        : base(nameof(DetectProblemStep))
    {
        AddDialog(new TextPrompt(nameof(TextPrompt)));
        AddDialog(new NumberPrompt<int>(nameof(NumberPrompt<int>)));

        AddDialog(new ReviewSelectionDialog());

        AddDialog(new WaterfallDialog(nameof(WaterfallDialog), new WaterfallStep[]
        {
            NameStepAsync,
            EmailStepAsync
        }));

        InitialDialogId = nameof(WaterfallDialog);
    }

    private static async Task<DialogTurnResult> NameStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
    {
        // Create an object in which to collect the user's information within the dialog.
        stepContext.Values[UserInfo] = new UserProfile();

        var promptOptions = new PromptOptions { Prompt = MessageFactory.Text("Please enter your name."),
            Validations = nameof(NameValidation)
        };

        // Ask the user to enter their name.
        return await stepContext.PromptAsync(nameof(TextPrompt), promptOptions, cancellationToken);
    }

    private static async Task<DialogTurnResult> EmailStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
    {
        // Create an object in which to collect the user's information within the dialog.
        stepContext.Values[UserInfo] = new UserProfile();

        var promptOptions = new PromptOptions
        {
            Prompt = MessageFactory.Text("Please enter your Email."),
        };

        // Ask the user to enter their name.
        return await stepContext.PromptAsync(nameof(TextPrompt), promptOptions, cancellationToken);
    }



    public Task<bool> NameValidation(PromptValidatorContext<string> promptContext, CancellationToken cancellationToken)
    {

        return Task.FromResult(false);
    }


}

}

1
Why don't you make one of the waterfall steps a validation step? Make a call to your db with the information from the previous step at the 'start' of the chosen waterfall step, and then send to the cx 'Confirming!' or 'Please wait!', then move on to the next step.JJ_Wailes

1 Answers

0
votes

Unfortunately, the documentation is far from clear about this.

The Validations-object is used to pass objects to your custom validator function. This is useful when you want change you validation at runtime. That is, validation depends on information you don't have when adding the Prompt to the DialogSet (which you do in the constructor).

If your validation can be determined when your prompt is added to the DialogSet, you just do:

AddDialog(new TextPrompt(nameof(TextPrompt), NameValidation));

But if you want to add additional validation to the prompt when you actually call the PromptAsync(), you can pass anything to you validator like this:

private static async Task<DialogTurnResult> NameStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
{
    // Create an object in which to collect the user's information within the dialog.
    stepContext.Values[UserInfo] = new UserProfile();

    var validations = new AdditionalValidationOptions { MinLength = 3 };

    var promptOptions = new PromptOptions { Prompt = MessageFactory.Text("Please enter your name."),
        Validations = validations 
    };

    // Ask the user to enter their name.
    return await stepContext.PromptAsync(nameof(TextPrompt), promptOptions, cancellationToken);
}

public Task<bool> NameValidation(PromptValidatorContext<string> promptContext, CancellationToken cancellationToken)
{
    var validations = (AdditionalValidationOptions)promptContext.Options.Validations;

    return Task.FromResult(promptContext.Recognized.Value >= validations.MinLength);
}

and define a class

public class AdditionalValidationOptions 
{
    public int MinLength {get; set;}
}