php & twilio
Post on 15-Dec-2014
1.501 Views
Preview:
DESCRIPTION
TRANSCRIPT
1 PHP & Twilio, PHPNW 2011
PHP & TWILIOMichael Peacock, October, 2011
2 PHP & Twilio, PHPNW 2011
ABOUT ME
• Senior/Lead Web Developer• Web Systems Developer
• Telemetry Team – Smith Electric Vehicles US Corp
• Author• PHP 5 Social Networking, PHP 5 E-Commerce
Development, Drupal Social Networking (6 & 7), Selling online with Drupal e-Commerce, Building Websites with TYPO3
• PHPNE Volunteer• Occasional technical speaker
• PHP North-East, PHPNW 2010, SuperMondays, PHPNW 2011 Unconference, ConFoo 2012
3 PHP & Twilio, PHPNW 2011
NO. NOT MILK FLOATS (ANYMORE)
Photo courtesy of kenjonbro: http://www.flickr.com/photos/kenjonbro/4037649210/in/set-72157623026469013
4 PHP & Twilio, PHPNW 2011
ALL ELECTRIC, COMMERCIAL VEHICLES.
5 PHP & Twilio, PHPNW 2011
SMITH ELECTRIC VEHICLES & TELEMETRY
• Worlds largest manufacturer of Commercial, all-electric vehicles
• Smith Link – on-board vehicle telematics system, capturing over 2500 data points each second on the vehicle and broadcasting them over mobile network
• ~400 telemetry enabled vehicles on the road• Worlds largest telemetry project outside of F1
6 PHP & Twilio, PHPNW 2011
Collection of “Infrastructure APIs”• Make and receive phone calls• Send and receive text messages• Buy phone numbers• Record caller messages• Conference calls• IVR: Interactive Voice Response• DTMF: Gathering number presses• Text to speech
7 PHP & Twilio, PHPNW 2011
SMITH, TELEMETRY & TWILIO
• Field Engineers• Phone up telemetry to see if a new device they have
installed in the field is broadcasting data• When in the field their access to laptop / Internet is limited• Gives instant feedback which is recorded by the system
• Customers• Proof of concept: Telephone access to telematics data
• Where is my truck? Geolocate• Current metrics: Battery, Current, Voltage, Temperature,
Speed• Can it get to...does the truck have enough charge to reach a
new destination
• Basic useful information for fleet managers away from their desks
8 PHP & Twilio, PHPNW 2011
APPLICATIONS
Potential use cases:• Verify customers phone number• Provide a telephone based entry point to your web
application• Let customers phone up to manage their account or track
their order on the move
• Lead generation• Numbers for lead sources to track conversions
• Customer service• When your contact form is submitted or an order placed,
automatically connect a member of your team to the customer
• Request a call back form
9 PHP & Twilio, PHPNW 2011
CONTROLLED THROUGH XML
• Caller dials your number
• Twilio calls your application, which returns XML instructions
• XML instructions dictate what Twilio should say, and where to send the user depending on their response
10 PHP & Twilio, PHPNW 2011
CONTROLLED THROUGH XML
Standard header:header('Content-type: text/xml');echo '<?xml version="1.0" encoding="UTF-8"?>';
• <Response>• <Gather>• <Say>• <Redirect>• <Dial>• <Play>• <Record>
11 PHP & Twilio, PHPNW 2011
API & PHP LIBRARY
• PHP Library simplifies the REST side of the API, including:
• Making calls
• Looking up transcripts
• Looking up call recordings
• Buying phone numbers
• Send SMS messages
• Control flow is handed through XML, which can be generated using the Twiml class of the PHP Library
• https://github.com/twilio/twilio-php
12 PHP & Twilio, PHPNW 2011
SANDBOX
• $30 free credit on sign up
• Sandbox number with access code
• Until you are ready to buy your own number, you can call their sandbox number, enter a PIN and it redirects to your application
13 PHP & Twilio, PHPNW 2011
PHONE NUMBERS
• $1 per month• Available for US, Canada and now (beta) UK• Buy online or through the rest API
14 PHP & Twilio, PHPNW 2011
MAKE A CALL
$client = new Services_Twilio(‘AC123', '123'); $call = $client->account->calls->create('FROM', 'CALL', 'http://mydomain.com/call-xml' );
Call ID is generated:$call->sid;
Length is recorded$call->length
15 PHP & Twilio, PHPNW 2011
SPEAK
<Say>Hello conference attendees</Say>
$response = new Services_Twilio_Twiml();$response->say(‘Hello conference attendees');echo $ output;
16 PHP & Twilio, PHPNW 2011
ASK FOR A RESPONSE
<Response><Gather action=“my-app-url/process"
numDigits="1"><Say>Welcome Conference PHP North
West 2011 Delegates.</Say><Say>For talks at 10 45 press 1.</Say>
</Gather><Say>Sorry, I didn't get your
response.</Say><Redirect>my-app-url</Redirect>
</Response>
17 PHP & Twilio, PHPNW 2011
ASK FOR A RESPONSE
$response = new Services_Twilio_Twiml();
$gather = $response->gather(array('numDigits' => 1));
$gather->say("Welcome Conference PHP North West 2011 Delegates.");
$gather->say("For talks at 10 45 press 1.");
$response->say("Sorry, I didn't catch that");
echo $response;
18 PHP & Twilio, PHPNW 2011
ACT ON A RESPONSE
$response = (int) $_REQUEST['Digits'];
header('Content-type: text/xml');echo '<?xml version="1.0" encoding="UTF-8"?>';Switch( $response ){
case 1:echo ‘<Response><Say>Track 1 details…</Say></Response>’break;case 2:echo ‘<Response><Say>Track 2 details…</Say></Response>’break;
}exit();
19 PHP & Twilio, PHPNW 2011
TRANSFER THE CALLER
<Dial>+44XXXXXXX</Dial>
20 PHP & Twilio, PHPNW 2011
ENFORCING STATE
Each stage of the application is accessible via a URL. Theoretically, someone could directly visit a deep link (if they know it) and see customer information.
• Simplest solution is to generate a unique token (with a short TTL) when caller first dials
• Token is then passed around to each <Redirect> response
• Any authentication details are associated with the token
• Tokens regularly cleaned up
21 PHP & Twilio, PHPNW 2011
TWILIO CONNECT
Lets you write Twilio applications, hosted and managed on your servers (free or charged for) but that connect directly to your customers Twilio account, so they can manage their own Twilio usage billing
22 PHP & Twilio, PHPNW 2011
TWIMLETS
• You don’t have to host code on your server
• If its a basic script, with no server interaction, you can use “twimlets” http://labs.twilio.com/twimlets/
• Forward calls• Ring up to 10 phones at once until one answers• Voice mail• Conference call• Etc
• Create them using an online wizard and Twilio hosts the XML
23 PHP & Twilio, PHPNW 2011
DEBUGGING
24 PHP & Twilio, PHPNW 2011
DEMO APPLICATION
Call: 0161 8840908
25 PHP & Twilio, PHPNW 2011
REALLY BASIC DEMO APPLICATION<?php$twilio = new TwilioDemo();class TwilioDemo{
public function __construct(){
header('Content-type: text/xml');echo '<?xml version="1.0" encoding="UTF-8"?>';
$route = ( isset( $_GET['route'] ) ) ? $_GET['route'] : '';
switch( $route ){
case 'process':$this->process();break;
default:case '';
$this->menu();break;
}}
Based on the URL, redirect the user
26 PHP & Twilio, PHPNW 2011
REALLY BASIC DEMO APPLICATION
Read the menu, if no response selected, repeat
private function menu(){
$out = <<<EOD<Response> <Gather action="http://www./twiliodemo/twiliodemo.php?route=process" numDigits="1"> <Say>Hello PHP North West Conference attendee. At Nine Thirty we have the keynote from Ian Barber, How
to Stand on the Shoulders of Giants.</Say> <Say>For talks at 10 45, press 1.</Say> <Say>For talks at 11 45, press 2</Say> <Say>Lunch is at 12 45</Say> <Say>For talks at 2 press 3.</Say> <Say>For talks at 3, press 4.</Say> <Say>For talks at 4 30, press 5.</Say> <Say>For un conference talks, press 6.</Say> </Gather> <!-- If caller didn't press any keys, prompt and try again. --> <Say>Sorry, I didn't get your response.</Say> <Redirect>http://www..co.uk/twiliodemo/twiliodemo.php?route=main</Redirect></Response>
EOD;echo $out;exit();
}
27 PHP & Twilio, PHPNW 2011
REALLY BASIC DEMO APPLICATION
private function process(){$selection = (int) $_REQUEST['Digits'];switch( $selection ){case 1:$this->ten45();break;case 2:$this->eleven45();break;case 3:$this->two();break;case 4:$this->three();break;case 5:$this->four30();break;case 5:$this->unconference();break;default:$this->oi();break;}}
Process based on the user input
28 PHP & Twilio, PHPNW 2011
REALLY BASIC DEMO APPLICATION
Read out the information
private function ten45(){
echo '<Response>';echo '<Say>Track 1: Zend Framework 2 - State Of The Art :
Enrico Zimuel.</Say>';echo '<Say>Track 2: REST and HATEOAS - A Case Study :
Ben Longden.</Say>';echo '<Say>Track 3: Magic Behind the Numbers - Software
Metrics In Practice : Sebastian Marek.</Say>';echo
'<Redirect>http://www.invoicecentral.co.uk/twiliodemo/twiliodemo.php?route=main</Redirect>';
echo '</Response>';}
29 PHP & Twilio, PHPNW 2011
CONCLUSION
• Twilio is really, really easy to use
• Build a simple application in a minute
• Build a complex application in five
• You control the responses through a new request after each user interaction
• Really cheap to use
30 PHP & Twilio, PHPNW 2011
Q & A
Michael PeacockWeb Systems Developer – Telemetry Team – Smith Electric Vehicles US Corpmichael.peacock@smithelectric.com
Senior / Lead Developer, Author & Entrepreneurme@michaelpeacock.co.uk www.michaelpeacock.co.uk
@michaelpeacock
http://joind.in/3819 http://www.slideshare.net/michaelpeacock
top related