1.1. Connect an Asterisk server to Twilio’s SIP service
To connect an Asterisk server to Twilio’s SIP service, you have to configure a ‘SIP Trunk’ in your Twilio account.
First log in to your twilio.com account, then, on the left sidebar click on ‘Explore Products’. On the ‘Explore Products’ page scroll down and click on ‘Elastic SIP Trunking’. Next, on the left sidebar, under ‘Elastic SIP Trunking’, click on ‘Manage’ > ‘Trunks’. On the ‘Elastic SIP Trunks’ page, click on the ‘Create new SIP Trunk’ button. On the ‘Create a New SIP Trunk’ pop-up window, enter a name for the new SIP trunk. Next, on the ‘General Settings’ page of the new trunk, scroll down to ‘Symmetric RTP’ and disable it by clicking on the ‘Enabled’ switch. Leave all the other settings as they are and click on the ‘Save’ button at the bottom of the page. On the left sidebar click on ‘Termination’. On the ‘Termination’ page, in the ‘Termination SIP URI’ field, enter your personal subdomain, which will be prepended to ‘.pstn.twilio.com’. For example, if your server’s hostname is ‘mail.example.com’, you can enter ‘example’. The ‘example.pstn.twilio.com’ termination SIP URI has to be unique. If another identical URI has already been registered, you will see the ‘Not Available’ message next to the URI field. Then, on the same page, in the ‘Authentication’ section, under ‘Credential Lists’ click on the ‘+’ sign to add a new set of credentials. On the ‘New Credential List’ pop-up window, in the ‘Friendly Name’ field, enter a generic name for the credentials, like ‘asterisk-credentials’, in the ‘Username’ field enter a username, like ‘twiliousername’, and in the ‘Password’ field enter a strong password, then click ‘Create’. Leave all the other settings as they are and click on the ‘Save’ button at the bottom of the page.
On the left sidebar, click on ‘Origination’, then, under ‘Origination URIs’, click on the ‘+’ sign to add a new origination URI. On the ‘Add Origination URL’ pop-up window, in the ‘Origination SIP URI’ field, enter sip:123.123.123.123:5827, where 123.123.123.123 is the IPv4 address of your server on which Asterisk is running and 5827 is the custom Asterisk port. Leave the other fields as they are and click ‘Save’. Then scroll down to the bottom of the page and click the ‘Save’ button.
Finally, on the left sidebar, click on ‘Numbers’ and on the ‘Numbers’ page click ‘Add a number’ in the upper right corner. From the drop-down list, choose ‘Add an Existing Number’. On the ‘Add a number’ pop-up window select the phone number acquired from Twilio that you want to associate with this connection and click ‘Add Selected’. You can add multiple phone numbers in the same manner. All inbound calls on the number(s) associated with the configured trunk will be routed by Twilio to your Asterisk server.
Next, open the /etc/asterisk/pjsip.conf file for editing:
nano /etc/asterisk/pjsip.conf
Add the following blocks at the bottom of this file (add the transport-udp and transport-wss blocks only if you haven’t added them already):
[transport-udp]
type=transport
protocol=udp
bind=0.0.0.0:5827
[transport-wss]
type=transport
protocol=wss
bind=0.0.0.0:5827
; Twilio registration
[twilio1]
type=registration
transport=transport-udp
outbound_auth=twilio1
;server_uri=sip:example.pstn.twilio.com
client_uri=sip:15252525252@example.pstn.twilio.com
auth_rejection_permanent=no
[twilio1]
type=auth
auth_type=userpass
username=twiliousername
password=Kj5Rd4Sqc9V7Qd6We4
[twilio1]
type=aor
max_contacts=250
contact=sip:15252525252@example.pstn.twilio.com
[Tw+15252525252]
type=endpoint
transport=transport-udp
context=context-in
disallow=all
allow=ulaw
allow=alaw
direct_media=no
from_user=15252525252
from_domain=example.pstn.twilio.com
outbound_auth=twilio1
aors=twilio1
[twilio1]
type=identify
endpoint=Tw+15252525252
match=54.244.51.0
match=54.244.51.1
match=54.244.51.2
match=54.244.51.3
match=54.244.51.0/24
match=54.172.60.0
match=54.172.60.1
match=54.172.60.2
match=54.172.60.3
match=54.172.60.0/23
match=34.203.250.0/23
; Regular extension
[601]
type=aor
max_contacts=250
[auth601]
type=auth
auth_type=userpass
username=601
password=Y24bE9vtt4wL2szK9q
[601]
type=endpoint
context=context-out
message_context=textmessages
aors=601
auth=auth601
transport=transport-wss
webrtc=yes
disallow=all
allow=ulaw
allow=alaw
allow=vp8
allow=h264
max_audio_streams=10000000
max_video_streams=10000000
dtls_cert_file=/etc/asterisk/keys/asterisk.pem
dtls_private_key=/etc/asterisk/keys/asterisk.key
[601]
type=identify
endpoint=601
match=123.123.123.123
Replace 5827 with your custom Asterisk port, replace example.pstn.twilio.com with the ‘Termination SIP URI’ that you have set up earlier in your ‘twilio.com’ account, replace 15252525252 with your Twilio phone number that you want to associate with your outbound calls (the phone number must include the country calling code, here 1), replace twiliousername with the username that you have set up earlier for the configured SIP trunk on the ‘Termination’ page, replace Kj5Rd4Sqc9V7Qd6We4 with the password for the twiliousername username, replace 601 with the internal extension that you want to use to receive incoming calls, replace Y24bE9vtt4wL2szK9q with the password for extension 601 and replace 123.123.123.123 with the public IPv4 address of your server.
Please note that if your physical server is located in a geographical area different from North America, you can change the termination SIP URI from example.pstn.twilio.com to a ‘localized’ URI, like example.pstn.dublin.twilio.com, or example.pstn.frankfurt.twilio.com, etc., to reduce latency. You can see the list of localized URIs in your configured trunk settings, on the ‘Termination’ page, under ‘Show Localized URIs’. If you choose to use a localized termination SIP URI, you will have to also change the list of IPs in the ‘identify’ block shown above. The list of IPs preceded by ‘match=’ in the ‘identify’ block, will depend on the chosen geographical location. The list of IPs belonging to each available geographical location can be found here.
Next, open the /etc/asterisk/extensions.conf file for editing:
nano /etc/asterisk/extensions.conf
Add the following lines at the bottom of this file:
[context-out]
autohints = yes
; If you dial a number that begins with a plus sign, remove the plus sign
exten => _+X.,1,Goto(${EXTEN:1}, 1)
; If you dial a number that is 4 digits long or less, dial an Asterisk extension, otherwise dial an outside number using Localphone/Telnyx/Flowroute/Twilio/Vonage
exten => _X.,1,GotoIf($[${LEN($EXTEN)} < 5]?lessthanfive:morethanfour)
exten => _X.,2(lessthanfive),Goto(${EXTEN},1)
exten => _X.,3(morethanfour),Set(sipProv=${PJSIP_HEADER(read,X-SipProvider)})
exten => _X.,4,Set(callFromNb=${PJSIP_HEADER(read,X-CallFromNumber)})
exten => _X.,5,GotoIf($[${sipProv}==Ln]?localphone:secondcheck)
exten => _X.,6(secondcheck),GotoIf($[${sipProv}==Tx]?telnyx:thirdcheck)
exten => _X.,7(thirdcheck),GotoIf($[${sipProv}==Fl]?flowroute:fourthcheck)
exten => _X.,8(fourthcheck),GotoIf($[${sipProv}==Tw]?twilio:fifthcheck)
exten => _X.,9(fifthcheck),GotoIf($[${sipProv}==Vn]?vonage:endcall)
exten => _X.,10(localphone),Dial(PJSIP/${EXTEN}@Ln${callFromNb},60,tr)
exten => _X.,11,Answer()
exten => _X.,12,Hangup()
exten => _X.,13(telnyx),Dial(PJSIP/${EXTEN}@Tx${callFromNb},60,tr)
exten => _X.,14,Answer()
exten => _X.,15,Hangup()
exten => _X.,16(flowroute),Dial(PJSIP/${EXTEN}@Fl${callFromNb},60,tr)
exten => _X.,17,Answer()
exten => _X.,18,Hangup()
exten => _X.,19(twilio),Dial(PJSIP/${EXTEN}@Tw${callFromNb},60,tr)
exten => _X.,20,Answer()
exten => _X.,21,Hangup()
exten => _X.,22(vonage),Dial(PJSIP/${EXTEN}@Vn${callFromNb},60,tr)
exten => _X.,23,Answer()
exten => _X.,24(endcall),Hangup()
exten => 601,1,Dial(PJSIP/601,60)
exten => 601,2,Answer()
exten => 601,3,Hangup()
[context-in]
autohints = yes
;Twilio number
exten => +15252525252,1,Goto(context-out,601,1)
[textmessages]
autohints = yes
exten => 601,1,MessageSend(pjsip:${EXTEN},${MESSAGE(from)})
same => n,Hangup()
Again, replace 601 with the internal extension that you want to use to receive incoming calls, and replace 15252525252 with your Twilio phone number that you will use to receive phone calls. Please note that the phone number in the [context-in] context has to be preceded by the ‘+’ sign. If you have multiple phone numbers, you can configure them similarly:
;Twilio number
exten => +15252525252,1,Goto(context-out,601,1)
exten => +15353535353,1,Goto(context-out,601,1)
exten => +15454545454,1,Goto(context-out,602,1)
Restart Asterisk:
systemctl restart asterisk
Then, if you run asterisk -rv followed by pjsip show endpoints, you will see something similar to the following:
Endpoint: Tw+15252525252 Not in use 0 of inf
OutAuth: twilio1/twiliousername
Aor: twilio1 250
Contact: twilio1/sip:15252525252@example.pstn.twilio.com ebc4c6e2a1 NonQual nan
Transport: transport-udp udp 0 0 0.0.0.0:5827
Identify: twilio1/Tw+15252525252
Match: 54.244.51.0/32
Match: 54.244.51.1/32
Match: 54.244.51.2/32
Match: 54.244.51.3/32
Match: 54.244.51.0/24
Match: 54.172.60.0/32
Match: 54.172.60.1/32
Match: 54.172.60.2/32
Match: 54.172.60.3/32
Match: 54.172.60.0/23
Match: 34.203.250.0/23
This means that the Asterisk server is registered with Twilio’s SIP service and that you can make and receive phone calls using your Twilio account and phone number(s). All the other Asterisk settings (related to DTLS certificates, the /etc/asterisk/http.conf file, the /etc/asterisk/rtp.conf file, etc.) are the same as for all the other providers (Telnyx, Localphone, etc.) and are detailed in the Install Asterisk chapter.
1.2. Connect an Asterisk server to Flowroute’s SIP service
To connect an Asterisk server to Flowroute’s SIP service, first open the /etc/asterisk/pjsip.conf file for editing:
nano /etc/asterisk/pjsip.conf
Add the following blocks at the bottom of this file (add the transport-udp and transport-wss blocks only if you haven’t added them already):
[transport-udp]
type=transport
protocol=udp
bind=0.0.0.0:5827
[transport-wss]
type=transport
protocol=wss
bind=0.0.0.0:5827
; Flowroute registration
[flowroute1]
type=registration
transport=transport-udp
outbound_auth=flowroute1
server_uri=sip:us-east-va.sip.flowroute.com:5060
client_uri=sip:65972394@us-east-va.sip.flowroute.com:5060
auth_rejection_permanent=no
contact_user=65972394
[flowroute1]
type=auth
auth_type=userpass
username=65972394
password=R52Qc4n9J5s6b
[flowroute1]
type=aor
max_contacts=250
contact=sip:us-east-va.sip.flowroute.com
[Fl+16262626262]
type=endpoint
transport=transport-udp
context=context-in
disallow=all
allow=ulaw
allow=alaw
direct_media=no
from_user=16262626262
from_domain=us-east-va.sip.flowroute.com
outbound_auth=flowroute1
aors=flowroute1
[flowroute1]
type=identify
endpoint=Fl+16262626262
match=us-east-va.sip.flowroute.com
; Regular extension
[601]
type=aor
max_contacts=250
[auth601]
type=auth
auth_type=userpass
username=601
password=Y24bE9vtt4wL2szK9q
[601]
type=endpoint
context=context-out
message_context=textmessages
aors=601
auth=auth601
transport=transport-wss
webrtc=yes
disallow=all
allow=ulaw
allow=alaw
allow=vp8
allow=h264
max_audio_streams=10000000
max_video_streams=10000000
dtls_cert_file=/etc/asterisk/keys/asterisk.pem
dtls_private_key=/etc/asterisk/keys/asterisk.key
[601]
type=identify
endpoint=601
match=123.123.123.123
Replace 5827 with your custom Asterisk port, replace 65972394 with your ‘Username & Auth Username’, and R52Qc4n9J5s6b with your SIP password (once you log in to your Flowroute account, on the left sidebar click on ‘Interconnection’, then click on the ‘Registration’ tab; under ‘SIP Credentials’ you will find your ‘Username & Auth Username’ and your SIP password), replace 16262626262 with your Flowroute phone number that you want to associate with your outbound calls (the phone number must include the country calling code, here 1), replace 601 with the internal extension that you want to use to receive incoming calls, replace Y24bE9vtt4wL2szK9q with the password for extension 601 and replace 123.123.123.123 with the public IPv4 address of your server.
Please note that if your physical server is located in a geographical area different from the east coast of the United States, you can change the subdomain us-east-va.sip.flowroute.com with that of the Flowroute Point of Presence that is nearer to the physical location of your server, to reduce latency. The subdomains for all the available Points of Presence are listed on the same ‘Registration’ tab, under ‘Point of Presence Options’.
Next, open the /etc/asterisk/extensions.conf file for editing:
nano /etc/asterisk/extensions.conf
Add the following lines at the bottom of this file:
[context-out]
autohints = yes
; If you dial a number that begins with a plus sign, remove the plus sign
exten => _+X.,1,Goto(${EXTEN:1}, 1)
; If you dial a number that is 4 digits long or less, dial an Asterisk extension, otherwise dial an outside number using Localphone/Telnyx/Flowroute/Twilio/Vonage
exten => _X.,1,GotoIf($[${LEN($EXTEN)} < 5]?lessthanfive:morethanfour)
exten => _X.,2(lessthanfive),Goto(${EXTEN},1)
exten => _X.,3(morethanfour),Set(sipProv=${PJSIP_HEADER(read,X-SipProvider)})
exten => _X.,4,Set(callFromNb=${PJSIP_HEADER(read,X-CallFromNumber)})
exten => _X.,5,GotoIf($[${sipProv}==Ln]?localphone:secondcheck)
exten => _X.,6(secondcheck),GotoIf($[${sipProv}==Tx]?telnyx:thirdcheck)
exten => _X.,7(thirdcheck),GotoIf($[${sipProv}==Fl]?flowroute:fourthcheck)
exten => _X.,8(fourthcheck),GotoIf($[${sipProv}==Tw]?twilio:fifthcheck)
exten => _X.,9(fifthcheck),GotoIf($[${sipProv}==Vn]?vonage:endcall)
exten => _X.,10(localphone),Dial(PJSIP/${EXTEN}@Ln${callFromNb},60,tr)
exten => _X.,11,Answer()
exten => _X.,12,Hangup()
exten => _X.,13(telnyx),Dial(PJSIP/${EXTEN}@Tx${callFromNb},60,tr)
exten => _X.,14,Answer()
exten => _X.,15,Hangup()
exten => _X.,16(flowroute),Dial(PJSIP/${EXTEN}@Fl${callFromNb},60,tr)
exten => _X.,17,Answer()
exten => _X.,18,Hangup()
exten => _X.,19(twilio),Dial(PJSIP/${EXTEN}@Tw${callFromNb},60,tr)
exten => _X.,20,Answer()
exten => _X.,21,Hangup()
exten => _X.,22(vonage),Dial(PJSIP/${EXTEN}@Vn${callFromNb},60,tr)
exten => _X.,23,Answer()
exten => _X.,24(endcall),Hangup()
exten => 601,1,Dial(PJSIP/601,60)
exten => 601,2,Answer()
exten => 601,3,Hangup()
[context-in]
autohints = yes
; Flowroute number
exten => 16262626262,1,Goto(context-out,601,1)
[textmessages]
autohints = yes
exten => 601,1,MessageSend(pjsip:${EXTEN},${MESSAGE(from)})
same => n,Hangup()
Again, replace 601 with the internal extension that you want to use to receive incoming calls, and replace 16262626262 with your Flowroute phone number that you will use to receive phone calls. If you have multiple phone numbers, you can configure them similarly:
; Flowroute number
exten => 16262626262,1,Goto(context-out,601,1)
exten => 16363636363,1,Goto(context-out,601,1)
exten => 16464646464,1,Goto(context-out,602,1)
Restart Asterisk:
systemctl restart asterisk
Then, if you run asterisk -rv followed by pjsip show endpoints, you will see something similar to the following:
Endpoint: Fl+16262626262 Not in use 0 of inf
OutAuth: flowroute1/65972394
Aor: flowroute1 250
Contact: flowroute1/sip:65972394@us-east-va.sip.flowroute.com 524ac6e2c5 NonQual nan
Transport: transport-udp udp 0 0 0.0.0.0:5827
Identify: flowroute1/Fl+16262626262
Match: 34.226.36.34/32
Match: 34.226.36.32/32
Match: 34.226.36.33/32
Match: 34.226.36.35/32
Match: 34.210.91.112/32
Match: 34.210.91.114/32
This means that the Asterisk server is registered with Flowroute’s SIP service and that you can make and receive phone calls using your Flowroute account and phone number(s). All the other Asterisk settings (related to DTLS certificates, the /etc/asterisk/http.conf file, the /etc/asterisk/rtp.conf file, etc.) are the same as for all the other providers (Telnyx, Localphone, etc.) and are detailed in the Install Asterisk chapter.
1.3. Connect an Asterisk server to Vonage’s SIP service
To connect an Asterisk server to Vonage’s SIP service, first open the /etc/asterisk/pjsip.conf file for editing:
nano /etc/asterisk/pjsip.conf
Add the following blocks at the bottom of this file (add the transport-udp and transport-wss blocks only if you haven’t added them already):
[transport-udp]
type=transport
protocol=udp
bind=0.0.0.0:5827
[transport-wss]
type=transport
protocol=wss
bind=0.0.0.0:5827
; Vonage registration
[vonage-reg1]
type=registration
outbound_auth=vonage-auth1
server_uri=sip:sip.nexmo.com
client_uri=sip:ebc94562@sip.nexmo.com
[vonage-auth1]
type=auth
auth_type=userpass
username=ebc94562
password=DG5eqcpzwK6W4C4Q5
[vonage1]
type=aor
contact=sip:sip.nexmo.com
qualify_frequency=120
[Vn+18282828282]
type=endpoint
aors=vonage1
outbound_auth=vonage-auth1
context=context-in
transport=transport-udp
from_user=18282828282
allow=alaw
allow=ulaw
allow=g729
[vonage-identify]
type=identify
endpoint=Vn+18282828282
match=5.10.112.122
match=5.10.112.121
match=173.193.199.24
match=174.37.245.34
match=119.81.44.6
match=119.81.44.7
match=169.60.141.29
match=169.60.141.30
; Regular extension
[601]
type=aor
max_contacts=250
[auth601]
type=auth
auth_type=userpass
username=601
password=Y24bE9vtt4wL2szK9q
[601]
type=endpoint
context=context-out
message_context=textmessages
aors=601
auth=auth601
transport=transport-wss
webrtc=yes
disallow=all
allow=ulaw
allow=alaw
allow=vp8
allow=h264
max_audio_streams=10000000
max_video_streams=10000000
dtls_cert_file=/etc/asterisk/keys/asterisk.pem
dtls_private_key=/etc/asterisk/keys/asterisk.key
[601]
type=identify
endpoint=601
match=123.123.123.123
Replace 5827 with your custom Asterisk port, replace ebc94562 with your Vonage ‘API key’ and DG5eqcpzwK6W4C4Q5 with your Vonage ‘API secret’ (once you log in to your Vonage account, on the left sidebar click on your username to expand the menu, then click on ‘Settings’, then, on the ‘API keys’ page you will find the ‘API key’ and the ‘API secret’), replace 18282828282 with your Vonage phone number that you want to associate with your outbound calls (the phone number must include the country calling code, here 1), replace 601 with the internal extension that you want to use to receive incoming calls, replace Y24bE9vtt4wL2szK9q with the password for extension 601 and replace 123.123.123.123 with the public IPv4 address of your server.
Next, open the /etc/asterisk/extensions.conf file for editing:
nano /etc/asterisk/extensions.conf
Add the following lines at the bottom of this file:
[context-out]
autohints = yes
; If you dial a number that begins with a plus sign, remove the plus sign
exten => _+X.,1,Goto(${EXTEN:1}, 1)
; If you dial a number that is 4 digits long or less, dial an Asterisk extension, otherwise dial an outside number using Localphone/Telnyx/Flowroute/Twilio/Vonage
exten => _X.,1,GotoIf($[${LEN($EXTEN)} < 5]?lessthanfive:morethanfour)
exten => _X.,2(lessthanfive),Goto(${EXTEN},1)
exten => _X.,3(morethanfour),Set(sipProv=${PJSIP_HEADER(read,X-SipProvider)})
exten => _X.,4,Set(callFromNb=${PJSIP_HEADER(read,X-CallFromNumber)})
exten => _X.,5,GotoIf($[${sipProv}==Ln]?localphone:secondcheck)
exten => _X.,6(secondcheck),GotoIf($[${sipProv}==Tx]?telnyx:thirdcheck)
exten => _X.,7(thirdcheck),GotoIf($[${sipProv}==Fl]?flowroute:fourthcheck)
exten => _X.,8(fourthcheck),GotoIf($[${sipProv}==Tw]?twilio:fifthcheck)
exten => _X.,9(fifthcheck),GotoIf($[${sipProv}==Vn]?vonage:endcall)
exten => _X.,10(localphone),Dial(PJSIP/${EXTEN}@Ln${callFromNb},60,tr)
exten => _X.,11,Answer()
exten => _X.,12,Hangup()
exten => _X.,13(telnyx),Dial(PJSIP/${EXTEN}@Tx${callFromNb},60,tr)
exten => _X.,14,Answer()
exten => _X.,15,Hangup()
exten => _X.,16(flowroute),Dial(PJSIP/${EXTEN}@Fl${callFromNb},60,tr)
exten => _X.,17,Answer()
exten => _X.,18,Hangup()
exten => _X.,19(twilio),Dial(PJSIP/${EXTEN}@Tw${callFromNb},60,tr)
exten => _X.,20,Answer()
exten => _X.,21,Hangup()
exten => _X.,22(vonage),Dial(PJSIP/${EXTEN}@Vn${callFromNb},60,tr)
exten => _X.,23,Answer()
exten => _X.,24(endcall),Hangup()
exten => 601,1,Dial(PJSIP/601,60)
exten => 601,2,Answer()
exten => 601,3,Hangup()
[context-in]
autohints = yes
; Vonage number
exten => 18282828282,1,Goto(context-out,601,1)
[textmessages]
autohints = yes
exten => 601,1,MessageSend(pjsip:${EXTEN},${MESSAGE(from)})
same => n,Hangup()
Again, replace 601 with the internal extension that you want to use to receive incoming calls, and replace 18282828282 with your Vonage phone number that you will use to receive phone calls. If you have multiple phone numbers, you can configure them similarly:
; Vonage number
exten => 18282828282,1,Goto(context-out,601,1)
exten => 18383838383,1,Goto(context-out,601,1)
exten => 18484848484,1,Goto(context-out,602,1)
Restart Asterisk:
systemctl restart asterisk
Then, if you run asterisk -rv followed by pjsip show endpoints, you will see something similar to the following:
Endpoint: Vn+18282828282 Not in use 0 of inf
OutAuth: vonage-auth1/ebc94562
Aor: vonage1 250
Contact: vonage1/sip:ebc94562@sip.nexmo.com c6427fde6a NonQual nan
Transport: transport-udp udp 0 0 0.0.0.0:5827
Identify: vonage-identify1/Vn+18282828282
Match: 5.10.112.122
Match: 5.10.112.121
Match: 173.193.199.24
Match: 174.37.245.34
Match: 119.81.44.6
Match: 119.81.44.7
Match: 169.60.141.29
Match: 169.60.141.30
This means that the Asterisk server is registered with Vonage’s SIP service and that you can make and receive phone calls using your Vonage account and phone number(s). All the other Asterisk settings (related to DTLS certificates, the /etc/asterisk/http.conf file, the /etc/asterisk/rtp.conf file, etc.) are the same as for all the other providers (Telnyx, Localphone, etc.) and are detailed in the Install Asterisk chapter.