IVR: Call screening and Voicemail

IVRs (interactive voice response) are automated phone systems that can facilitate communication between callers and businesses. In this tutorial you will learn how to screen and send callers to voicemail if an agent is busy.

Start Tutorial

About this application

Extra Terrestrial Phone Home

This Laravel sample application is modeled after a typical call center experience, but with more Reese's Pieces.

Stranded aliens can call a phone number and receive instructions on how to get out of earth safely, or call their home planet directly. 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.

Let's get started! Click the right arrow above to move to the next step of the tutorial.

See Also:

Route the call to an agent

When our caller chooses a planet we need to figure out where to route the call. Depending on her input we will route this call to an extension. In this case an extension will be used to look up an Agent, so any string can be used to define an extension.

Once we look up the Agent, we can Dial the agent's phone number and try to connect the call. Let's look at those steps next.

See Also:

Looking up an Agent

In this case the Agent model is very simple. This is the migration used to create it:

    'agents', function (Blueprint $table) {

We have a handle or extension that we can use to look up an Agent, and we have a phone_number that we will call.

See Also:

Call the Agent

This code begins the process of transferring the call to our agent.

By passing a url to the Dial > Number TwiML tag, we are telling Twilio to make a POST request to the screen-call 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. Let's look at that route next.

See Also:

The Agent screens the call

When our agent picks up their 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 connect-message route (mapped to showConnectMessage, below) which will <Say> a quick message, then continue with the original <Dial> command and connect the two parties.

If the agent instead does not respond, the processing of the caller's <Dial> verb will end and Twilio will look to the URL we provided for more instructions - in this case, the agentVoicemail route.

See Also:

Send the Caller to Voicemail

When Twilio makes a request to our voicemail controller, it will pass a DialCallStatus which will tell us if the call was successful. If it was "completed" we hangup — otherwise we need to <Say> a quick prompt and then <Record> a voicemail from the caller.

We also specify an action for <Record>. This route will be called after the call (and hence the recording) has finished. The route will say "Goodbye" and then Hangup.

See Also:

Record the Caller

When we tell Twilio to Record we have a few options we can pass to the Record verb.

Here we instruct Record to stop the recording at 20 seconds, to transcribe the call, and where to send the transcription when it's complete. Adding the transcribeCallback attribute to <Record> implies 'transcribe' => true.

Notice we redirect to a URL that is specific to an agent. This is a convenient way to specify which agent was called to produce this voice message. This way we can also save the associated agent together with the voicemail.

See Also:

View an Agent's Voicemails

Once we look up the Agent, all we need to do is display all of her recordings. We simply bind the recordings to a view that is then rendered.

It is possible to look up recordings via the Twilio REST API, but since we have all the data we need in the transcribeCallback request, we can easily store it ourselves and save a roundtrip.

See Also:

Where to next?

That's it! We've just implemented an IVR with real Agents, call screening and voicemail. If you're a PHP developer working with Twilio, you might want to check out these other tutorials.

Part 1 of this Tutorial: ET Phone Home Service - IVR Phone Trees

Increase your rate of response by automating the workflows that are key to your business.

Appointment Reminders

Send your customers a text message when they have an upcoming appointment - this tutorial shows you how to do it from a background job.

Did this help?

As always, we'd love to hear what you thought of this tutorial! Shoot us a note to let us know what you liked and what we can do better. See you next time!