Last active
August 10, 2017 11:45
-
-
Save jazzedge/753178e0958c0092235b186f47a2620b to your computer and use it in GitHub Desktop.
Bot - Libraries #2 email validation as an external library
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
https://github.com/Microsoft/BotBuilder-Samples/tree/master/Node/demo-ContosoFlowers | |
This is how you could package an email validation: | |
var EmailRegex = new RegExp(/[a-z0-9!#$%&'*+\/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+\/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/); | |
var lib = new builder.Library('validators'); | |
lib.dialog('email', | |
new builder.IntentDialog() | |
.onBegin(function (session, args) { | |
session.dialogData.retryPrompt = args.retryPrompt; | |
session.send(args.prompt); | |
}).matches(EmailRegex, function (session) { | |
session.endDialogWithResult({ response: session.message.text }); | |
}).onDefault(function (session) { | |
session.send(session.dialogData.retryPrompt); | |
})); | |
// Export createLibrary() function | |
module.exports.createLibrary = function () { | |
return lib.clone(); | |
}; | |
And this is how you can call the validator from your existing code: | |
// Waterfall Dialog | |
[ | |
function (session) { | |
session.beginDialog('validators:email', { | |
prompt: 'What\'s your email?', | |
retryPrompt: 'Something is wrong with that email address. Please try again.' | |
}); | |
}, | |
function (session, args, next) { | |
var email = args.response; | |
// TODO: Save email address | |
// ... | |
} | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment