Stranded aliens can call an agent and receive instructions on how to get off of Earth safely. In this tutorial, we'll show you the key bits of code that allow an agent to send a caller to voicemail, and later read transcripts and listen to voicemails.
To run this sample app yourself, download the code and follow the instructions on GitHub.
When our alien caller chooses a planet, we need to figure out where to route the call. Depending on their input, we will route this call to an extension. Extensions are used to look up an agent. Any string can be used to define an extension.
When our alien caller has made their choice we use the key-press to look up the requested
Once we look up the agent, we can use the <Dial> verb to dial the agent's phone number and try to connect the call.
Now that our user has chosen their agent, our next step is to connect the call to that agent.
This code begins the process of transferring the call to our agent.
By passing a
url to the
<Number> noun, we are telling Twilio to make a POST request to the
agent/screencall route after the agent has picked up but before connecting the two parties.
Essentially, we are telling Twilio to execute some TwiML that only the agent will hear.
Our agent can now be called, but how does our agent interact with this feature? Let's dig into what is happening in the agent's screening call.
When our agent picks up the phone, we use a
<Gather> verb to ask them if they want to accept the call.
If the agent responds by entering any digit, the response will be processed by our
route. This will
<Say> a quick message and continue with the original
<Dial> command to connect the two parties.
Now our agent can interact with the call, but what if our agent is currently out? In these cases it's helpful to have voicemail set up.
When Twilio makes a request to our
Call action method, it will pass a
DialCallStatus argument to tell us the call status. If the status was
"completed", we hang up. Otherwise, we need to
<Say> a quick prompt and then
<Record> a voicemail from the alien caller.
Now let's take a step back to see how to actually record the call.
When we tell Twilio to record, we have a few options we can pass to the
Here we instruct
<Record> to stop the recording at 20 seconds, to
transcribe the call, and to send the transcription to the agent when it's complete.
Notice that we redirect to a URL that is specific to this agent. This is a convenient way to specify which agent was called to produce the voice message. This way we can also save the associated agent together with the voicemail.
If you choose to record voice or video calls, you need to comply with certain laws and regulations, including those regarding obtaining consent to record (such as California’s Invasion of Privacy Act and similar laws in other jurisdictions). Additional information on the legal implications of call recording can be found here.
Notice: Twilio recommends that you consult with your legal counsel to make sure that you are complying with all applicable laws in connection with communications you record or store using Twilio.
Finally, we will see how to view an agent's voicemail.
Once we look up the agent, all we need to do is display their recordings. We bind the agent, along with their recordings, to a
It is possible to look up recordings via the Twilio REST API, but since we have all of the data we need in the
transcribe_callback request, we can easily store it ourselves and save a roundtrip.
That's it! We've just implemented an IVR with real Agents, call screening and voicemail.
If you're a Python developer working with Twilio, you might want to check out these other tutorials.
Increase your rate of response by automating the workflows that are key to your business.
Instantly collect structured data from your users with a survey conducted over a voice call or SMS text messages.
Thanks for checking out this tutorial! If you have any feedback to share with us, we'd love to hear it. Connect with us on Twitter and let us know what you build!