Skip to content


Twilio Calling Card Application

So as I’ve promised here is my simplified tutorial on how to create a Two-Stage-Dialing application with Twilio.

Please note that due to a lack of time that I’ve been now, I haven’t been able to test it, but it should work. Or at least it will probably show you, how Twilio could be used.

First off, lets describe what a calling card application is for those unaware with the terminology. Well a calling card application is a Telephony application that verifies a user by a PIN number (calling card) and, depending on the actual amount of balance the user has left and the destination he wants to call, allows him to perform an outgoing call. Calling cards, are also called ‘two-stage-dialing applications’, ‘debit card applications’, ‘prepaid cards’, ‘toll-free calls’ etc etc.

The actual call flow that such an application should handle is the following:

A simplified call flow for a Toll Free Application

A simplified call flow for a Toll Free Application

In lamens terms, a call arrives on the network. We initially have to verify the user by requesting a PIN number. The PIN is then validated and if the user is allowed he is prompted to enter a destination to dial-out. When the call is over, the duration of the call is being deducted from the PIN.

So, let’s brake down the above call flow in terms of Twilio actions. Please note that I am using the TwilML library I’ve mentioned creating at my previous post here. If there is someone who needs it done using the official Twilio library I will try to help you out :)

Next: Handle incoming calls – Asking for a PIN

Posted in PHP, Telephony.

Tagged with , , , , .


4 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.

  1. Shane Jones says

    Is there a way to terminate the call when the user’s prepaid balance reaches $0?

  2. mobius says

    Well the whole point when building such an application is to work pro-actively. When you actually verify the final destination called (in the example here), you should try and figure out what could be the maximum duration of the call based on the destination and his available funds.

    Most of the time at this point there is a radius server that performs an Authorisation request. The process however is simple.

    Take this for example: I have $5 worth of credit. And I am calling destination +447 which according to my rates costs $0.05/minute. The maximum duration, so that I won’t overcome the $5 credit is 5/0.05 = 100 call minutes. You pass 100*60=6000 seconds as the timeLimit parameter of the Dial Verb and it should terminate the call at exactly 100 minutes.

    Reasonably the next question that comes in mind is what do you do if you have two simultaneous callers using the same PIN. Well there are two solutions into this, one easy and one not so easy.

    Easy: You mark the PIN while being used as being used, and you don’t let anyone else use it.

    Not so easy: You reserve a fund in the PIN beforehand, so that each new PIN usage will find the remaining credit to be ($5 – reservation). You would probably need a b2bua at this point to perform some kind of authorisation requests at specified intervals and terminate the calls when the credit has run out. However this involves Telephony infrastructure that destroy the whole benefit of using Twilio.

    Hope I helped :)

  3. Xodov says

    it was very interesting to read.
    I want to quote your post in my blog. It can?
    And you et an account on Twitter?

  4. mobius says

    Xodov, be my guest! Sorry but I am not using twitter yet :)



Some HTML is OK

or, reply to this post via trackback.