Setting up a voicemail system

All companies need a voicemail system, from a small one-person company to a big 100-person company.

This voicemail system will be set up as one big mailbox that people can call into and leave a message. The message is then e-mailed to you along with a transcription of the message.

Getting ready

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

How to do it...

Let's build a simple voicemail system that can serve as a mailbox for your company.

  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. Create a file on your website called voicemail.php, with the following code:
    <?php
    include 'Services/Twilio.php';
    include("config.php");
    
    $myemail = 'MYEMAIL@me.com';
    $message = 'I am not available right now. Please leave a message.';
    $transcribe = true;
    
    $client = new Services_Twilio($accountsid, $authtoken);
    $response = new Services_Twilio_Twiml();
    
    $headers = 'From: voicemail@mywebsite.com' . "\r\n" .'Reply-To: voicemail@mywebsite.com' . "\r\n" .'X-Mailer: Twilio Voicemail';
    
    $from = strlen($_REQUEST['From']) ? $_REQUEST['From'] : $_REQUEST['Caller'];
    $to = strlen($_REQUEST['To']) ? $_REQUEST['To'] : $_REQUEST['Called'];
    
    if( strtolower($_REQUEST['TranscriptionStatus']) == "completed") {
      $body = "You have a new voicemail from " . ($from) . "\n\n";
      $body .= "Text of the transcribed voicemail:\n{$_REQUEST['TranscriptionText']}.\n\n";
      $body .= "Click this link to listen to the message:\n{$_REQUEST['RecordingUrl']}.mp3";
      mail($myemail, "New Voicemail Message from " . ($from), $body, $headers);
      die;
    } else if(strtolower($_REQUEST['TranscriptionStatus']) == "failed") {
      $body = "You have a new voicemail from ".($from)."\n\n";
      $body .= "Click this link to listen to the message:\n{$_REQUEST['RecordingUrl']}.mp3";
      mail($myemail, "New Voicemail Message from " . ($from), $body, $headers);
      die;
    } else if(strlen($_REQUEST['RecordingUrl'])) {
      $response->say("Thanks.  Good bye.");
      $response->hangup();
      if(strlen($transcribe) && strtolower($transcribe) != 'true') {
        $body = "You have a new voicemail from ".($from)."\n\n";
        $body .= "Click this link to listen to the message:\n{$_REQUEST['RecordingUrl']}.mp3";
        mail($myemail, "New Voicemail Message from " . ($from), $body, $headers);
      }
    } else {
      $response->say( $message );
      if( $transcribe )
        $params = array("transcribe"=>"true", "transcribeCallback"=>"{$_SERVER['SCRIPT_URI']}");
      else
        $params = array();
      $response->record($params);
    }
    $response->Respond(); 
    ?>
    Note

    This voicemail system is pretty basic. We repeat a message and then prompt the caller to leave their message; we then supply an e-mail of the link to the recording as well as the transcription, if transcribing was turned on.

  5. To have a number point to this script, upload voicemail.php somewhere and then 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 voicemail.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 communicate with Twilio's API.

In step 4, we uploaded voicemail.php.

Finally, in step 5, we configured a phone number to direct all calls to voicemail.php.

When a user calls into this number, we supply a voicemail box and then send you an e-mail containing a transcription of the message, a link to the recording, and the name of the caller.