Botbuilder-samples: Know the number of attempts inside a validation

Created on 20 Feb 2019  路  3Comments  路  Source: microsoft/BotBuilder-Samples

Is your feature request related to a problem? Please describe.
Know the number of attempts in the same validation.

Describe the solution you'd like
PromptValidatorContext should have the number of attempts in the same validation.

Additional context

AddDialog(new TextPrompt(PromptKeys.UserCode, ValidateUserCode))
...

 private static async Task<bool> ValidateUserCode(PromptValidatorContext<string> promptContext, CancellationToken cancellationToken)
        {
            var value = promptContext.Recognized.Value?.Trim() ?? string.Empty;
            var regex = @"(CI)[0-9]*";
            var match = Regex.Match(value, regex, RegexOptions.IgnoreCase);

            if (match.Success)
            {
                promptContext.Recognized.Value = value;
                return true;
            }

            await promptContext.Context.SendActivityAsync(BotStrings.SharedValidateUserCode);
            return false;
        }

We have a code similar to this and we want to cancel the dialog after some attempts.
Example:

 private static async Task<bool> ValidateUserCode(PromptValidatorContext<string> promptContext, CancellationToken cancellationToken)
        {
            if (numberOfAttempts > 5) {
                // cancel everything
                promptContext.EndDialogAsync();
           }
            var value = promptContext.Recognized.Value?.Trim() ?? string.Empty;
            var regex = @"(CI)[0-9]*";
            var match = Regex.Match(value, regex, RegexOptions.IgnoreCase);

            if (match.Success)
            {
                promptContext.Recognized.Value = value;
                return true;
            }

            await promptContext.Context.SendActivityAsync(BotStrings.SharedValidateUserCode);
            return false;
        }
4.4 P1 customer-reported

Most helpful comment

All 3 comments

Hey guys,
I'm not sure if I'm missing something here, but I can't find a way to actually leave the validator and end the dialog. @CKGrafico, in you're example you suggest to use "promptContext.EndDialogAsync();" after 5 retries. Actually, the prompt context does not derive form the DialogContext, so the EndDialog method is not available. Is there any other way to get out of the custom validator? If not, what's the point of having an attempt counter? :-)

Could you give me a hint, @johnataylor ?

Was this page helpful?
0 / 5 - 0 ratings