Twilio Voicemail With Webscript
One very useful Twilio feature is the ability to record a phone message. Today, I will demonstrate just how easy it is to record a phone message and retrieve it by orchestrating Twilio actions with a couple of simple scripts hosted on Webscript.
Twilio's TwiML control language includes an instruction to record a message. The instruction specifies the webhook URL for handling the message after it is recorded. Of course, I'll use Webscript to handle that event.
To record a voicemail message, I will first create the script that will handle the Twilio event that is generated when Twilio answers the call. (This concept was covered in a previous post.) That script will deliver the TwiML that instructs Twilio to synthesize request to leave a message, record that message, and ultimately call another script to handle that message:
twiliodemo.webscript.io/voicemail
return
[[<?xml version="1.0"?>
<Response>
<Say>Please leave a short message at the tone.</Say>
<Record
action="https://twiliodemo.webscript.io/voicemail/receive"
timeout="10"
maxLength="60"/>
</Response>]], {['Content-Type'] = 'text/xml'}
The script that handles the receipt of the message will simply send me an email using Webscript's built-in "alert" routine:
twiliodemo.webscript.io/voicemail/receive
local notice = string.format('Voicemail from %s: %s',
request.form.From,
request.form.RecordingUrl)
alert.email(notice)
Notice that the recording is stored by Twilio and the URL of that recording is delivered to the script as a parameter ("RecordingURL"). This script uses script alerts to send email to the script's owner.
(You can try this by calling 714-798-2663 and leaving a message.)
That's all for today. Today's demonstration showed that Twilio's services and a couple of simple scripts hosted on Webscript create a straightforward voicemail system. Soon, we'll use Webscript and Twilio to create a simple conference call.