Our Blog

Make Twilio calls using SIP phones – Utilize Twilio Sip

Prajina MP
May 25, 2017

 

” Make inbound and outbound calls with your sip phone (Hard phone or soft phone) without any additional PBX set up or cloud phone systems. “

 

                              Traditionally, connecting a SIP phone and making calls were cumbersome. It required a dedicated PBX, or expensive cloud telephone. With Twilio Voice supporting SIP registration, it is now easy to utilize the Twilio cloud to power your SIP enabled phone. You can use either SIP hard phones such a Cisco, Yealink and Snom or soft phones such as zoiper, x-lite or sj-phone to connect to Twilio and make calls.

                             In this Blog, we will demonstrate how to register multiple extensions with Twilio, and  to make calls between the extensions and also external calls. We have also provided easy to use scripts that you may use from our server, or modify to meet your specific needs.
Twilio allows to connect SIP-enabled devices or soft phones directly to Twilio and to use the Voice API to handle calls. The following diagram shows how sip endpoints can make calls within and to out side of the network. Sip endpoints can call each other and can make call to out side.Also can receive call external to the system.

 

sipdiag

Twilio sip endpoint registration

 

We are going to register twilio sip end point in android phone and make inbound and outbound calls with twilio sip end points. We are using  Zoiper soft phone, but the same procedure applies for other soft or hard VOIP phones.To register and test Twilio domain endpoints you’ll need a twilio account with an incoming phone number and Zoiper soft phone installed on your phone.

Create twilio sip domain

 

Step 1: Create credential list
Log in to your twilio account go to programmable voice.Select “credential lists” ,create a credential list ,add credentials and save your passwords (we need this later). In this example, your extensions will have a 3 digit number, but with a little programming tweak you may use either 4 digit extensions or even a 1 digit extension.

twilio_crd

And you have your credentials.

SIPCredlist

 

Step 2: Create twilio sip domain.

Select “Domains” and click the “+” icon.Enter a friendly name and a unique sip uri. We also need to change the request url but we are going to do that later.

twilio_sip1

 

Add your credential lists , enable the allow to register end points, add credential lists and save your domain.

 

twilio_sip22

 

Now we all set to register sip endpoints in android phones.Remember the credentials we added in our credential list.Those are the user name and password pair we are going to use here.

Register twilio sip end points

 

SIPZ

 

Create new account in zoiper , enter user name and password from your credential.Enter your twilio domain name in the Host field.Don’t forget to add region in sip domain name.
Eg : siptwilionethram.sip.us1.twilio.com
Then save and register your twilio end point.You will see a green dot on successful end point registration .Follow the same procedure and register another end point. That extension can be named as 102. You can see the successfully registered endpoints under your sip domain in the twilio console.

siplist

 

Making Inbound and outbound calls with endpoint

 

Inbound call
First we make a call to our twilio endpoint by configuring the voice url of a twilio number. You may have to purchase a new number. Create a php script CallToSip with following code or you can use the php script we have hosted at “http://nethram.com/sandbox/Twiliosip/CallToSip.php”
for testing.

 

<?php
echo header('content-type: text/xml');
echo '<?xml version="1.0" encoding="UTF-8"?>';
/** Get sip endpoint list **/
$params=$_REQUEST['SipUser'];
$toNumberList=explode(",",$params);
?>
<Response>
	<?php
	for($i=0; $i < sizeof($toNumberList) ;$i++) { $to=$toNumberList[$i]; ?>
		<Dial timeout="20">

			<Sip>
				<?php echo $to; ?>
			</Sip>

		</Dial>
		<?php } ?>
</Response>

 

To dial a sip endpoint configure the twilio incoming phone number’s voice url with
“http://nethram.com/sandbox/Twiliosip/CallToSip.php?SipUser=100@siptwilionethram.sip.us1.twilio.com”.
Or with script hosted in your server. Replace sip endpoint with yours.
twilio_sip4
Dial twilio number from any of your voice network and call will be forwarded to your twilio endpoint registered in android phone.
You can also call to multiple sip phones(sip endpoints) by adding coma separated sip end points to voice url as “http://nethram.com/sandbox/callbackweb/CallToSip.php?SipUser=100@siptwilionethram.sip.us1.twilio.com,101@siptwilionethram.sip.us1.twilio.com”.

 

Out bound call
Next we are going to make call from our twilio end point.Create a new file calltwiliosip.php and copy the following code or you can use script hosted in our sever
“http://www.nethram.com/sandbox/callbackweb/calltwiliosip.php”.


<?php 
echo header('content-type: text/xml');
echo '<?xml version="1.0" encoding="UTF-8"?>';
$to=$_REQUEST['To'];
$callerId=$_REQUEST["callerId"];
/** Extracting user name **/
$pos1 = strpos($to,":");
$pos2 = strpos($to,"@");
$tosip=substr($to,$pos1+1,$pos2-$pos1-1);
if(strlen($tosip) == 3)
{
/**Extracting sip endpoint**/
    $pos2 = strpos($to,":",strpos($to,":")+1);
    $tosip=substr($to,$pos1+1,$pos2-$pos1-1); 

    ?>
    <Response>
        <Dial>
            <Sip>
                <?php echo $tosip; ?>
            </Sip>
        </Dial>
    </Response>
    <?php } else { if(substr($tosip,0,2)=="00") $tosip=substr($tosip,2,strlen($tosip)-1); if(substr($tosip,0,3)=="011") $tosip=substr($tosip,3,strlen($tosip)-1); ?>
    <Response>
        <Dial callerId="<?php echo $callerId; ?>" >
            <?php echo $tosip; ?>
        </Dial>
    </Response>
    <?php } ?>

 

Configure your sip domain’s voice url with
“http://www.nethram.com/sandbox/callbackweb/calltwiliosip.php?=callerId=+12246048844” or the script hosted in your server , replace callerId with any of your twilio incoming phone number, or a verified Twilo number.

 

SIPVoice

If the user dials a 3 digit number then the script assumes that it will be a sip extension and script will forward the call to the corresponding sip extension. In all other cases, the script assumes that it is a phone number and call will be forwarded to the corresponding number. International numbers should have a 00 or 011 prefix, and the this prefix will be substituted with “+”.
To test first case dial 100 (registered end point username) from zoiper soft phone.Dial a 10 digit phone number to test the second case.If you dial an international phone number be sure that global permissions for the country is enabled in your Twilio account.

3 Responses to “Make Twilio calls using SIP phones – Utilize Twilio Sip”

Trackbacks/Pingbacks

  1.  Making Phone Calls Using Twilio SIP

Leave a Reply

Your email address will not be published. Required fields are marked *