PHP & Twilio<br />Michael Peacock, October, 2011<br />
About Me<br /><ul><li>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-Comm...
PHPNE Volunteer
Occasional technical speaker
PHP North-East, PHPNW 2010, SuperMondays, PHPNW 2011 Unconference, ConFoo 2012</li></li></ul><li>No. Not milk floats (anym...
All Electric, Commercial Vehicles.<br />
Smith Electric Vehicles & Telemetry	<br />Worlds largest manufacturer of Commercial, all-electric vehicles<br />Smith Link...
Collection of “Infrastructure APIs”<br /><ul><li>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</li></li></ul><li>Smith, Telemetry & Twilio<br /><ul><li>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</li></li></ul><li>Applications<br />Potential use cases:...
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
Upcoming SlideShare
Loading in …5
×

PHP & Twilio

2,090 views

Published on

PHPNW Lightning talk on Twilio with PHP

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
2,090
On SlideShare
0
From Embeds
0
Number of Embeds
6
Actions
Shares
0
Downloads
21
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

PHP & Twilio

  1. 1. PHP & Twilio<br />Michael Peacock, October, 2011<br />
  2. 2. About Me<br /><ul><li>Senior/Lead Web Developer
  3. 3. Web Systems Developer
  4. 4. Telemetry Team – Smith Electric Vehicles US Corp
  5. 5. Author
  6. 6. PHP 5 Social Networking, PHP 5 E-Commerce Development, Drupal Social Networking (6 & 7), Selling online with Drupal e-Commerce, Building Websites with TYPO3
  7. 7. PHPNE Volunteer
  8. 8. Occasional technical speaker
  9. 9. PHP North-East, PHPNW 2010, SuperMondays, PHPNW 2011 Unconference, ConFoo 2012</li></li></ul><li>No. Not milk floats (anymore)<br />Photo courtesy of kenjonbro: http://www.flickr.com/photos/kenjonbro/4037649210/in/set-72157623026469013<br />
  10. 10. All Electric, Commercial Vehicles.<br />
  11. 11. Smith Electric Vehicles & Telemetry <br />Worlds largest manufacturer of Commercial, all-electric vehicles<br />Smith Link – on-board vehicle telematics system, capturing over 2500 data points each second on the vehicle and broadcasting them over mobile network<br />~400 telemetry enabled vehicles on the road<br />Worlds largest telemetry project outside of F1<br />
  12. 12. Collection of “Infrastructure APIs”<br /><ul><li>Make and receive phone calls
  13. 13. Send and receive text messages
  14. 14. Buy phone numbers
  15. 15. Record caller messages
  16. 16. Conference calls
  17. 17. IVR: Interactive Voice Response
  18. 18. DTMF: Gathering number presses
  19. 19. Text to speech</li></li></ul><li>Smith, Telemetry & Twilio<br /><ul><li>Field Engineers
  20. 20. Phone up telemetry to see if a new device they have installed in the field is broadcasting data
  21. 21. When in the field their access to laptop / Internet is limited
  22. 22. Gives instant feedback which is recorded by the system
  23. 23. Customers
  24. 24. Proof of concept: Telephone access to telematics data
  25. 25. Where is my truck? Geolocate
  26. 26. Current metrics: Battery, Current, Voltage, Temperature, Speed
  27. 27. Can it get to...does the truck have enough charge to reach a new destination
  28. 28. Basic useful information for fleet managers away from their desks</li></li></ul><li>Applications<br />Potential use cases:<br /><ul><li>Verify customers phone number
  29. 29. Provide a telephone based entry point to your web application
  30. 30. Let customers phone up to manage their account or track their order on the move
  31. 31. Lead generation
  32. 32. Numbers for lead sources to track conversions
  33. 33. Customer service
  34. 34. When your contact form is submitted or an order placed, automatically connect a member of your team to the customer
  35. 35. Request a call back form</li></li></ul><li>Controlled Through XML<br /><ul><li>Caller dials your number
  36. 36. Twilio calls your application, which returns XML instructions
  37. 37. XML instructions dictate what Twilio should say, and where to send the user depending on their response</li></li></ul><li>Controlled through XML<br />Standard header:<br />header('Content-type: text/xml');<br />echo '<?xml version="1.0" encoding="UTF-8"?>';<br /><ul><li><Response>
  38. 38. <Gather>
  39. 39. <Say>
  40. 40. <Redirect>
  41. 41. <Dial>
  42. 42. <Play>
  43. 43. <Record></li></li></ul><li>API & PHP Library<br /><ul><li>PHP Library simplifies the REST side of the API, including:
  44. 44. Making calls
  45. 45. Looking up transcripts
  46. 46. Looking up call recordings
  47. 47. Buying phone numbers
  48. 48. Send SMS messages
  49. 49. Control flow is handed through XML, which can be generated using the Twiml class of the PHP Library
  50. 50. https://github.com/twilio/twilio-php</li></li></ul><li>Sandbox<br /><ul><li>$30 free credit on sign up
  51. 51. Sandbox number with access code
  52. 52. Until you are ready to buy your own number, you can call their sandbox number, enter a PIN and it redirects to your application</li></li></ul><li>Phone Numbers<br /><ul><li>$1 per month
  53. 53. Available for US, Canada and now (beta) UK
  54. 54. Buy online or through the rest API</li></li></ul><li>Make a call<br />$client = newServices_Twilio(‘AC123', '123'); <br />$call = $client->account->calls->create('FROM', 'CALL', 'http://mydomain.com/call-xml' );<br />Call ID is generated:<br />$call->sid;<br />Length is recorded<br />$call->length<br />
  55. 55. Speak<br /><Say>Hello conference attendees</Say><br />$response = new Services_Twilio_Twiml();<br />$response->say(‘Hello conference attendees');<br />echo $ output;<br />
  56. 56. Ask for a Response<br /><Response><br /> <Gather action=“my-app-url/process" numDigits="1"><br /><Say>Welcome Conference PHP North West 2011 Delegates.</Say><br /><Say>For talks at 10 45 press 1.</Say><br /></Gather><br /> <Say>Sorry, I didn't get your response.</Say><br /> <Redirect>my-app-url</Redirect><br /></Response><br />
  57. 57. Ask for a Response<br />$response = new Services_Twilio_Twiml();<br />$gather = $response->gather(array('numDigits' => 1));<br />$gather->say("Welcome Conference PHP North West 2011 Delegates.");<br />$gather->say("For talks at 10 45 press 1.");<br />$response->say("Sorry, I didn't catch that");<br />echo $response;<br />
  58. 58. Act on a Response<br />$response = (int) $_REQUEST['Digits'];<br />header('Content-type: text/xml');<br />echo '<?xml version="1.0" encoding="UTF-8"?>';<br />Switch( $response )<br />{<br />case 1:<br /> echo ‘<Response><Say>Track 1 details…</Say></Response>’<br /> break;<br />case 2:<br />echo ‘<Response><Say>Track 2 details…</Say></Response>’<br /> break;<br />}<br />exit();<br />
  59. 59. Transfer the Caller<br /><Dial>+44XXXXXXX</Dial><br />
  60. 60. Enforcing State<br />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.<br /><ul><li>Simplest solution is to generate a unique token (with a short TTL) when caller first dials
  61. 61. Token is then passed around to each <Redirect> response
  62. 62. Any authentication details are associated with the token
  63. 63. Tokens regularly cleaned up</li></li></ul><li>Twilio Connect<br />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<br />
  64. 64. Twimlets<br /><ul><li>You don’t have to host code on your server
  65. 65. If its a basic script, with no server interaction, you can use “twimlets” http://labs.twilio.com/twimlets/
  66. 66. Forward calls
  67. 67. Ring up to 10 phones at once until one answers
  68. 68. Voice mail
  69. 69. Conference call
  70. 70. Etc
  71. 71. Create them using an online wizard and Twilio hosts the XML</li></li></ul><li>Debugging<br />
  72. 72. Demo Application<br />Call: 0161 8840908<br />
  73. 73. Really Basic Demo Application<br /><?php<br />$twilio = new TwilioDemo();<br />class TwilioDemo{<br /> public function __construct()<br /> {<br /> header('Content-type: text/xml');<br /> echo '<?xml version="1.0" encoding="UTF-8"?>';<br /> $route = ( isset( $_GET['route'] ) ) ? $_GET['route'] : '';<br /> switch( $route )<br /> {<br /> case 'process':<br /> $this->process();<br /> break;<br /> default:<br /> case '';<br /> $this->menu();<br /> break;<br /> }<br /> }<br />Based on the URL, redirect the user<br />
  74. 74. Really Basic Demo Application<br />Read the menu, if no response selected, repeat<br />private function menu()<br /> {<br /> $out = <<<EOD<br /> <Response><br /> <Gather action="http://www./twiliodemo/twiliodemo.php?route=process" numDigits="1"><br /> <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><br /> <Say>For talks at 10 45, press 1.</Say><br /> <Say>For talks at 11 45, press 2</Say><br /> <Say>Lunch is at 12 45</Say><br /> <Say>For talks at 2 press 3.</Say><br /> <Say>For talks at 3, press 4.</Say><br /> <Say>For talks at 4 30, press 5.</Say><br /> <Say>For un conference talks, press 6.</Say><br /> </Gather><br /> <!-- If caller didn't press any keys, prompt and try again. --><br /> <Say>Sorry, I didn't get your response.</Say><br /> <Redirect>http://www..co.uk/twiliodemo/twiliodemo.php?route=main</Redirect><br /> </Response> <br />EOD;<br /> echo $out;<br /> exit();<br /> }<br />
  75. 75. Really Basic Demo Application<br />private function process()<br /> {<br /> $selection = (int) $_REQUEST['Digits'];<br /> switch( $selection )<br /> {<br /> case 1:<br /> $this->ten45();<br /> break;<br /> case 2:<br /> $this->eleven45();<br /> break;<br /> case 3:<br /> $this->two();<br /> break;<br /> case 4:<br /> $this->three();<br /> break;<br /> case 5:<br /> $this->four30();<br /> break;<br /> case 5:<br /> $this->unconference();<br /> break; <br /> default:<br /> $this->oi();<br /> break; <br /> } <br /> }<br />Process based on the user input<br />
  76. 76. Really Basic Demo Application<br />Read out the information<br />private function ten45()<br /> {<br /> echo '<Response>';<br /> echo '<Say>Track 1: Zend Framework 2 - State Of The Art : EnricoZimuel.</Say>';<br /> echo '<Say>Track 2: REST and HATEOAS - A Case Study : Ben Longden.</Say>';<br /> echo '<Say>Track 3: Magic Behind the Numbers - Software Metrics In Practice : Sebastian Marek.</Say>';<br /> echo '<Redirect>http://www.invoicecentral.co.uk/twiliodemo/twiliodemo.php?route=main</Redirect>';<br /> echo '</Response>';<br /> }<br />
  77. 77. Conclusion<br /><ul><li>Twilio is really, really easy to use
  78. 78. Build a simple application in a minute
  79. 79. Build a complex application in five
  80. 80. You control the responses through a new request after each user interaction
  81. 81. Really cheap to use</li></li></ul><li>Q & A<br />Michael Peacock<br />Web Systems Developer – Telemetry Team – Smith Electric Vehicles US Corp<br />michael.peacock@smithelectric.com<br />Senior / Lead Developer, Author & Entrepreneur<br />me@michaelpeacock.co.uk <br />www.michaelpeacock.co.uk<br />@michaelpeacock<br />http://joind.in/3819 <br />http://www.slideshare.net/michaelpeacock<br />

×