71
Phone Calls and SMS from PHP PHP World 2016 David Stockton November 16, 2016

Phone calls and sms from php

Embed Size (px)

Citation preview

Page 1: Phone calls and sms from php

Phone Calls and SMS from PHP

PHP World 2016 David Stockton

November 16, 2016

Page 2: Phone calls and sms from php

Standard Interactions

Page 3: Phone calls and sms from php

Input

Page 4: Phone calls and sms from php

Web Pages / Forms

Page 5: Phone calls and sms from php

APIs

Page 6: Phone calls and sms from php

CLI

Page 7: Phone calls and sms from php

Output

Page 8: Phone calls and sms from php

HTML

Page 9: Phone calls and sms from php

Graphics

Page 10: Phone calls and sms from php

Documents

Page 11: Phone calls and sms from php

Reports

Page 12: Phone calls and sms from php

JSON

Page 13: Phone calls and sms from php

Console Output

Page 14: Phone calls and sms from php

Files

Page 15: Phone calls and sms from php

How else can we receive input and produce

output?

Page 16: Phone calls and sms from php

Input: Phone Call

Page 17: Phone calls and sms from php

Input: SMS Message

Page 18: Phone calls and sms from php

Output: Text Message

Page 19: Phone calls and sms from php

Output: MMS Message

Page 20: Phone calls and sms from php

Output: Phone Call

Page 21: Phone calls and sms from php

APIs for Phone Interaction

Page 22: Phone calls and sms from php
Page 23: Phone calls and sms from php

Twilio SDK

composer require twilio/sdk

Page 24: Phone calls and sms from php

Let's Send an SMS

new \Twilio\Rest\Client($config['sid'], $config['token']);

Make a client

Page 25: Phone calls and sms from php

Let's Send an SMS

$client->messages->create( $toNumber, array( 'from' => '+1<purchased#>', 'body' => $fortune ));

Page 26: Phone calls and sms from php

All Together$toNumber = $argv[1] ?? '<default #>';$toNumber = \PhpWorld\Utility::normalizeNumber($toNumber);$fortune = $argv[2] ?? `fortune`;$client->messages->create( $toNumber, array( 'from' => '+<purchased #>', 'body' => $fortune ));

Page 27: Phone calls and sms from php

How to Use?

$ php bin/fortune.php

$ php bin/fortune.php <destination #>

$ php bin/fortune.php <destination #> "Text message from the command line"

Page 28: Phone calls and sms from php

php bin/fortune.php

Page 29: Phone calls and sms from php

php bin/fortune.php 7206757471

Page 30: Phone calls and sms from php

php bin/fortune.php 7206757471 "Your account has been created or something."

Page 31: Phone calls and sms from php

What can we do?

• Notifications

• Two-Factor Authentication (2FA)

• Responses to Queries

Page 32: Phone calls and sms from php

Make a Phone Call

• API Call to Twitter

• Twitter contacts URL for instructions

Page 33: Phone calls and sms from php

TwiML

• Twilio's Markup Language for controlling phone calls

• XML

Page 34: Phone calls and sms from php

TwiML Commands• Say - Read text to the caller

• Play - Play an audio file for the caller

• Dial - Add another party to the call

• Record - Record the caller's voice

• Gather - Collect digits the caller types on their keypad

• Sms - Send an SMS message during a phone call

Page 35: Phone calls and sms from php

More TwiML• Hangup - Hang up the call

• Queue - Add the caller to a queue of callers.

• Redirect - Redirect call flow to a different TwiML document.

• Pause - Wait before executing more instructions

• Reject - Decline an incoming call without being billed.

• Message - Send an MMS or SMS message reply

Page 36: Phone calls and sms from php

How Outgoing Calls Work

Your App TwilioAPI Call

TwiMLCalleeTwilio Plays Twiml

Page 37: Phone calls and sms from php

TwiML

<?xml version="1.0" encoding="UTF-8"?><Response> <Say voice="woman">Your call is very important to us, but not so important we'll answer it with a human. Please leave a message. </Say> <Record maxLength="20" playBeep="true"/></Response>

Page 38: Phone calls and sms from php

XML

<?xml version="1.0" encoding="UTF-8"?><Response> <Say voice="woman">Your call is very important to us, but not so important we'll answer it with a human. Please leave a message. </Say> <Record maxLength="20" playBeep="true"/></Response>

Page 39: Phone calls and sms from php

Root Element

<?xml version="1.0" encoding="UTF-8"?><Response> <Say voice="woman">Your call is very important to us, but not so important we'll answer it with a human. Please leave a message. </Say> <Record maxLength="20" playBeep="true"/></Response>

Page 40: Phone calls and sms from php

Commands - Say

<?xml version="1.0" encoding="UTF-8"?><Response> <Say voice="woman">Your call is very important to us, but not so important we'll answer it with a human. Please leave a message. </Say> <Record maxLength="20" playBeep="true"/></Response>

Page 41: Phone calls and sms from php

<?xml version="1.0" encoding="UTF-8"?><Response> <Say voice="woman">Your call is very important to us, but not so important we'll answer it with a human. Please leave a message. </Say> <Record maxLength="20" playBeep="true"/></Response>

Commands - Record

Page 42: Phone calls and sms from php

Configure Twilio

Page 43: Phone calls and sms from php

Let's Make a Call

<?xml version="1.0" encoding="UTF-8"?><Response> <Say voice="man"> Due to inclement weather, school will be closed tomorrow. Please stay home and stay safe. </Say></Response>

Page 44: Phone calls and sms from php

The Full File

<?php header('Content-Type: text/xml'); ?><?xml version="1.0" encoding="UTF-8"?><Response> <Say voice="man" loop="2"> Due to inclement weather, school will be closed tomorrow. Please stay home and stay safe. </Say></Response>

Page 45: Phone calls and sms from php

Tell Twilio Who To Call<?phprequire_once __DIR__ . '/../vendor/autoload.php';/** @var \Twilio\Rest\Client $client */$client = require_once __DIR__ . '/../src/PhpWorld/client.php';$to = \PhpWorld\Utility::normalizeNumber($argv[1] ?? '+<default #>');$from = '+<purchased #>';$options = [ 'url' => 'http://' . $_SERVER['HTTP_HOST'] . '/twiml/school_outage.php',];$client->calls->create($to, $from, $options);

Page 46: Phone calls and sms from php

Tell Twilio Who To Call<?phprequire_once __DIR__ . '/../vendor/autoload.php';/** @var \Twilio\Rest\Client $client */$client = require_once __DIR__ . '/../src/PhpWorld/client.php';$to = \PhpWorld\Utility::normalizeNumber($argv[1] ?? '+<default #>');$from = '+<purchased #>';$options = [ 'url' => 'http://' . $_SERVER['HTTP_HOST'] . '/twiml/school_outage.php',];$client->calls->create($to, $from, $options);

Page 47: Phone calls and sms from php

Tell Twilio Who To Call<?phprequire_once __DIR__ . '/../vendor/autoload.php';/** @var \Twilio\Rest\Client $client */$client = require_once __DIR__ . '/../src/PhpWorld/client.php';$to = \PhpWorld\Utility::normalizeNumber($argv[1] ?? '+<default #>');$from = '+<purchased #>';$options = [ 'url' => 'http://' . $_SERVER['HTTP_HOST'] . '/twiml/school_outage.php',];$client->calls->create($to, $from, $options);

Page 48: Phone calls and sms from php

Tell Twilio Who To Call<?phprequire_once __DIR__ . '/../vendor/autoload.php';/** @var \Twilio\Rest\Client $client */$client = require_once __DIR__ . '/../src/PhpWorld/client.php';$to = \PhpWorld\Utility::normalizeNumber($argv[1] ?? '+<default #>');$from = '+<purchased #>';$options = [ 'url' => 'http://' . $_SERVER['HTTP_HOST'] . '/twiml/school_outage.php',];$client->calls->create($to, $from, $options);

Page 49: Phone calls and sms from php

Tell Twilio Who To Call<?phprequire_once __DIR__ . '/../vendor/autoload.php';/** @var \Twilio\Rest\Client $client */$client = require_once __DIR__ . '/../src/PhpWorld/client.php';$to = \PhpWorld\Utility::normalizeNumber($argv[1] ?? '+<default #>');$from = '+<purchased #>';$options = [ 'url' => 'http://' . $_SERVER['HTTP_HOST'] . '/twiml/school_outage.php',];$client->calls->create($to, $from, $options);

Page 50: Phone calls and sms from php

Tell Twilio Who To Call<?phprequire_once __DIR__ . '/../vendor/autoload.php';/** @var \Twilio\Rest\Client $client */$client = require_once __DIR__ . '/../src/PhpWorld/client.php';$to = \PhpWorld\Utility::normalizeNumber($argv[1] ?? '+<default #>');$from = '+<purchased #>';$options = [ 'url' => 'http://' . $_SERVER['HTTP_HOST'] . '/twiml/school_outage.php',];$client->calls->create($to, $from, $options);

Page 51: Phone calls and sms from php

Phone and SMS Conversations

• Twilio is like a well-behaved HTTP client

• It uses sessions

• We can use session to remember things about the caller or the conversation

Page 52: Phone calls and sms from php

Phone IVR

• Give callers a menu

• Look up information

• Connect to people

• Solve problems

• Give answers

Page 53: Phone calls and sms from php

First TwiML<?php header('Content-Type: text/xml'); ?> <?xml version="1.0" encoding="UTF-8"?> <Response> <Say> Thanks for calling my demo IVR. Please listen closely as this is the first time you've heard this menu. </Say> <Gather numDigits="1" action="/twiml/process_ivr_menu.php"> <Say> Press 1 to hear a random sound. Press 2 to connect to a conference call. Press 3 to hear some wisdom. Press 4 to be connected to support. </Say> </Gather> <Say> I don't understand. Please try again later. </Say> <Hangup/></Response>

Page 54: Phone calls and sms from php

<?php header('Content-Type: text/xml'); ?> <?xml version="1.0" encoding="UTF-8"?> <Response> <Say> Thanks for calling my demo IVR. Please listen closely as this is the first time you've heard this menu. </Say> <Gather numDigits="1" action="/twiml/process_ivr_menu.php"> <Say> Press 1 to hear a random sound. Press 2 to connect to a conference call. Press 3 to hear some wisdom. Press 4 to be connected to support. </Say> </Gather> <Say> I don't understand. Please try again later. </Say> <Hangup/></Response>

Greeting

Page 55: Phone calls and sms from php

<?php header('Content-Type: text/xml'); ?> <?xml version="1.0" encoding="UTF-8"?> <Response> <Say> Thanks for calling my demo IVR. Please listen closely as this is the first time you've heard this menu. </Say> <Gather numDigits="1" action="/twiml/process_ivr_menu.php"> <Say> Press 1 to hear a random sound. Press 2 to connect to a conference call. Press 3 to hear some wisdom. Press 4 to be connected to support. </Say> </Gather> <Say> I don't understand. Please try again later. </Say> <Hangup/></Response>

Menu

Page 56: Phone calls and sms from php

Error Handling<?php header('Content-Type: text/xml'); ?> <?xml version="1.0" encoding="UTF-8"?> <Response> <Say> Thanks for calling my demo IVR. Please listen closely as this is the first time you've heard this menu. </Say> <Gather numDigits="1" action="/twiml/process_ivr_menu.php"> <Say> Press 1 to hear a random sound. Press 2 to connect to a conference call. Press 3 to hear some wisdom. Press 4 to be connected to support. </Say> </Gather> <Say> I don't understand. Please try again later. </Say> <Hangup/></Response>

Page 57: Phone calls and sms from php

Goodbye.<?php header('Content-Type: text/xml'); ?> <?xml version="1.0" encoding="UTF-8"?> <Response> <Say> Thanks for calling my demo IVR. Please listen closely as this is the first time you've heard this menu. </Say> <Gather numDigits="1" action="/twiml/process_ivr_menu.php"> <Say> Press 1 to hear a random sound. Press 2 to connect to a conference call. Press 3 to hear some wisdom. Press 4 to be connected to support. </Say> </Gather> <Say> I don't understand. Please try again later. </Say> <Hangup/></Response>

Page 58: Phone calls and sms from php

Now What?

• What if they press something that's not 1-4?

• What if they press 1-4?

• Need to figure out what to do next

Page 59: Phone calls and sms from php

Handle ! 1-4<?phprequire_once __DIR__ . '/../vendor/autoload.php';session_start();$digits = $_POST['Digits'] ?? 0; switch ($digits) { case 1: // Random sound case 2: // Conference call case 3: // Random Phrase case 4: // Support queue default: if (countFailures() >= 3) { echo bailOnCall(); return; } echo badInput(); return;}

Page 60: Phone calls and sms from php

countFailures()

function countFailures($count = null){ $badInput = $count ?? $_SESSION[__FUNCTION__] ?? 0; $badInput++; $_SESSION[__FUNCTION__] = $badInput; return $badInput;}

Page 61: Phone calls and sms from php

badInput()

function badInput(){ header('Content-type: text/xml'); $twiml = new \Twilio\Twiml(); $twiml->say('That was not a valid selection.'); $twiml->redirect('/twiml/ivr_menu.php'); return $twiml;}

Page 62: Phone calls and sms from php

bailOnCall()function bailOnCall(){ header('Content-type: text/xml'); $twiml = new \Twilio\Twiml(); $twiml->say('Your rotary phone is not compatible with this app.'); $twiml->say('Goodbye.'); $twiml->hangup(); return $twiml;}

Page 63: Phone calls and sms from php

Call Handlerswitch ($digits) { case 1: // Random sound echo randomSound(); break; case 2: // Conference call echo conferenceCall(); break; case 3: // Random Phrase echo fortune(); break; case 4: // Support queue echo supportQueue(); break; default: ... }

Page 64: Phone calls and sms from php

randomSound()function randomSound(){ header('Content-type: text/xml'); countFailures(-1); $sounds = ['gull.mp3', 'lawnmower.mp3', 'monkey.mp3']; $index = array_rand($sounds); $twiml = new \Twilio\Twiml(); $twiml->play('/sounds/' . $sounds[$index]); $twiml->redirect('/twiml/ivr_menu.php'); return $twiml;}

Page 65: Phone calls and sms from php

Conference Call

function conferenceCall(){ header('Content-type: text/xml'); $response = new \Twilio\Twiml(); $response->say('You have joined the conference.'); $dial = $response->dial(); $dial->conference('PHP World'); return $response;}

Page 66: Phone calls and sms from php

fortune()

function fortune(){ $fortune = `fortune -s`; header('Content-type: text/xml'); $response = new \Twilio\Twiml(); $response->sms($fortune); $response->say($fortune); return $response;}

Page 67: Phone calls and sms from php

supportQueue()function supportQueue() { header('Content-type: text/xml'); $response = new \Twilio\Twiml(); if ($_POST['From'] == '<my number>') { // Support agent $dial = $response->dial(); $dial->queue('support', ['url' => '/twiml/about_to_connect.php']); } else { // Enqueue $response->enqueue('support'); } return $response;}

Page 68: Phone calls and sms from php

supportQueue()function supportQueue() { header('Content-type: text/xml'); $response = new \Twilio\Twiml(); if ($_POST['From'] == '<my number>') { // Support agent $dial = $response->dial(); $dial->queue('support', ['url' => '/twiml/about_to_connect.php']); } else { // Enqueue $response->enqueue('support'); } return $response;}

Page 69: Phone calls and sms from php

Just Scratching the Surface

• Twilio can keep our application informed about how calls are going

• We can interact with ongoing calls via the REST API

Page 70: Phone calls and sms from php

Questions?

• Twitter: @dstockto

• Joind.in - https://joind.in/talk/e2e2b

Page 71: Phone calls and sms from php

20162016