Receive incoming calls from Twilio with Asterisk

Twilio is a great service which is aimed at developers who want to integrate phone calls and text messaging into their application with ease.

It can also be used to create phone systems, with which you can control and route calls dynamically using XML files (known as TwiML).

If you want to terminate your calls to IP Phones, Twilio can direct calls to SIP. In order for this to work, you’ll need Asterisk or some asterisk-based distribution.

Twilio have many gateways which calls may originate from, 7 of them are currently in use, 16 of them are not. Sadly means that we need to create 7 peers (minimum) in order to receive calls from all gateways, however asterisk makes it simpler.

The following code needs to be entered into your sip.conf, please set the secret to something of your own choice.

I have added the IPs for the gateways that are not yet in use – whilst this isn’t needed, it’s recommended.

[twilio](!)
type=peer
fromuser=twilio
secret=xxxxxxx
context=incoming
insecure=invite

; north american gateways - currently the only ones in use
[twilio-1](twilio)
host = 107.21.222.153
[twilio-2](twilio)
host = 107.21.211.20
[twilio-3](twilio)
host = 107.21.231.147
[twilio-4](twilio)
host = 54.236.81.101
[twilio-5](twilio)
host = 54.236.96.128
[twilio-6](twilio)
host = 54.236.97.29
[twilio-7](twilio)
host = 54.236.97.135

; south american gateways - not currently used but added for future use
[twilio-8](twilio)
host = 54.232.85.81
[twilio-9](twilio)
host = 54.232.85.82
[twilio-10](twilio)
host = 54.232.85.84
[twilio-11](twilio)
host = 54.232.85.85

; european gateways - not currently used but added for future use
[twilio-12](twilio)
host = 54.228.219.168
[twilio-13](twilio)
host = 54.228.233.229
[twilio-14](twilio)
host = 5176.34.236.224
[twilio-15](twilio)
host = 176.34.236.247

; asian gateways - not currently used but added for future use
[twilio-16](twilio)
host = 46.137.219.1
[twilio-17](twilio)
host = 46.137.219.3
[twilio-18](twilio)
host = 46.137.219.35
[twilio-19](twilio)
host = 46.137.219.135
[twilio-20](twilio)
host = 54.249.244.21
[twilio-21](twilio)
host = 54.249.244.24
[twilio-22](twilio)
host = 54.249.244.27
[twilio-23](twilio)
host = 54.249.244.28

Now, you need to enter some lines into your extensions.conf in order to tell the incoming calls where to go.

I have used the context incoming, but you can use whatever you want but remember to change all references to it.

[incoming]
exten => 441234567890,1,Dial(SCCP/100)
same => n,Hangup

You will see I have used ‘441234567890’ as the extension, this can be anything but using the actual number is useful.

I have my calls going to a Cisco IP Phone that uses the Skinny Protocol, however, if you were using SIP you could use Dial(SIP/100).

If you have multiple numbers that you want to be terminated to SIP but want them to be dealt with differently in asterisk make sure you use different extensions.

Now for the TwiML to connect calls.

<?xml version="1.0" encoding="UTF-8"?>
<Response>
<Dial>
<Sip username="twilio" password="xxxxxxx">sip:441234567890@123.123.123.123</Sip>
</Dial>
</Response>

Make sure the username and password are the same as what you have set in sip.conf, and you’ll need to change the SIP URI to include your extension and IP address.

You should now be able to receive incoming calls to asterisk from Twilio.

Please leave a comment if you are having any problems and I’ll try my best to assist.