Creating a Custom International Calling Card with Twilio

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:

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.

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:

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!