- Twilio Cookbook(Second Edition)
- Roger Stringer
- 380字
- 2021-07-16 12:06:37
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.
- Download the Twilio Helper Library from https://github.com/twilio/twilio-php/zipball/master and unzip it.
- Upload the
Services/
folder to your website. - 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 ?>
- 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(); ?>
- 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.
- 網(wǎng)絡(luò)教育學(xué)習(xí)指導(dǎo)
- RCNP實(shí)驗(yàn)指南:構(gòu)建高級(jí)的路由互聯(lián)網(wǎng)絡(luò)(BARI)
- Cisco OSPF命令與配置手冊(cè)
- CorelDRAW X6 中文版圖形設(shè)計(jì)實(shí)戰(zhàn)從入門到精通
- SEO 20日
- 物聯(lián)網(wǎng)安全與深度學(xué)習(xí)技術(shù)
- 物聯(lián)網(wǎng)關(guān)鍵技術(shù)及應(yīng)用
- 計(jì)算機(jī)網(wǎng)絡(luò)安全實(shí)訓(xùn)教程(第二版)
- SSL VPN : Understanding, evaluating and planning secure, web/based remote access
- 雷達(dá)饋線技術(shù)
- Mastering Dart
- C/C++串口通信:典型應(yīng)用實(shí)例編程實(shí)踐
- Bonita Open Solution 5.x Essentials
- 基于IPv6的家居物聯(lián)網(wǎng)開(kāi)發(fā)與應(yīng)用技術(shù)
- 物聯(lián)網(wǎng)M2M開(kāi)發(fā)技術(shù):基于無(wú)線CPU-Q26XX