- 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.
- EJB 3.1從入門到精通
- CorelDRAW X6 中文版圖形設計實戰從入門到精通
- Building Django 2.0 Web Applications
- Learning Karaf Cellar
- 局域網組建、管理與維護項目教程(Windows Server 2003)
- 通信簡史:從信鴿到6G+
- 面向5G-Advanced的關鍵技術
- 基于性能的保障理論與方法
- Windows Server 2012 Hyper-V虛擬化管理實踐
- 工業互聯網創新實踐
- 網管第一課:網絡操作系統與配置管理
- 大型企業微服務架構實踐與運營
- 云工廠:開啟中國制造云時代
- Building RESTful Web Services with .NET Core
- 世界互聯網發展報告2021