How let your PHP application interact with phones through incoming and outgoing SMS and voice calls. This talk uses the Twilio platform and API to show how to send and receive text and voice calls to allow for different interactive options for you and your applications.
34. 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
35. 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
36. How Outgoing Calls Work
Your App TwilioAPI Call
TwiML
CalleeTwilio Plays Twiml
37. 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>
38. 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>
39. 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>
40. 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>
41. <?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
43. 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>
44. 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>
51. 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
52. Phone IVR
• Give callers a menu
• Look up information
• Connect to people
• Solve problems
• Give answers
53. 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>
54. <?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
55. <?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
56. 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>
57. 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>
58. 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
59. Handle ! 1-4
<?php
require_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;
}
69. 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