• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
PHP & Twilio
 

PHP & Twilio

on

  • 1,654 views

PHPNW Lightning talk on Twilio with PHP

PHPNW Lightning talk on Twilio with PHP

Statistics

Views

Total Views
1,654
Views on SlideShare
1,653
Embed Views
1

Actions

Likes
0
Downloads
18
Comments
0

1 Embed 1

http://www.docseek.net 1

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    PHP & Twilio PHP & Twilio Presentation Transcript

    • PHP & Twilio
      Michael Peacock, October, 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
    • No. 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 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
    • 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
    • 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
    • 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
    • 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
    • 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>
    • 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
    • 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
    • Phone Numbers
      • $1 per month
      • Available for US, Canada and now (beta) UK
      • Buy online or through the rest API
    • Make 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
    • Speak
      <Say>Hello conference attendees</Say>
      $response = new Services_Twilio_Twiml();
      $response->say(‘Hello conference attendees');
      echo $ output;
    • 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>
    • 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;
    • 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();
    • Transfer the Caller
      <Dial>+44XXXXXXX</Dial>
    • 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
    • Twilio Connect
      Lets 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
    • 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
    • Debugging
    • Demo Application
      Call: 0161 8840908
    • 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
    • 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();
      }
    • 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
    • Really Basic Demo Application
      Read out the information
      private 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>';
      }
    • 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
    • Q & A
      Michael Peacock
      Web Systems Developer – Telemetry Team – Smith Electric Vehicles US Corp
      michael.peacock@smithelectric.com
      Senior / Lead Developer, Author & Entrepreneur
      me@michaelpeacock.co.uk
      www.michaelpeacock.co.uk
      @michaelpeacock
      http://joind.in/3819
      http://www.slideshare.net/michaelpeacock