在转发到SIP URI

时间:2018-01-09 02:43:29

标签: twilio twilio-functions

我正试图从我的twilio号码转发我的sip设备。然而,这目前无法正常工作,因为Twilio不喜欢SIP提供商使用负载均衡网址。在我的例子中,sip uri以@in.callcentric.com结尾,而@alpha11.callcentric.com:5070又向三个服务器发送请求。如果我直接使用其中一个服务器,即@in.callcentric.com而不是delete,则可以使用。然而,这不是理想的imo。

我找到了一个执行SRV查找的节点模块。这是我可以用twilio函数来解决问题的吗?

有没有办法强制twilio查找SRV记录并自动使用它来转发呼叫?

Relevant link

1 个答案:

答案 0 :(得分:1)

使用NPM内置的dns module我能够让事情发挥作用。这就是我的功能现在的样子。似乎工作正常。

const dns = require('dns');

let sipUri = '1777xxxxxxxxxx@in.callcentric.com';
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();

  dns.resolveSrv('_sip._'+protocol+'.'+host, (err, addresses) => {
    var resolvedhost = addresses[0].name+':'+addresses[0].port;
    dial.sip('sip:'+user+'@'+resolvedhost+';region='+region);
    console.log(twiml.toString());
    // return the TwiML
    callback(null, twiml);
  });
};

这会手动查询SRV记录的主机名,然后使用返回的第一个结果。不考虑重量和优先级。

Gist