Skip to content

Commit

Permalink
Replace DialogAction.validatedPrompt with Custom Prompts
Browse files Browse the repository at this point in the history
  • Loading branch information
pcostantini committed May 16, 2017
1 parent 594727c commit 8a6e8aa
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 22 deletions.
12 changes: 4 additions & 8 deletions Node/demo-ContosoFlowers/bot/dialogs/details.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,14 @@ lib.dialog('/', [
session.dialogData.recipientLastName = args.response;
session.beginDialog('validators:phonenumber', {
prompt: session.gettext('ask_recipient_phone_number'),
retryPrompt: session.gettext('invalid_phone_number'),
maxRetries: Number.MAX_VALUE
retryPrompt: session.gettext('invalid_phone_number')
});
},
function (session, args) {
session.dialogData.recipientPhoneNumber = args.response;
session.beginDialog('validators:notes', {
prompt: session.gettext('ask_note'),
retryPrompt: session.gettext('invalid_note'),
maxRetries: Number.MAX_VALUE
retryPrompt: session.gettext('invalid_note')
});
},
function (session, args) {
Expand Down Expand Up @@ -84,8 +82,7 @@ lib.dialog('sender', [
}
session.beginDialog('validators:email', {
prompt: session.gettext('ask_email'),
retryPrompt: session.gettext('invalid_email'),
maxRetries: Number.MAX_VALUE
retryPrompt: session.gettext('invalid_email')
});
},
function (session, args, next) {
Expand All @@ -95,8 +92,7 @@ lib.dialog('sender', [
session.dialogData.email = args.response;
session.beginDialog('validators:phonenumber', {
prompt: session.gettext('ask_phone_number'),
retryPrompt: session.gettext('invalid_phone_number'),
maxRetries: Number.MAX_VALUE
retryPrompt: session.gettext('invalid_phone_number')
});
},
function (session, args, next) {
Expand Down
48 changes: 34 additions & 14 deletions Node/demo-ContosoFlowers/bot/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,40 @@ var EmailRegex = new RegExp(/[a-z0-9!#$%&'*+\/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+\/

var lib = new builder.Library('validators');

lib.dialog('notes',
builder.DialogAction.validatedPrompt(builder.PromptType.text, function (response) {
return response && response.length <= 200;
}));

lib.dialog('phonenumber',
builder.DialogAction.validatedPrompt(builder.PromptType.text, function (response) {
return PhoneRegex.test(response);
}));

lib.dialog('email',
builder.DialogAction.validatedPrompt(builder.PromptType.text, function (response) {
return EmailRegex.test(response);
}));
lib.dialog('notes', basicPrompterWithExpression(function (input) {
return input && input.length <= 200;
}));

lib.dialog('phonenumber', basicPrompterWithRegex(PhoneRegex));

lib.dialog('email', basicPrompterWithRegex(EmailRegex));

function basicPrompterWithRegex(regex) {
return new builder.IntentDialog()
.onBegin(function (session, args) {
session.dialogData.retryPrompt = args.retryPrompt;
session.send(args.prompt);
}).matches(regex, function (session) {
session.endDialogWithResult({ response: session.message.text });
}).onDefault(function (session) {
session.send(session.dialogData.retryPrompt);
});
}

function basicPrompterWithExpression(expression) {
return new builder.IntentDialog()
.onBegin(function (session, args) {
session.dialogData.retryPrompt = args.retryPrompt;
session.send(args.prompt);
}).onDefault(function (session) {
var input = session.message.text;
if (expression(input)) {
session.endDialogWithResult({ response: input });
} else {
session.send(session.dialogData.retryPrompt);
}
});
}

// Export createLibrary() function
module.exports.createLibrary = function () {
Expand Down

0 comments on commit 8a6e8aa

Please sign in to comment.