Skip to content

Instantly share code, notes, and snippets.

@SalesforceBobLightning
Last active February 1, 2019 08:54
Show Gist options
  • Save SalesforceBobLightning/9681bf5581e29f53aac91a12777b10ca to your computer and use it in GitHub Desktop.
Save SalesforceBobLightning/9681bf5581e29f53aac91a12777b10ca to your computer and use it in GitHub Desktop.
Twilio Flex Function to route calls to a Voicemail TwiML on TaskRouter Workflow timeout
const VOICEMAIL_TWIML_URL = 'https://handler.twilio.com/twiml/XXXXXXXXXXXXXXXXXXXXXXXXXXXX';
process.on('unhandledRejection', error => {
console.log('unhandledRejection', error.message);
});
exports.handler = function (context, event, callback) {
var twiml = new Twilio.twiml.VoiceResponse();
const client = context.getTwilioClient();
if (event.DialCallStatus === "completed") {
twiml.hangup();
callback(null, twiml);
} else if (event.EventType == "reservation.timeout" || event.EventType == "workflow.timeout") {
console.log(event.EventType);
console.log('timeout');
const attributes = JSON.parse(event.TaskAttributes);
client.calls(attributes.call_sid)
.update({ method: 'POST', url: VOICEMAIL_TWIML_URL })
.then(call => {
console.log(call.to)
callback(null, twiml);
})
.catch(error => callback(error));
} else {
callback(null, twiml);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment