Aleksey Kontsevich
Aleksey Kontsevich

Reputation: 4991

Data validation before next dialog waterfall step in Bot Framework

Have simple waterfall dialog:

SendMessageDialog = [
    function (session) {
        builder.Prompts.time(session, "Enter dates?");
    },
    function (session, results) {
        session.conversationData.start = builder.EntityRecognizer.resolveTime([results.response]).toISOString();

        if(typeof results.response.resolution.end != "undefined")
            session.conversationData.end = results.response.resolution.end.toISOString();
    }
];

Bot successfully recognizes time in different formats, and if format is invalid makes default prompt to user proposing to re-enter data like:

I didn't understand. Please choose an option from the list.

In Prompts option I can only change this default retryPrompt message. What if I need additional validation like:

Is there is an easy way to add additional validation to retry same waterfall step and ask user to re-enter data? How to implement this? Is there a workable code for BotBuilder 3.9?

There are some examples exist to make some validations with LUIS API calls, however they work only on next waterfall step. Goal not to go to the next step until correct data entered - is it possible? Thanks!

Upvotes: 1

Views: 2243

Answers (1)

Aleksey Kontsevich
Aleksey Kontsevich

Reputation: 4991

Right after the question was asked, had found how-to Create custom prompts to validate input:

Result code:

[
    function (session) {
        // Call start/end time prompt dialog
        session.beginDialog("DatePromptDialog");
    },
    ...
]

DatePromptDialog = [
    function (session, args) {
        var options = { retryPrompt: "I didn’t recognize dates you entered. Please try again using format: start - end dates" };

        if (args && args.reprompt && args.endTimeMissed) {
            builder.Prompts.time(session, "Please specify both start - end times:", options);
        } else if (args && args.reprompt && args.dateInPast){
            builder.Prompts.time(session, "That date seems to be in the past! Please enter a valid date.", options);
        } else {
            builder.Prompts.time(session, "Enter dates?", options);
        }
    },
    function (session, results) {
        var args = {};
        delete session.conversationData.start;  // Clear previous values
        delete session.conversationData.end;

        // Get start time
        session.conversationData.start = builder.EntityRecognizer.resolveTime([results.response]).toISOString();
        // Get duration end time if available
        if(typeof results.response.resolution.end != "undefined")
            session.conversationData.end = results.response.resolution.end.toISOString();
        else {
            args.endTimeMissed = true;
            args.reprompt = true;
        }

        // Convert dates from string
        var currDate = new Date(); // Current date
        var startDate = new Date(session.conversationData.start);
        var endDate = new Date(session.conversationData.end);

        if(startDate < currDate || endDate < currDate) {
            args.dateInPast = true;
            args.reprompt = true;
        }

        if (args.reprompt) {
            // Repeat the dialog
            session.replaceDialog('DatePromptDialog', args);
        } else {
            // Success
            session.endDialog();
        }
    }
];

Upvotes: 1

Related Questions