0

Basic case: System will call person A. If person A picks up the phone it will call person B and the 2 will be connected.

I read several answers here such as https://stackoverflow.com/a/20976996/1907888 but it's still unclear.

Would the following work? It would call PERSON_A if person responds it will connect to conference then call PERSON_B and connect to same conference? Do I need to start the conference first?

$response = new VoiceResponse();
$dial = $response->dial('PERSON_A');
if($dial->conference('Room 1234')) {
    $dial = $response->dial('PERSON_B');
    $dial->conference('Room 1234');
}
Robert Sinclair
  • 4,550
  • 2
  • 44
  • 46

1 Answers1

1

Twilio developer evangelist here.

When you control calls with Twilio there are two mechanisms by which it works. There is the Twilio REST API which your application can use to make things happen, like start or change a call. Then there are webhooks, which are HTTP requests that Twilio makes to your application when things change in a call, like someone dialling your Twilio number, entering data on the phone, or an person answering an outbound call. You respond to webhooks with TwiML, a subset of XML, with instructions for what to do with the call next.

In this case, you want to place a call to person A to start with. For this, you will need the REST API to make that call. When person A answers the phone, Twilio will then make a webhook request to your application to find out what to do next. It is at this point that you can both call person B, again using the REST API, and place person A into a conference call, by responding with TwiML.

So, your initial outbound REST API call should look a bit like this:

use Twilio\Rest\Client;

// Find your Account Sid and Auth Token at twilio.com/console
// and set the environment variables. See http://twil.io/secure
$sid = getenv("TWILIO_ACCOUNT_SID");
$token = getenv("TWILIO_AUTH_TOKEN");
$twilio = new Client($sid, $token);

$call = $twilio->calls
               ->create($personANumber, // to
                        $yourTwilioNumber, // from
                        ["url" => "http://example.com/conference.php"]
               );

The URL you send when you place the call will be where Twilio sends the webhook request. So, in response to example.com/conference.php in this case, you will need to dial another person and respond with TwiML to direct person A into the conference call.

This time, instead of sending a URL, you can actually send TwiML in the REST API response. Something like this:

use Twilio\Rest\Client;
use Twilio\TwiML\VoiceResponse;

// Find your Account Sid and Auth Token at twilio.com/console
// and set the environment variables. See http://twil.io/secure
$sid = getenv("TWILIO_ACCOUNT_SID");
$token = getenv("TWILIO_AUTH_TOKEN");
$twilio = new Client($sid, $token);

$twiml = new VoiceResponse();
$dial = $twiml->dial();
$dial->conference("Conference Name");

$call = $twilio->calls
               ->create($personBNumber, // to
                        $yourTwilioNumber, // from
                        ["twiml" => $twiml->toString()]
               );

echo $twiml.toString();

In this case, I have used the same TwiML for both legs of the call, because they are both entering the same conference. You could respond with different TwiML based on what's happening.

Let me know if that helps at all.

philnash
  • 70,667
  • 10
  • 60
  • 88