Extending Click-to-Call to Simultaneously Call Multiple Numbers

May 18, 2010
Written by
John Sheehan
Contributor
Opinions expressed by Twilio contributors are their own

Twilio Bug Logo

2585116025_17a91b480c_m
Did you know Twilio has an exhaustive list of HowTos and Example Code? Whether you’re new to Twilio, or just looking to get a jump start on your latest project, this is a great place to see some how some of the most common use cases are actually built. I like to think of it as the Twilio Cookbook full of tasty recipes for you to try at home!

Simultaneously Call Multiple Numbers with Click-to-Call

Click-to-call is one of our most popular examples. With click-to-call you can allow visitors to your web site call you directly with a couple clicks. The process usually goes like this: the visitor enters their phone number on the web site and submits the form, a call is initiated to the visitor, once they answer the phone another call is initiated to the company and the two ends are connected.

Instead of having the application just call one number, what if we wanted to have it dial 5, 10, or more numbers simultaneously and let the first person who answers take care of the call? If no one answers after a short period of time, we’ll prompt the caller to record a message. In this post I’ll start with the existing click-to-call example and update it to support dialing multiple numbers simultaneously.

Key Changes in This Example

This example will update the current click-to-call example in a few key places:

  1. After the visitor submits the form our application will call them first instead of calling the agent first.
  2. Once the visitor answers the phone we will play a short message indicating that we are connecting their call.
  3. After the message we’ll use Twilio’s simultaneous dial technique to connect the visitor to an agent.
  4. If no agents respond after 30 seconds we’ll prompt the caller to record a message.

Starting the Call

We’ll start by adding a form to our HTML to collect the visitor’s phone number.

<h3>Please enter your phone number, and you will be connected to an agent</h3>
<form action="makecall.php" method="post">
<span>Your Number: <input type="text" name="called" /></span>
<input type="submit" value="Connect me!" />
</form>

Once the user presses submit, the form sends the number (via POST) to makecall.php. Makecall.php uses the free Twilio PHP Helper library to call the company’s phone number. I’ve removed some of the error handling in the original example for brevity.

<?php
require "twilio.php";
/* Set our AccountSid and AuthToken */
$AccountSid = "AXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
$AuthToken = "YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY";
/* Outgoing Caller ID you have previously validated with Twilio */
$number = 'NNNNNNNNNN';
/* Outgoing Number you wish to call */
$outgoing = $_REQUEST['called'];
/* Instantiate a new Twilio Rest Client */
$client = new TwilioRestClient($AccountSid, $AuthToken);
/* make Twilio REST request to initiate outgoing call */
$response = $client->request("/2008-08-01/Accounts/$AccountSid/Calls",
"POST", array(
"Caller" => $number,
"Called" => $outgoing,
"Url" => 'http://example.com/callback.php'
));
/* redirect back to the main page with CallSid */
$msg = urlencode("Connecting... ".$_REQUEST['called']);
header("Location: index.php?msg=$msg");
?>

Handling the Call Once Connected

At this point the web site visitor’s phone will be ringing. When they answer, Twilio will process the TwiML returned from callback.php to direct the call. Again we’ll use the Twilio PHP Helper library to generate and output the TwiML.

<?php
require("twilio.php");
$r = new Response();
$r->append(new Say("Thank you for calling! Please wait a moment while we connect your call."));
$d = new Dial(array("timeout" => "30"));
$numbers = array(
"555-555-1111",
"555-555-2222",
"555-555-3333",
"555-555-4444",
"555-555-5555",
"555-555-6666",
"555-555-7777",
"555-555-8888",
"555-555-9999",
);
for ($numbers as $number) {
$d->append(new Number($number));
}
$r->append($d);
// if no one answers, record a message
$r->append(new Say("We're sorry, no agents were available to take your call. Please leave a message."));
$r->append(new Record(array("action" => "record.php"));
$r->Respond();
?>

Simultaneous dial isn’t the right solution in every situation. You should take care to use simultaneous dial in situations where you know the behavior of the called parties. Keep in mind that the first phone that picks up cancels all the other connections. If you dial an office phone system or a cellphone in airplane mode, it may pick up after a single ring, preventing the other phones from ringing long enough for a human to ever answer.

Photo credit: Chiot’s Run on Flickr