Code

Stuff I Built: Simple International Calling Card with Twilio

Reposted from the Twilio Company Blog

When I came downstairs this morning I was greeted by two bubbly and very sleep deprived Australians eager for some tea, and a chance to call Mum.  My first thought – there’s a Twilio app for that (or there will be soon)!

Being in the Christmas sprint, I decided I’d quickly code up an application that would make it easy for them to call a U.S. number from the landline at our house or any local phone, and be forwarded to their mom’s, boyfriends, and other folks through a simple menu.  20 minutes later, we made our first call!

Setting Up the International “Calling Card”

Twilio doesn’t provide international phone numbers, but you can set up a U.S. number and have it forward to an international destination using the <Dial> verb.  You don’t even need to use the REST API to make the outbound calls, its so simple!

Files to create:

  • * Handler for the incoming call, to greet the caller and read the menu, gather the menu selection keypress
  • * PHP handler for taking the keypress and directing the application to the right file to dial the number
  • * Files for each of the phone menu options, going to the different numbers to call

Setting up incoming-call.php

This first file is the one that I pointed the Twilio phone number to, to handle incoming calls.  It greets the caller and reads them a menu of people to call, and asks them to press a number to start.

It looks something like this:

<?php
 header("content-type: text/xml");
 echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
?>

<Response>

<Gather numDigits="1" action="make-call.php" method="POST">

 <Say voice="woman">Hey girls, ready to call someone? If you know your s\
election, you may make it at any time.</Say>
 <Say>Press 1 to Call Laurens Mom</Say>
 <Say>Press 2 to Call Jace</Say>
 <Say>Press 3 to Call Eleesa's Home</Say>
 <Say>Press 4 to Call Duh lane ah's Cell Phone</Say>
 <Say>To get help, Press 5 to Call Danielle</Say>

</Gather>

<Say voice="woman">Thanks for using this Twil ee oh app, created by Danielle. \
Happy holidays!</Say>

</Response>

Setting up make-call.php

After the caller has pressed as key, the application posts the results to make-call.php, so we need to create a php file that understands what to do next with that information, and route the call.

<?php 
 
        if($_REQUEST['Digits'] == '1') { 
                header("Location: call-laurens-mom.php");
                die;
        }

        if($_REQUEST['Digits'] == '2') {
                header("Location: call-jace.php");
                die;
        }

        if($_REQUEST['Digits'] == '3') {
                header("Location: call-elisas-home.php");
                die;
        }

        if($_REQUEST['Digits'] == '4') {
                header("Location: call-dlaina-cell.php");
                die;
        }

        if($_REQUEST['Digits'] == '5') {
                header("Location: call-danielle.php");
                die;
        }


        header("content-type: text/xml");
        echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";

?>

Setting up TwiML to Connect the Call

As you can see in the previous php script make-call.php, each selection directed the application to a different file.  This file is a very simple piece of TwiML that uses the <Dial> verb to connect the call.  Each one is pretty much the same, and looks like this:

<?php
        header("content-type: text/xml");
        echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
?>

<Response>

<Say>Connecting you to Danielle, for help with this application..</Say>

<Dial>4256987497</Dial>

</Response>

It’s Not Pre-paid, It’s Pay-As-You-Go

The best part about this for Elisa and Lauren is that it isn’t a prepaid card where they spend $50 and and are stuck with the card, even if they don’t use it up.  I’m billing them for exactly the amount they use, and they don’t have to pay for it until after the fact.  I can imagine turning custom pay-as-you-go calling cards into a really interesting business.

So there you have it.  If you have any international guests in your home this holiday season, or are interested in going into the calling card business, this might be a good place to start.  The app took less than 20 minutes to write, mostly because we were goofing around with the text to speech quite a bit, and is written with PHP.

You do need an upgraded Twilio account to get a phone number and make international calls, so maybe some Twilio minutes would be a good thing to ask Santa to bring you.  Happy holidays!

2 Comments

Leave a Reply

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