Twilio Cookbook
上QQ阅读APP看书,第一时间看更新

Screening calls

Call screening is a useful ability to have on your calling systems. For example, let's say you have three people on call in your support department and you want to call the first available agent.

This recipe will try and connect to each phone number available in a given array and check to see if a person answers or not; if a person does, it connects the call.

Getting ready

The complete source code for this recipe can be found in the Chapter2/Recipe2 folder.

How to do it...

We're going to build a call-handling system that will forward calls to our list of agents; the first agent who accepts the call by pushing a button will get the call.

  1. Download the Twilio Helper Library from https://github.com/twilio/twilio-php/zipball/master and unzip it.
  2. Upload the Services/ folder to your website.
  3. Upload config.php to your website and make sure the following variables are set:
    <?php
      $accountsid = '';  //  YOUR TWILIO ACCOUNT SID
      $authtoken = '';  //  YOUR TWILIO AUTH TOKEN
      $fromNumber = '';  //  PHONE NUMBER CALLS WILL COME FROM
    ?>
  4. The call-screening feature we are building will consist of three files. The first file is called call-screening.php and contains the following code:
    <?php 
    $numbers = array("1234567890", "1234567891", "1234567892");
    $number_index = isset($_REQUEST['number_index']) ? $_REQUEST['number_index'] : "0";
    $DialCallStatus = isset($_REQUEST['DialCallStatus']) ? $_REQUEST['DialCallStatus'] : "";
    header("content-type: text/xml"); 
    if($DialCallStatus!="completed" && $number_index<count($numbers)){ 
    ?>
      <Response>
        <Dial action="call-screening.php?number_index=<?php echo $number_index+1 ?>">
      <Number url="areyouhuman.xml">
          <?php echo $numbers[$number_index] ?>
        </Number>
        </Dial>
      </Response>
    <?php
    } else {
    ?>
      <Response>
        <Hangup/>
      </Response>
    <?php
    }
    ?>
  5. When the phone is answered, we trigger a call to areyouhuman.xml that prompts the person answering the phone to press any key. Pressing any key will notify the system that it is indeed a person.
    <?xml version="1.0" encoding="UTF-8"?>
    <Response>
        <Gather action="iamhuman.xml">
            <Say>Press any key to accept this call</Say>
        </Gather>
        <Hangup/>
    </Response>
  6. Since the person who was called did press a key, we assume he or she is a real person, and not a machine, and connect the call.
      <?xml version="1.0" encoding="UTF-8"?>
      <Response>
        <Say>Connecting</Say>
      </Response>
  7. Finally, you have to point your Twilio phone number to it.

    Insert the URL to this page in the Voice Request URL box. Then, any calls that you receive at this number will be processed via call-screening.php.

How it works...

In steps 1 and 2, we downloaded and installed the Twilio Helper Library for PHP. This library is at the heart of your Twilio-powered apps.

In step 3, we uploaded config.php that contains our authentication information to talk to Twilio's API.

In step 4, we uploaded call-screening.php; in steps 5 and 6, we created areyouhuman.xml and iamhuman.xml.

Finally, in step 7, we configured a phone number to direct all calls to call-screening.php.

Now, all calls to this phone number are sent directly to call-screening.php. The app then directs the call to the first number on the $numbers list.

When the call is answered, we trigger areyouhuman.xml, which waits for the person who answered the call to hit any key on their phone.

When any key is pressed, we can safely assume that the person who answered the phone is a person and not an answering machine; we then connect them to iamhuman.xml that then connects the call. If no key is pressed, we try the next number on the list.

As I mentioned, this type of recipe comes in handy for having multiple on-call agents in departments such as Support and Sales.