Mauro
Mauro

Reputation: 1487

How to connect 2 calls with Twilio

I'm making a very simple application with Twilio. A customer calls a Twilio number and twilio put this person <Enqueue>. I'm storing the CallSid identifier on a database for later use. When the customer call, twilio make another call to another person with the twilio-php sdk. This person can reject o accept the call.

This is running well.

My problem is: if the person accept the call how I can connect (so they can talk together) this 2 calls? Is possible with the <Dial> verb and the CallSid?

Upvotes: 1

Views: 2117

Answers (1)

xmjw
xmjw

Reputation: 3434

Twilio Evangelist here,

You can do this using the <Dial> and <Queue> TwiML verbs.

When a customer calls you, you probably want to connect them to another person we'll call the agent. So, to place the customer in the call queue (which it sounds like you've got working):

<Response>
  <Enqueue>some-queue-name</Enqueue>
</Response>

It's worth setting a waitUrl attribute on the <Enqueue> so that you can have status messages or hold music for the caller. When your agent accepts the call, all you need to is use <Dial> and <Queue> to connect them together:

<Response>
  <Dial>
    <Queue>some-queue-name</Queue>
  </Dial>
</Response>

At this point the customer is talking to the agent, and the calls are connected.

It's also worth adding, that if you keep the CallSid you can also use the REST API to make changes to a live call. Effectively, you instruct Twilio to start executing different TwiML. In PHP:

<?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 = "AC7957fb240ace4634d57f3888c4966461"; 
$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("CAe1644a7eed5088b159577c5802d8be38");
$call->update(array(
    "Url" => "http://demo.twilio.com/docs/voice.xml",
    "Method" => "POST"));
echo $call->to;

Hope that helps!

Upvotes: 2

Related Questions