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.
; north american gateways - currently the only ones in use
host = 184.108.40.206
host = 220.127.116.11
host = 18.104.22.168
host = 22.214.171.124
host = 126.96.36.199
host = 188.8.131.52
host = 184.108.40.206
; south american gateways - not currently used but added for future use
host = 220.127.116.11
host = 18.104.22.168
host = 22.214.171.124
host = 126.96.36.199
; european gateways - not currently used but added for future use
host = 188.8.131.52
host = 184.108.40.206
host = 5220.127.116.11
host = 18.104.22.168
; asian gateways - not currently used but added for future use
host = 22.214.171.124
host = 126.96.36.199
host = 188.8.131.52
host = 184.108.40.206
host = 220.127.116.11
host = 18.104.22.168
host = 22.214.171.124
host = 126.96.36.199
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.
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"?>
<Sip username="twilio" password="xxxxxxx">sip:email@example.com</Sip>
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.