Question

I have an app which lets the users dial number(s) they want to add to a call. Each user is subjected to Balance they have in their account.

The dial is performed by using TwiML <Dial>

So as per my amount per minute rate i calculate the remaining balance in terms of seconds and set that as a timeLimit for <Dial>.

I want to do a simple thing like when the user is in a call and his call timeLimit is about to expire, I would want to charge them using my payment methods and If the charge was a success replenish the timeLimit for the same call.

Can this be done?

Was it helpful?

Solution

Twilio Developer Evangelist here.

There isn't a way to modify the timeLimit on a dial while the call is in progress. But I think I have a solution that could work for you.

Instead of dialing the number directly you could call into a conference with a timeLimit.

<Response>
  <Dial timeLimit="30">
    <Conference>YourCall</Conference>
  </Dial>
</Response>

Then when their account is replenished you could modify the live call to redirect to a TwiML url that rejoins the conference call with the new timeLimit:

<?php
// Get the PHP helper library from twilio.com/docs/php/install
require_once('/path/to/twilio-php/Services/Twilio.php'); // Loads the library

// Your Account Sid and Auth Token from twilio.com/user/account
$sid = "{{ sid }}"; 
$token = "{{ auth_token }}"; 
$client = new Services_Twilio($sid, $token);

// Get an object from its sid. If you do not have a sid,
// check out the list resource examples on this page
$call = $client->account->calls->get("{{call sid}}");
$call->update(array(
        "Url" => "http://youserver.com/conference.xml",
    "Method" => "POST"
    ));
echo $call->to;

OTHER TIPS

Probably easier is to use the https://www.twilio.com/docs/api/rest/change-call-state function of the Twilio REST API.The REST API is asynchronous.

In your situation you can do it as follows:

  1. Dial timeLimit=[Max] (for an unlimited time, 4 hours is the max)
  2. After a while, try to recharge the account.
  3. On recharge success: Do nothing, the call persists.
  4. On recharge failure: Disconnect by executing the change-call-state function of the Twilio REST API. You could even play an audio file or do other stuff before disconnect. For example ask the caller to verify its account because recharge failed or something.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top