Skip to content

Instantly share code, notes, and snippets.

@mplacona
Created March 28, 2019 10:42
Show Gist options
  • Save mplacona/0336de35751935691ef62f6660f8f21f to your computer and use it in GitHub Desktop.
Save mplacona/0336de35751935691ef62f6660f8f21f to your computer and use it in GitHub Desktop.
exports.handler = function (context, event, callback) {
/***** configuration *****/
const phoneNumber = 'YOUR_PHONE_NUMBER';
const timeout = event.Timeout || 12;
const secureRecordingLinks = false;
const voiceOpts = {
'voice': 'alice',
'language': 'en-US'
};
const reject = [
// To block a caller, add the E164 formatted number here
];
let rejectMessage = "You are calling from a restricted number. Goodbye.";
/***** end configuration *****/
let thisFunction = 'https://' + context.DOMAIN_NAME + '/personal-voicemail';
function shouldReject() {
return reject.length && event.From && reject.includes(event.From);
}
function rejectInbound() {
let twiml = new Twilio.twiml.VoiceResponse();
if (rejectMessage) {
twiml.say(rejectMessage, voiceOpts);
}
twiml.hangup();
return twiml;
}
function handleInbound() {
const dialParams = {
'action': thisFunction
};
if (event.CallerId) {
dialParams.callerId = event.CallerId;
}
if (timeout) {
dialParams.timeout = timeout;
}
const twiml = new Twilio.twiml.VoiceResponse();
twiml.dial(dialParams, phoneNumber);
return twiml;
}
function redirect() {
const twiml = new Twilio.twiml.VoiceResponse();
twiml.redirect(thisFunction);
return twiml;
}
switch (true) {
case event.CallStatus === 'queued':
callback(null, redirect());
break;
case event.CallStatus === 'ringing':
callback(null, shouldReject() ? rejectInbound() : handleInbound());
break;
default:
callback();
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment