Created
March 28, 2019 10:42
-
-
Save mplacona/0336de35751935691ef62f6660f8f21f to your computer and use it in GitHub Desktop.
This file contains 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
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