Making Phone Calls Using Twilio SIP

Making Phone Calls Using Twilio SIP
August 03, 2017
Written by

The following is a guest post from Nethram.

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.

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 Your 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.

Make sure to have your credentials on hand as well.

Step 2: Create Your 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.

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

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 Endpoints

Create new account in Zoiper and enter your 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.
For example:  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.

Making Inbound and Outbound Calls with Your 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.
 


 
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”.

Outbound Calling

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.

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.