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

Recording a phone call

Recording a call is handy for conducting interviews. In this example, we're going to build on the Click-to-Call recipe and add in the ability to record the call.

Getting ready

The complete source code for this recipe can be found at Chapter1/Recipe5.

How to do it...

This recipe will expand on our Click-to-Call system to include the ability to record the phone call. We'll also set up a nice method to retrieve recordings.

  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
      $toNumber = '';  //    YOUR PHONE NUMBER TO CONNECT TO
      $toEmail = ''; // YOUR EMAIL ADDRESS TO SEND RECORDING TO
    ?>
  4. Upload a file called record-call.php to your website:
    <?php
    session_start();
    include 'Services/Twilio.php';
    include("config.php");
    if( isset($_GET['msg']) )
      echo $msg;
    ?>
    <h3>Please enter your phone number, and you will beconnected to <?=$toNumber?></h3>
    <form action="makecall.php" method="post">
    <span>Your Number: <input type="text"name="called" /></span>
    <input type="submit" value="Connect me!" />
    </form>

    This file displays a form that, when submitted, triggers the rest of the calling process.

  5. Now, upload a file named makecall.php to your website:
    <?php
    session_start();
    include 'Services/Twilio.php';
    include("config.php");
    $client = new Services_Twilio($accountsid, $authtoken);
    if (!isset($_REQUEST['called'])) {
      $err = urlencode("Must specify your phone number");
      header("Location: record-call.php?msg=$err");
      die;
    }
    
    $url = (!empty($_SERVER['HTTPS'])) ?"https://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI']:"http://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
    $url = str_replace("makecall","recording",$url);
    
    $call = $client->account->calls->create($fromNumber, $to,'callback.php?number=' .$_REQUEST['called'],array("record"=>true));
    
    $msg = urlencode("Connecting... ".$call->sid);
    $_SESSION['csid'] = $call->sid;
    $RecordingUrl = $url."?csid=".$call->sid;
    $subject = "New phone recording from{$_REQUEST['called']}";
    $body = "You have a new phone recording from{$_REQUEST['called']}:\n\n";
    
    $body .= $RecordingUrl;
    
    $headers = 'From: noreply@'.$_SERVER['SERVER_NAME']. "\r\n" .
      'Reply-To: noreply@'.$_SERVER['SERVER_NAME'] . "\r\n" .
      'X-Mailer: Twilio';
    mail($toEmail, $subject, $body, $headers);
    header("Location: record-call.php?msg=$msg");
    ?>

    The makecall.php file handles the actual setting up of the call and also sends you an e-mail that provides you with a link to view the recording.

  6. Next, upload a file named callback.php to your website:
    <?php
      header("content-type: text/xml");
      echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
    ?>
    <Response>
      <Say>A customer at the number <?php echo$_REQUEST['number']?> is calling</Say>
      <Dial record=true><?php echo $_REQUEST['number']?></Dial>
    </Response>

    Did you catch what we did here? We told the Dial command to record the call. This means anything that is spoken during this call is now recorded.

  7. Finally, upload a file named recording.php to your website:
    <?php
    if( isset($_GET['csid']) ){
      getRecording( $_GET['csid'] );
    }else{
      die( "Invalid recording!");
    }
    function getRecording($caSID){
      global $accountsid,$authtoken;
        $version = '2010-04-01';
        $url = "https://api.twilio.com/2010-04-01/Accounts/{$accountsid}/Calls/{$caSID}/Recordings.xml";
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_USERPWD,"{$accountsid}:{$authtoken}");
        curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
        $output = curl_exec($ch);
        $info = curl_getinfo($ch);
        curl_close($ch);
        $output = simplexml_load_string($output);
        echo "<table>";
        foreach ($output->Recordings->Recording as $recording)
            {
          echo "<tr>";
          echo "<td>".$recording->Duration." seconds</td>";
          echo "<td>".$recording->DateCreated."</td>";
          echo '<td><audio src="https://api.twilio.com/2010-04-01/Accounts/'.$sid.'/Recordings/'.$recording->Sid.'.mp3" controls preload="auto"autobuffer></audio></td>';
            echo "</tr>";
        }
        echo "</table>";
    }

How it works...

In steps 1 and 2, we downloaded and installed the Twilio Helper Library for PHP.

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

In steps 4, 5, and 6, we re-created the Click-to-Call functionality from the previous recipe but with one difference: we also set makecall.php to e-mail us a link to do the recording, as well as setting callback.php to actually do the recording.

As with the preceding Adding Click-to-Call functionality to your website recipe, a user is presented with a form on the website where they enter their information and click to begin a call. The difference here is that the call is actually recorded; once it's finished, the system e-mails you a link to listen to your recording.

One thing to remember with recordings is that it could take a few minutes after the call for the recording to be available. Hence, the script e-mails you a link to view the recording instead of the recording itself.