Skip to content

Instantly share code, notes, and snippets.

@charsi
Last active January 10, 2021 02:40
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save charsi/222e6e24577d61d70afac6406d54973d to your computer and use it in GitHub Desktop.
Save charsi/222e6e24577d61d70afac6406d54973d to your computer and use it in GitHub Desktop.
Twilio function to forward calls to a SIP host that uses SRV (RFC 3263) records for load balancing. Twilio doesn't natively support SRV for looking up SIP servers. So, calls made to most SIP hosts result in a DNS failure. This script is a workaround for that issue. https://stackoverflow.com/questions/48160769
const dns = require('dns');
let sipUri = '1777xxxxxxx112@in.callcentric.com'; // Any sip uri using srv records should work
let protocol = 'udp';
let region = 'us2' ;
exports.handler = function(context, event, callback) {
var user = sipUri.split('@')[0];
var host = sipUri.split('@')[1];
// generate the TwiML to tell Twilio how to forward this call
let twiml = new Twilio.twiml.VoiceResponse();
const dial = twiml.dial({
answerOnBridge: 'true' // Don't answer incoming call until outgoing call is answered
});
dns.resolveSrv('_sip._'+protocol+'.'+host, (err, addresses) => { // Get SRV records for host
var resolvedhost = addresses[0].name+':'+addresses[0].port; // Fisrt SRV record returned
dial.sip('sip:'+user+'@'+resolvedhost+';region='+region);
console.log(twiml.toString());
// return the TwiML
callback(null, twiml);
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment