forked from microsoft/BotBuilder-Samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
validators.js
49 lines (40 loc) · 1.72 KB
/
validators.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
var builder = require('botbuilder');
var PhoneRegex = new RegExp(/^(\+\d{1,2}\s)?\(?\d{3}\)?[\s.-]?\d{3}[\s.-]?\d{4}$/);
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('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 () {
return lib.clone();
};
module.exports.PhoneRegex = PhoneRegex;
module.exports.EmailRegex = EmailRegex;