Thev
Thev

Reputation: 59

Play sound during Twilio outbound phone call

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:

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!

Upvotes: 1

Views: 2096

Answers (1)

philnash
philnash

Reputation: 73027

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.

Upvotes: 1

Related Questions