Hot off the heels of the Callin’ Oates soulful emergency music hotline, developers started creating their own music hotlines powered by Twilio. Local Phoenix band Bears of Manitou developed their own hotline with new songs at 1-866-25-BEARS.
One of our Developer Evangelists, Rob Spectre, ported Callin’ Oates over to Python, though along the way Hall and Oates got a little more punk rock at 676-606-2607. See Rob’s code, environment setup documentation and code comments on his GitHub project.
Below the developers of Callin’ Oates, Michael Selvidge and Reid Butler, share their ‘how to’ and sample code to build your own music hotline in PHP.
Full version originally posted on Reid’s new (Twilio-unaffiliated) blog Exploring Twilio.
Callin’ Oates: How We Did It
We knew that we needed what’s called an “IVR” (Intearactive Voice Response) so we started by reading up on the IVR the Basics from Twilio’s How To Guides. We then realized we would need to split the application into two parts. If you read over the IVR the Basics post from Twilio, you will see how close the Callin’ Oates code below actually is.
The first is the file used to greet the caller and present the audio options. After a little trial and error, here is the properly formatted file that we created (based on TwiML):
1 2 3 4 5 6 7 8 9 10 11 12 13 14
<?xml version=”1.0″ encoding=”UTF-8″?>
<Gather action=”handle-user-input.php” numDigits=”1″>
<Say voice=”woman” language=”en”>This is your welcome message to the users</Say>
<Say voice=”woman” language=”en”>To hear the first option, please press 1.</Say>
<Say voice=”woman” language=”en”>To hear the second option, please press 2.</Say>
<Say voice=”woman” language=”en”>To hear the third option, please press 3.</Say>
<Say voice=”woman” language=”en”>To hear the fourth option, please press 4.</Say>
<!– If user doesn’t input anything, prompt and try again. –>
<Say>Sorry, I didn’t get your response.</Say>
As seen in that file, Twilio’s basic language makes it simple to create the voice menu for the users. The key to that file is telling the app what to do with the response once the user presses a key on their phone. We need to tell the app where to send the user-entered data and what to do with it, so we created handle-user-input.php.
Next Step, use <Play>
Next, we turn our focus to the handle-user-input.php. This php script would be the core of our app and respond to the digit pressed by the user and execute accordingly. Replace the bits below to reference the full URL of your own personal audio files that you want to have played based on the user’s selection. We did this with Hall and Oates songs–and they turned out to be really cool about it. No guarantees if you want to make a Metallica phone line (*cough* Napster).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
echo ‘<?xml version=”1.0″ encoding=”UTF-8″?>’;
//Create variable with user input.
$user_pushed = (int) $_REQUEST['Digits'];
if ($user_pushed == 1)
echo ‘<Play>URL OF YOUR FIRST FILE TO PLAY</Play>’;
elseif ($user_pushed == 2)
echo ‘<Play>URL OF YOUR SECOND FILE TO PLAY</Play>’;
elseif ($user_pushed ==3)
echo ‘<Play>URL OF YOUR THRD FILE TO PLAY</Play>’;
elseif ($user_pushed ==4)
echo ‘<Play>URL OF YOUR FORTH FILE TO PLAY</Play>’;
echo “<Say>Sorry, I can’t do that yet.</Say>”;
Once executed, the code above will create a properly formatted response for the Twilio server to process.
With these two files, you can easily create an interactive voice system to do any of the following:
- Play a song/file based on user selection (as seen above)
- Change the <Play> verb to <Say> and read a message to the user.
- Add Twilio credentials to the script and change the <Play> verb to <sms> and send a text message.
- Add Twilio credentials to the script and change the <Play> verb to <dial> and call another number.
Reid Butler works in San Francisco at a digital media company as a Sr. Product Development Manager. Leveraging some long forgotten scripting experience from previous gigs, he helped create Callin’ Oates over a weekend with pal Michael Selvidge. Follow him on Twitter at @rbutlersf. Michael Selvidge is the Sr. Corporate Communication Manager at Twilio. Follow him on Twitter at @selviano