Microsoft Bot Framework: How to validate user response to bot Prompts?

Microsoft Bot Framework has multiple prompts to get user response while having the conversation such as Prompts.textPrompts.numberPrompts.ChoicePrompts.confirm

But there is automatic validation for number, choice and confirm prompt. For example if the user responds with wrong choice then the chat bot will re-prompt the choices and ask yo to select again. Check documentation here.

This post will show how can we put validation response which we get form the Prompts.text

Bot Framework provides validatedPrompt  which is a function which validates the user prompt and returns the response back if the condition is true.

here is an example,

bot.dialog('/getDetail', [
    function (session) {
      session.beginDialog('/validateAge', { prompt: "What's your age?" });

      //if false response, then prmopts "I did not understand {age}""    

    },
    function (session, results) {

        if (results.response) {
            session.send("Thank you for adding your age");
        } 
    }
]).triggerAction({
     matches: /^lets validate$/i

})


bot.dialog('/validateAge', builder.DialogAction.validatedPrompt(builder.PromptType.text, function (response) {
   if(response> 0 && response < 70){

    return response;
   }
}));

Source: My question and answer on StackOverflow here

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s