php & twilio

30
1 PHP & Twilio, PHPNW 2011 PHP & TWILIO Michael Peacock, October, 2011

Upload: michael-peacock

Post on 15-Dec-2014

1.501 views

Category:

Technology


0 download

DESCRIPTION

PHPNW Lightning talk on Twilio with PHP

TRANSCRIPT

Page 1: PHP & Twilio

1 PHP & Twilio, PHPNW 2011

PHP & TWILIOMichael Peacock, October, 2011

Page 2: PHP & Twilio

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

Page 3: PHP & Twilio

3 PHP & Twilio, PHPNW 2011

NO. NOT MILK FLOATS (ANYMORE)

Photo courtesy of kenjonbro: http://www.flickr.com/photos/kenjonbro/4037649210/in/set-72157623026469013

Page 4: PHP & Twilio

4 PHP & Twilio, PHPNW 2011

ALL ELECTRIC, COMMERCIAL VEHICLES.

Page 5: PHP & Twilio

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

Page 6: PHP & Twilio

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

Page 7: PHP & Twilio

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

Page 8: PHP & Twilio

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

Page 9: PHP & Twilio

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

Page 10: PHP & Twilio

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>

Page 11: PHP & Twilio

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

Page 12: PHP & Twilio

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

Page 13: PHP & Twilio

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

Page 14: PHP & Twilio

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

Page 15: PHP & Twilio

15 PHP & Twilio, PHPNW 2011

SPEAK

<Say>Hello conference attendees</Say>

$response = new Services_Twilio_Twiml();$response->say(‘Hello conference attendees');echo $ output;

Page 16: PHP & Twilio

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>

Page 17: PHP & Twilio

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;

Page 18: PHP & Twilio

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();

Page 19: PHP & Twilio

19 PHP & Twilio, PHPNW 2011

TRANSFER THE CALLER

<Dial>+44XXXXXXX</Dial>

Page 20: PHP & Twilio

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

Page 21: PHP & Twilio

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

Page 22: PHP & Twilio

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

Page 23: PHP & Twilio

23 PHP & Twilio, PHPNW 2011

DEBUGGING

Page 24: PHP & Twilio

24 PHP & Twilio, PHPNW 2011

DEMO APPLICATION

Call: 0161 8840908

Page 25: PHP & Twilio

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

Page 26: PHP & Twilio

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();

}

Page 27: PHP & Twilio

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

Page 28: PHP & Twilio

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>';}

Page 29: PHP & Twilio

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

Page 30: PHP & Twilio

30 PHP & Twilio, PHPNW 2011

Q & A

Michael PeacockWeb Systems Developer – Telemetry Team – Smith Electric Vehicles US [email protected]

Senior / Lead Developer, Author & [email protected] www.michaelpeacock.co.uk

@michaelpeacock

http://joind.in/3819 http://www.slideshare.net/michaelpeacock