1
votes

I am trying to play a sound through a conversation with Twilio. I am new to Twilio and I am running into some problems.

The call is initiated from a mobile application using the Twilio Client SDK plugin. In the Twilio console I have created a TwiML app, and have set the Request URL.

This is my TwiML code.

What I want:

  • The user initiates the call from the app.
  • The user gets a time limit (set in the TwiML on my server).
  • The call is connected with a number on the PSTN.
  • When the time limit is almost reached, a sound must be played to notify the user that the call will be ended soon. (Example: Time limit is 1 minute, 30 seconds into the call a sound plays)
  • The call is ended when the limit is reached.

I can already end the call when the time limit is reached but can't figure out how to notify the user.

What I have tried:

I have specified a Status Callback URL in the TwiML app, and I wanted to modify the call state from there, but the Status Callback is only called after the call has finished. This was based on this post.

I have tried to create a Conference based on this post, but I can't get it to work. I think because I am not using the REST API. I am initiating the call from the mobile app using the Client SDK. Should I use the REST API and how would I implement it in combination with the Client?

Server side I use PHP.

If more information is needed, please let me know!

1
Do you need the person who receives the call to hear the sound? If you only need the person using the app to hear it, then you could use the platform to generate the sound rather than in the call.philnash
Yes I have thought about that too, but I would rather handle it server side and integrate the sound into the call if possible.Thev

1 Answers

1
votes

Twilio developer evangelist here.

To handle this server side, you would need to dial into a <Conference> from your client and then generate a call to the number your user was calling using the REST API, directing them into the conference too.

So, your response to the original dial from the client should look a little like this:

require_once '/path/to/vendor/autoload.php';
use Twilio\Rest\Client;

// Your Account Sid and Auth Token from twilio.com/user/account
$sid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
$token = "your_auth_token";
$client = new Client($sid, $token);

$number = htmlspecialchars($_REQUEST["TO"]);

// Generate a call to the other party
$call = $client->calls->create(
    $number, 
    $from,
    array("url" => "http://example.com/conference?conference_name=EXAMPLE")
);

$response = new Twiml;
$limit = getLimit($TWILIO_CALLER_ID, 1);

$dial = $response->dial(array('callerId' => $TWILIO_CALLER_ID, 'timelimit' => $limit));

$dial->conference("EXAMPLE");

header("Content-Type: text/xml");
echo $response;

This will drop the caller into a conference and dial the receiver. When they answer the phone you will get a webhook to the URL (http://example.com/conference?conference_name=EXAMPLE in this case). You need to respond to that URL with the same conference room.

$response = new Twiml;

$dial = $response->dial();
$dial->conference($_REQUEST['conference_name']);

header("Content-Type: text/xml");
echo $response;

Then, when you want to alert the time left, you'll need to dial once more into the conference, this time just using TwiML's <Say> or <Play> to read out the alert.

You will need to set up a number that points to this conference to dial to. Then dial up that number when the time limit comes near and use a URL that says the message.

require_once '/path/to/vendor/autoload.php';
use Twilio\Rest\Client;

// Your Account Sid and Auth Token from twilio.com/user/account
$sid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
$token = "your_auth_token";
$client = new Client($sid, $token);

$number = $YOUR_CONFERENCE_NUMBER;

// Generate a call to the other party
$call = $client->calls->create(
    $number, 
    $TWILIO_CALLER_ID,
    array("url" => "http://example.com/time_message")
);

Finally, you need to respond to the /time_message endpoint with TwiML to say the message and then hang up.

$text1 = "Your limit is";
$text2 = "seconds";

$response = new Twiml;
$response->say($text1 . "10 seconds" . $text2)
$response->hangup();

header("Content-Type: text/xml");
echo $response;

Let me know if that helps at all.