Search code examples
c#botframeworkazure-bot-service

Set number of attempts to ConfirmPrompt in Microsoft Bot v4


ConfirmPrompt in MicrosoftBot v4 has no way to set the number of attempts to retry.

As understood from MicrosoftBot Framework v3, we have an option to set 'attempts' to retry the dialog. But I am not seeing a similar feature in v4. The documentation does not provide a clarity on the same.

private async Task<DialogTurnResult> Step2Async(WaterfallStepContext stepContext, CancellationToken cancellationToken)
        {
            var opts = new PromptOptions
            {
                Prompt = MessageFactory.Text("Do you want to continue the conversation?"),
                RetryPrompt = MessageFactory.Text("Sorry, I did not understand. Do u want me to continue this conversation?"),

            };
            return await stepContext.PromptAsync("ConfirmPrompt", opts, cancellationToken);
        }
private async Task<DialogTurnResult> Step3Async(WaterfallStepContext stepContext, CancellationToken cancellationToken)
        {

            var selectedchoice = (stepContext.Result as FoundChoice).Value;
            selectedchoice = (selectedchoice as string).ToLower();

            if (selectedchoice.Contains("yes"))
            {
                await stepContext.Context.SendActivityAsync(selectedchoice);
            }
            else if (selectedchoice.Contains("no"))
            {
                await stepContext.Context.SendActivityAsync(selectedchoice);
            } else
            {
               // I will find the Intent through LUIS to understand what the User is trying to say.
            }
            return await stepContext.EndDialogAsync();
        }

Currently, the retry option is unlimited attempts and unless the User the selects from the given list, the dialog does not exit.

I am expecting that the Retry option should be limited to 2 times and then should exit / end the dialog.


Solution

  • I had a look to the sources of Bot Builder (.Net version) here, in particular in the Prompts section, and it looks like I can't find the tooManyAttempts exception that was existing in Bot v3 as you mentioned.

    For those interested, it was looking like this in v3 (sources):

    protected virtual async Task MessageReceivedAsync(IDialogContext context, IAwaitable<IMessageActivity> message)
    {
        T result;
        if (this.TryParse(await message, out result))
        {
            context.Done(result);
        }
        else
        {
            --promptOptions.Attempts;
            if (promptOptions.Attempts >= 0)
            {
                await context.PostAsync(this.MakePrompt(context, promptOptions.Retry ?? promptOptions.DefaultRetry, promptOptions.Choices?.Keys.ToList().AsReadOnly(), promptOptions.Descriptions, promptOptions.RetrySpeak ?? promptOptions.DefaultRetrySpeak));
                context.Wait(MessageReceivedAsync);
            }
            else
            {
                //too many attempts, throw.
                await context.PostAsync(this.MakePrompt(context, promptOptions.TooManyAttempts));
                throw new TooManyAttemptsException(promptOptions.TooManyAttempts);
            }
        }
    }
    

    As you can see in v4 Github's issues and commits, there has been some recent discussions / work about that:

    So with this field, you should be able to implement your own logic on "stopping retries policy"