How can I use Twilio as a SIP trunk for my Asterisk to make and receive calls?

g3rv4 picture g3rv4 · Nov 7, 2013 · Viewed 18k times · Source

I have a Twilio account which has a number (let's say 8881231234), and I have Asterisk box. I'd like to use Twilio as an Asterisk trunk to be able to make calls at their rates and receive calls from that number on my Asterisk.

I haven't found any specs to interconnect Asterisk with Twilio. Is it possible to set up Asterisk so that every outgoing call is routed through Twilio and have the calls on my 8881231234 number ring on my SIP phone?

Answer

g3rv4 picture g3rv4 · Nov 7, 2013

As it was well pointed out below, Twilio now DOES WORK as a SIP trunk, you can visit https://www.twilio.com/docs/sip-trunking for more information.


THIS IS DEPRECATED!!! YOU SHOULD BE USING TWILIO's OWN SIP TRUNKING... READ HERE

Twilio doesn't work as a SIP trunk... it's aimed at developers who want to build applications over their services. However, nothing prevents you from building an app that lets you place outgoing calls through Twilio and receive calls from them on asterisk. You will need an HTTP server to return the XMLs Twilio requires.

Placing calls through Twilio

There are three steps for it:

  1. Create a domain at twilio and set up the users
  2. Set up your Asterisk so that when you dial a number, it reaches twilio
  3. Handle twilio's requests and return TwiML (a special kind of XML that's extremely well documented on their site). They have SDKs for almost any language, but I'll keep this response language-agnostic and deal only with TwiML

Create a domain at twilio

This should be pretty straightforward... Assuming you have a twilio account, just go to https://www.twilio.com/user/account/sip/domains and set up a domain. I have a dynamic ip, so I used a credential list (and will use that in the 2nd step). If you have static IP, I'd totally use that. I'm going to assume you called your domain "example" so I'll talk about the domain example.sip.twilio.com

As Voice URL, you should put a URL that points to your web server (yeah, you need one... but it can def be a cheap one) to a URL that can handle their request. On the third step I'll show you exactly what it should return.

Set up your Asterisk so that when you dial a number, it reaches twilio

The first thing you need to do is add a device on your sip.conf file

[twilio]
type = peer
username = myusername
remotesecret = mypassword
host = example.sip.twilio.com
qualify = no

and then, on your extensions.conf just use it (I'm using prefix 88 for twilio... so if I dial 8818881231235 it will call (888) 123-1235 in the US through twilio)

exten => _88.,1,Dial(SIP/${EXTEN:2}@twilio)

That's it... when you dial that, you reach twilio.

Handle twilio's requests and return TwiML

Twilio will invoke the url you set up on the first step. It does an HTTP POST that has as a parameter named To with the URL that your Asterisk invoked... so, if you dialed 8818881231235 you'd get in the To parameter

sip:[email protected]

You should get that parameter and extract the number, then just return

<?xml version="1.0" encoding="UTF-8"?>
<Response>
  <Dial callerId="+18881231234">
    <Number>18881231235</Number>
  </Dial>
</Response>

If you were using PHP, you could do something like this

<?xml version="1.0" encoding="UTF-8"?>
<Response>
  <Dial callerId="+18881231234">
    <Number><?php preg_match('/:([0-9]+)@/', $_POST['To'], $matches); echo $matches[1]; ?></Number>
  </Dial>
</Response>

Aaaand that's it, you're now using Twilio as a trunk to place calls.

Receiving calls from Twilio

Receiving calls from Twilio is easier (when you figured out a few things). First, you should set up a device for every IP they ask you to whitelist, so on your sip.conf you should add

[twiliocaller](!)
context = fromtwilio
type = peer
qualify=no
allowguest=yes
 
[twilioip-1](twiliocaller)
host=107.21.222.153
 
[twilioip-2](twiliocaller)
host=107.21.211.20
 
[twilioip-3](twiliocaller)
host=107.21.231.147
 
[twilioip-4](twiliocaller)
host=54.236.81.101
 
[twilioip-5](twiliocaller)
host=54.236.96.128
 
[twilioip-6](twiliocaller)
host=54.236.97.29
 
[twilioip-7](twiliocaller)
host=54.236.97.135
 
[twilioip-8](twiliocaller)
host=54.232.85.81
 
[twilioip-9](twiliocaller)
host=54.232.85.82
 
[twilioip-10](twiliocaller)
host=54.232.85.84
 
[twilioip-11](twiliocaller)
host=54.232.85.85
 
[twilioip-12](twiliocaller)
host=54.228.219.168
 
[twilioip-13](twiliocaller)
host=54.228.233.229
 
[twilioip-14](twiliocaller)
host=176.34.236.224
 
[twilioip-15](twiliocaller)
host=176.34.236.247
 
[twilioip-16](twiliocaller)
host=46.137.219.1
 
[twilioip-17](twiliocaller)
host=46.137.219.3
 
[twilioip-18](twiliocaller)
host=46.137.219.35
 
[twilioip-19](twiliocaller)
host=46.137.219.135
 
[twilioip-20](twiliocaller)
host=54.249.244.21
 
[twilioip-21](twiliocaller)
host=54.249.244.24
 
[twilioip-22](twiliocaller)
host=54.249.244.27
 
[twilioip-23](twiliocaller)
host=54.249.244.28

And you need to set up a number to call to your Asterisk. Assuming your Asterisk's domain is sip.example.com and you want twilio to ring extension 100, you need something that returns

<?xml version="1.0" encoding="UTF-8"?>
<Response>
  <Dial>
    <Sip>sip:[email protected]</Sip>
  </Dial>
</Response>

You don't need to use a server for this b/c twilio has twimlets... so, using the echo twimlet, you could set up your number to point to http://twimlets.com/echo?Twiml=%3C%3Fxml%20version%3D%221.0%22%20encoding%3D%22UTF-8%22%3F%3E%3CResponse%3E%3CDial%3E%3CSip%3Esip%3A100%40sip.example.com%3C%2FSip%3E%3C%2FDial%3E%3C%2FResponse%3E&

Aaand that's it... you should handle incoming calls at extension 100 in the context fromtwilio.

UPDATE: As pointed out by xinit on the comments, twilio may call you from any ip... even the one your domain (example.sip.twilio.com) points to... so you def want to update your [twilio] device by adding it the template. It should look like this now

[twilio](twiliocaller)
type = peer
username = myusername
remotesecret = mypassword
host = example.sip.twilio.com
qualify = no

If you're interested in how I got to this, take a look into these blog posts I did. Copied a lot from them to tweak this answer.

Making calls from home to twilio

Receiving calls from twilio