PHP & TwilioMichael Peacock, October, 2011
About MeSenior/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 2012No. Not milk floats (anymore)Photo courtesy of kenjonbro: http://www.flickr.com/photos/kenjonbro/4037649210/in/set-72157623026469013
All Electric, Commercial Vehicles.
Smith Electric Vehicles & Telemetry	Worlds largest manufacturer of Commercial, all-electric vehiclesSmith 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 roadWorlds largest telemetry project outside of F1
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 speechSmith, Telemetry & TwilioField 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 desksApplicationsPotential 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

PHP & Twilio

  • 1.
    PHP & TwilioMichaelPeacock, October, 2011
  • 2.
  • 3.
  • 4.
    Telemetry Team –Smith Electric Vehicles US Corp
  • 5.
  • 6.
    PHP 5 SocialNetworking, PHP 5 E-Commerce Development, Drupal Social Networking (6 & 7), Selling online with Drupal e-Commerce, Building Websites with TYPO3
  • 7.
  • 8.
  • 9.
    PHP North-East, PHPNW2010, SuperMondays, PHPNW 2011 Unconference, ConFoo 2012No. Not milk floats (anymore)Photo courtesy of kenjonbro: http://www.flickr.com/photos/kenjonbro/4037649210/in/set-72157623026469013
  • 10.
  • 11.
    Smith Electric Vehicles& Telemetry Worlds largest manufacturer of Commercial, all-electric vehiclesSmith 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 roadWorlds largest telemetry project outside of F1
  • 12.
    Collection of “InfrastructureAPIs”Make and receive phone calls
  • 13.
    Send and receivetext messages
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
    Text to speechSmith,Telemetry & TwilioField Engineers
  • 20.
    Phone up telemetryto see if a new device they have installed in the field is broadcasting data
  • 21.
    When in thefield their access to laptop / Internet is limited
  • 22.
    Gives instant feedbackwhich is recorded by the system
  • 23.
  • 24.
    Proof of concept:Telephone access to telematics data
  • 25.
    Where is mytruck? Geolocate
  • 26.
    Current metrics: Battery,Current, Voltage, Temperature, Speed
  • 27.
    Can it getto...does the truck have enough charge to reach a new destination
  • 28.
    Basic useful informationfor fleet managers away from their desksApplicationsPotential use cases:Verify customers phone number
  • 29.
    Provide a telephonebased entry point to your web application
  • 30.
    Let customers phoneup to manage their account or track their order on the move
  • 31.
  • 32.
    Numbers for leadsources to track conversions
  • 33.
  • 34.
    When your contactform is submitted or an order placed, automatically connect a member of your team to the customer
  • 35.
    Request a callback formControlled Through XMLCaller dials your number
  • 36.
    Twilio calls yourapplication, which returns XML instructions
  • 37.
    XML instructions dictatewhat Twilio should say, and where to send the user depending on their responseControlled through XMLStandard header:header('Content-type: text/xml');echo '<?xml version="1.0" encoding="UTF-8"?>';<Response>
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
    <Record>API & PHPLibraryPHP Library simplifies the REST side of the API, including:
  • 44.
  • 45.
  • 46.
    Looking up callrecordings
  • 47.
  • 48.
  • 49.
    Control flow ishanded through XML, which can be generated using the Twiml class of the PHP Library
  • 50.
  • 51.
  • 52.
    Until you areready to buy your own number, you can call their sandbox number, enter a PIN and it redirects to your applicationPhone Numbers$1 per month
  • 53.
    Available for US,Canada and now (beta) UK
  • 54.
    Buy online orthrough the rest APIMake a call$client = newServices_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
  • 55.
    Speak<Say>Hello conference attendees</Say>$response= new Services_Twilio_Twiml();$response->say(‘Hello conference attendees');echo $ output;
  • 56.
    Ask for aResponse<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>
  • 57.
    Ask for aResponse$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;
  • 58.
    Act on aResponse$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();
  • 59.
  • 60.
    Enforcing StateEach stageof 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
  • 61.
    Token is thenpassed around to each <Redirect> response
  • 62.
    Any authentication detailsare associated with the token
  • 63.
    Tokens regularly cleanedupTwilio ConnectLets you write Twilio applications, hosted and managed on your servers (free or charged for) but that connect directly to your customers Twilioaccount, so they can manage their own Twilio usage billing
  • 64.
    TwimletsYou don’t haveto host code on your server
  • 65.
    If its abasic script, with no server interaction, you can use “twimlets” http://labs.twilio.com/twimlets/
  • 66.
  • 67.
    Ring up to10 phones at once until one answers
  • 68.
  • 69.
  • 70.
  • 71.
    Create them usingan online wizard and Twilio hosts the XMLDebugging
  • 72.
  • 73.
    Really Basic DemoApplication<?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
  • 74.
    Really Basic DemoApplicationRead the menu, if no response selected, repeatprivate 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(); }
  • 75.
    Really Basic DemoApplicationprivate 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
  • 76.
    Really Basic DemoApplicationRead out the informationprivate function ten45() { echo '<Response>'; echo '<Say>Track 1: Zend Framework 2 - State Of The Art : EnricoZimuel.</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>'; }
  • 77.
    ConclusionTwilio is really,really easy to use
  • 78.
    Build a simpleapplication in a minute
  • 79.
    Build a complexapplication in five
  • 80.
    You control theresponses through a new request after each user interaction
  • 81.
    Really cheap touseQ & AMichael PeacockWeb Systems Developer – Telemetry Team – Smith Electric Vehicles US [email protected] / Lead Developer, Author & [email protected] www.michaelpeacock.co.uk@michaelpeacockhttp://joind.in/3819 http://www.slideshare.net/michaelpeacock