官术网_书友最值得收藏!

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.
    How to do 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.

主站蜘蛛池模板: 九江市| 汉川市| 金山区| 安岳县| 手游| 江川县| 内黄县| 广德县| 云龙县| 沙河市| 民乐县| 和林格尔县| 通城县| 侯马市| 郧西县| 定西市| 青河县| 芦溪县| 濉溪县| 五原县| 都安| 安康市| 桓仁| 依兰县| 左云县| 泾川县| 贡觉县| 塔城市| 香港 | 双牌县| 沂水县| 澄城县| 屏东县| 博乐市| 安福县| 安泽县| 清徐县| 若尔盖县| 池州市| 友谊县| 宜昌市|