Extending Click-to-Call to Simultaneously Call Multiple Numbers

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.

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.

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.

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