Last active
January 10, 2021 02:40
-
-
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
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
const dns = require('dns'); | |
let sipUri = '[email protected]'; // 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