Search code examples
botframeworkazure-bot-service

How do i prompt user to re-enter input for previous waterfall step if he/she finds the input entered to be incorrect


The scenario is as follows:

In case the input provided by user seems incorrect in a waterfall step (e.g. incorrect name, incorrect date) User would like to enter the last input again by saying “ (reenter)” or (enter again) .

It’s like if there are three steps in waterfall dialog: Step1->Step2->Step3

After providing input for Step2, if user sees the input to be incorrect he would like to interrupt the flow by saying any of the words mentioned above to enter input for previous step.

I could not find a way to go back to previous waterfall step.

There are few solution available to go back to previous step but I was unable to replicate the same in python.

Link1:- Bot framework v4.0 how to execute the previous waterfall step in a dialog

Link2:- https://pauliom.com/2018/08/08/manipulating-waterfall-steps-botframework-v4/

I tried handling the scenario using interrupt but the issue persists as the next Turn continues from where the conversation left off.


Solution

  • What you can use is Validators, check this article for more information or find below code snippets [c#] as a summary.
    I have also added a Python snippet in the end

    When a user replies to your prompt the validation will be triggered. If the validation returns false, a retry prompt will be sent to the user

    There is no need to go back to the previous step if you implement validators

                AddDialog(new TextPrompt("TextPromptId", UserNameValidation));
                AddDialog(new NumberPrompt<int>("NumberPromptId", MobileNumberValidation));
                AddDialog(new ChoicePrompt("ChoicePromptId", ChoiceValidataion));
    

    Username Validator:

    
        private Task<bool> UserNameValidation(PromptValidatorContext<string> promptContext, CancellationToken cancellationToken)  
               {  
                   return Task.FromResult(true);  
               } 
    

    Choice Validator:

    
    
        private Task<bool> ChoiceValidataion(PromptValidatorContext<FoundChoice> promptContext, CancellationToken cancellationToken)  
                {  
                    return Task.FromResult(true);  
                }  
    
    

    Mobile Phone Validator:

    
    
    
        private async Task<bool> MobileNumberValidation(PromptValidatorContext<int> promptcontext, CancellationToken cancellationtoken)  
                {  
                    if (!promptcontext.Recognized.Succeeded)  
                    {  
                        await promptcontext.Context.SendActivityAsync("Hello, Please enter the valid mobile no",  
                            cancellationToken: cancellationtoken);  
    
                        return false;  
                    }  
    
                    int count = Convert.ToString(promptcontext.Recognized.Value).Length;  
                    if (count != 10)  
                    {  
                        await promptcontext.Context.SendActivityAsync("Hello , you are missing some number !!!",  
                            cancellationToken: cancellationtoken);  
                        return false;  
                    }  
    
                    return true;  
                }   
    
    

    I am not a Python developer but you find a python sample in Here in the Python tab and the below snippet:

    async def age_prompt_validator(prompt_context: PromptValidatorContext) -> bool:
        # This condition is our validation rule. You can also change the value at this point.
        return (
            prompt_context.recognized.succeeded
            and 0 < prompt_context.recognized.value < 150
        )
    

    Hope that helps