IVRs (interactive voice response) are automated phone systems that can facilitate communication between callers and businesses. If you've ever dialed your credit card company to check on a balance after responding to a series of automated prompts, you've used an IVR. Using any web language of your choice, you can easily create powerful IVRs for your business using Twilio's API's.
A Twilio phone number is provisioned and its Voice URL is set to the handle-incoming-call.xml script. When a call is made to that number, Twilio makes a web request to your application's Voice URL for instructions on how to handle the call.
For this tutorial, we'll assume that we're a fictitious shipping company. When a customer dials our Twilio phone number, our IVR will read back a menu of options, gather their input, and return the store hours.
This demo shows how to create a simple phone tree using Twilio's <Say>, <Gather>, and <Dial> verbs.
handle-incoming-call.xml. When someone calls the number, Twilio will request this page with the standard TwiML request parameters.
<?xml version="1.0" encoding="UTF-8"?>
<Response>
<Gather action="handle-user-input.php" numDigits="1">
<Say>Welcome to TPS.</Say>
<Say>For store hours, press 1.</Say>
<Say>To speak to an agent, press 2.</Say>
<Say>To check your package status, press 3.</Say>
</Gather>
<!-- If customer doesn't input anything, prompt and try again. -->
<Say>Sorry, I didn't get your response.</Say>
<Redirect>handle-incoming-call.xml</Redirect>
</Response>
$user_pushed = (int) $_REQUEST['Digits'];
<?php
header('Content-type: text/xml');
echo '<?xml version="1.0" encoding="UTF-8"?>';
echo '<Response>';
# @start snippet
$user_pushed = (int) $_REQUEST['Digits'];
# @end snippet
if ($user_pushed == 1)
{
echo '<Say>Our store hours are 8 AM to 8 PM everyday.</Say>';
}
else {
// We'll implement the rest of the functionality in the
// following sections.
echo "<Say>Sorry, I can't do that yet.</Say>";
echo '<Redirect>handle-incoming-call.php</Redirect>';
}
echo '</Response>';
?>
This is part one of a five part series on building IVRs using Twilio's API. When you're ready continue on to the next section: