-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainmenu.js
60 lines (54 loc) · 1.7 KB
/
mainmenu.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
50
51
52
53
54
55
56
57
58
59
60
/*
** Validate all extensions here. Do not proceed without validating it is real!
** Virtually all of the security is implemented here (example, max invalids)
*/
exports.handler = function(context, event, callback) {
var configurl = context.CONFIG_URL;
if (event.Digits == null) {
getExten(context, event, callback, false);
return;
}
var got = require('got');
got(configurl, {json: true})
.then(function(response) {
routeExten(context, event, callback, response.body);
});
};
function getExten(context, event, callback, invmsg) {
var menuloopiter;
if (event.menuloopiter == null) {
menuloopiter = 1;
} else {
menuloopiter = parseInt(event.menuloopiter) + 1;
}
let twiml = new Twilio.twiml.VoiceResponse();
let msg = "";
if (invmsg) {
msg += "Invalid extension... ";
}
if (menuloopiter > context.MAX_MENULOOPITER) {
msg += "Too many attempts... Goodbye!";
twiml.say(msg);
callback(null, twiml);
return;
}
if (invmsg) {
msg += "Please try again....";
}
msg += "Please enter the extension you wish to reach.";
let nextUrl = "/mainmenu?menuloopiter=" + menuloopiter;
twiml.gather({numDigits: 4, action: nextUrl, timeout:10}).say(msg);
twiml.redirect(nextUrl);
callback(null, twiml);
}
function routeExten(context, event, callback, extenmap) {
var exten = event.Digits;
if (!(exten in extenmap)) {
console.log("Got extension " + exten);
getExten(context, event, callback, true);
return;
}
let twiml = new Twilio.twiml.VoiceResponse();
twiml.redirect("/rroncall?exten=" + exten);
callback(null, twiml);
}