IVR: Screening & Recording with PHP and Laravel

ET Phone Home 

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

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.

Read how Hulu built an IVR on Twilio that lets customers waiting on hold schedule a call back.

Route the call to an agent

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.

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.

Loading Code Samples...
Language
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Agent;
use Twilio\Twiml;

class ExtensionController extends Controller
{
    /**
     * Responds with a <Dial> to the caller's planet
     *
     * @return \Illuminate\Http\Response
     */
    public function showExtensionConnection(Request $request)
    {
        $selectedOption = $request->input('Digits');

        try {
            $agent = $this->_getAgentForDigit($selectedOption);
        }
        catch (ModelNotFoundException $e){
            return redirect()->route('main-menu-redirect');
        }

        $numberToDial = $agent->phone_number;

        $response = new Twiml();
        $response->say(
            "You'll be connected shortly to your planet. " .
            $this->_thankYouMessage,
            ['voice' => 'alice', 'language' => 'en-GB']
        );

        $dialCommand = $response->dial(
            ['action' => route('agent-voicemail', ['agent' => $agent->id], false),
             'method' => 'POST']
        );
        $dialCommand->number(
            $numberToDial,
            ['url' => route('screen-call', [], false)]
        );

        return $response;
    }

    private function _getAgentForDigit($digit)
    {
        $planetExtensions = [
            '2' => 'Brodo',
            '3' => 'Dagobah',
            '4' => 'Oober'
        ];
        $planetExtensionExists = isset($planetExtensions[$digit]);

        if ($planetExtensionExists) {
            $agent = Agent::where(
                'extension', '=', $planetExtensions[$digit]
            )->firstOrFail();

            return $agent;
        } else {
            throw new ModelNotFoundException;
        }
    }
}
app/Http/Controllers/ExtensionController.php
Route the call to an agent

app/Http/Controllers/ExtensionController.php

With this information, we present our aliens with a list of available agents so they can pick one. Let's see how to look up an agent.

Look up an agent

When we receive a call from an alien we give them a set of options. In this case the options are:

  • For Brodo, press 2
  • For Dagobah, press 3
  • For Oober, press 4

When our alien caller has made their choice we use the key-press to lookup an Agent.

Loading Code Samples...
Language
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Agent;
use Twilio\Twiml;

class ExtensionController extends Controller
{
    /**
     * Responds with a <Dial> to the caller's planet
     *
     * @return \Illuminate\Http\Response
     */
    public function showExtensionConnection(Request $request)
    {
        $selectedOption = $request->input('Digits');

        try {
            $agent = $this->_getAgentForDigit($selectedOption);
        }
        catch (ModelNotFoundException $e){
            return redirect()->route('main-menu-redirect');
        }

        $numberToDial = $agent->phone_number;

        $response = new Twiml();
        $response->say(
            "You'll be connected shortly to your planet. " .
            $this->_thankYouMessage,
            ['voice' => 'alice', 'language' => 'en-GB']
        );

        $dialCommand = $response->dial(
            ['action' => route('agent-voicemail', ['agent' => $agent->id], false),
             'method' => 'POST']
        );
        $dialCommand->number(
            $numberToDial,
            ['url' => route('screen-call', [], false)]
        );

        return $response;
    }

    private function _getAgentForDigit($digit)
    {
        $planetExtensions = [
            '2' => 'Brodo',
            '3' => 'Dagobah',
            '4' => 'Oober'
        ];
        $planetExtensionExists = isset($planetExtensions[$digit]);

        if ($planetExtensionExists) {
            $agent = Agent::where(
                'extension', '=', $planetExtensions[$digit]
            )->firstOrFail();

            return $agent;
        } else {
            throw new ModelNotFoundException;
        }
    }
}
app/Http/Controllers/ExtensionController.php
Find an agent by extension

app/Http/Controllers/ExtensionController.php

Now that our user has chosen their agent, our next step is to connect the call to that agent.

Call the 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/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.

Loading Code Samples...
Language
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Agent;
use Twilio\Twiml;

class ExtensionController extends Controller
{
    /**
     * Responds with a <Dial> to the caller's planet
     *
     * @return \Illuminate\Http\Response
     */
    public function showExtensionConnection(Request $request)
    {
        $selectedOption = $request->input('Digits');

        try {
            $agent = $this->_getAgentForDigit($selectedOption);
        }
        catch (ModelNotFoundException $e){
            return redirect()->route('main-menu-redirect');
        }

        $numberToDial = $agent->phone_number;

        $response = new Twiml();
        $response->say(
            "You'll be connected shortly to your planet. " .
            $this->_thankYouMessage,
            ['voice' => 'alice', 'language' => 'en-GB']
        );

        $dialCommand = $response->dial(
            ['action' => route('agent-voicemail', ['agent' => $agent->id], false),
             'method' => 'POST']
        );
        $dialCommand->number(
            $numberToDial,
            ['url' => route('screen-call', [], false)]
        );

        return $response;
    }

    private function _getAgentForDigit($digit)
    {
        $planetExtensions = [
            '2' => 'Brodo',
            '3' => 'Dagobah',
            '4' => 'Oober'
        ];
        $planetExtensionExists = isset($planetExtensions[$digit]);

        if ($planetExtensionExists) {
            $agent = Agent::where(
                'extension', '=', $planetExtensions[$digit]
            )->firstOrFail();

            return $agent;
        } else {
            throw new ModelNotFoundException;
        }
    }
}
app/Http/Controllers/ExtensionController.php
Connect the call to an Agent

app/Http/Controllers/ExtensionController.php

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.

The agent screens the 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 connect-message route. This will <Say> a quick message and continue with the original <Dial> command to connect the two parties.

Loading Code Samples...
Language
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Twilio\Twiml;

class AgentCallController extends Controller
{
    /**
     * Handles the callback after a call has finished
     *
     * @return \Illuminate\Http\Response
     */
    public function agentVoicemail(Request $request, $agentId)
    {
        $response = new Twiml();
        $callStatus = $request->input('DialCallStatus');

        if ($callStatus !== 'completed') {
            $response->say(
                'It appears that no agent is available. ' .
                'Please leave a message after the beep',
                ['voice' => 'alice', 'language' => 'en-GB']
            );

            $response->record(
                ['maxLength' => '20',
                 'method' => 'GET',
                 'action' => route('hangup', [], false),
                 'transcribeCallback' => route(
                     'store-recording', ['agent' => $agentId], false
                 )
                ]
            );

            $response->say(
                'No recording received. Goodbye',
                ['voice' => 'alice', 'language' => 'en-GB']
            );
            $response->hangup();

            return $response;
        }

        return "Ok";
    }

    /**
     * Replies with a hangup
     *
     * @return \Illuminate\Http\Response
     */
    public function showHangup()
    {
        $response = new Twiml();
        $response->say(
            'Thanks for your message. Goodbye',
            ['voice' => 'alice', 'language' => 'en-GB']
        );
        $response->hangup();

        return $response;
    }

    /**
     * Handles the callback from screening a call
     *
     * @return \Illuminate\Http\Response
     */
    public function screenCall(Request $request)
    {
        $customerPhoneNumber = $request->input('From');
        $spelledPhoneNumber = join(',', str_split($customerPhoneNumber));

        $response = new Twiml();
        $gather = $response->gather(
            ['numDigits' => 1,
             'action' => route('connect-message', [], false),
             'method' => 'GET'
            ]
        );
        $gather->say('You have an incoming call from: ');
        $gather->say($spelledPhoneNumber);
        $gather->say('Press any key to accept');

        $response->say('Sorry. Did not get your response');
        $response->hangup();

        return $response;
    }

    /**
     * Says message during connection between agent and customer (ET)
     *
     * @return \Illuminate\Http\Response
     */
    public function showConnectMessage(Request $request)
    {
        $response = new Twiml();
        $response->say('Connecting you to the extraterrestrial in distress');

        return $response;
    }
}
app/Http/Controllers/AgentCallController.php
Handle agent screening of incoming calls

app/Http/Controllers/AgentCallController.php

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.

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 hang up. 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 recording have finished. The route will say "Goodbye" and then <Hangup>.

Loading Code Samples...
Language
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Twilio\Twiml;

class AgentCallController extends Controller
{
    /**
     * Handles the callback after a call has finished
     *
     * @return \Illuminate\Http\Response
     */
    public function agentVoicemail(Request $request, $agentId)
    {
        $response = new Twiml();
        $callStatus = $request->input('DialCallStatus');

        if ($callStatus !== 'completed') {
            $response->say(
                'It appears that no agent is available. ' .
                'Please leave a message after the beep',
                ['voice' => 'alice', 'language' => 'en-GB']
            );

            $response->record(
                ['maxLength' => '20',
                 'method' => 'GET',
                 'action' => route('hangup', [], false),
                 'transcribeCallback' => route(
                     'store-recording', ['agent' => $agentId], false
                 )
                ]
            );

            $response->say(
                'No recording received. Goodbye',
                ['voice' => 'alice', 'language' => 'en-GB']
            );
            $response->hangup();

            return $response;
        }

        return "Ok";
    }

    /**
     * Replies with a hangup
     *
     * @return \Illuminate\Http\Response
     */
    public function showHangup()
    {
        $response = new Twiml();
        $response->say(
            'Thanks for your message. Goodbye',
            ['voice' => 'alice', 'language' => 'en-GB']
        );
        $response->hangup();

        return $response;
    }

    /**
     * Handles the callback from screening a call
     *
     * @return \Illuminate\Http\Response
     */
    public function screenCall(Request $request)
    {
        $customerPhoneNumber = $request->input('From');
        $spelledPhoneNumber = join(',', str_split($customerPhoneNumber));

        $response = new Twiml();
        $gather = $response->gather(
            ['numDigits' => 1,
             'action' => route('connect-message', [], false),
             'method' => 'GET'
            ]
        );
        $gather->say('You have an incoming call from: ');
        $gather->say($spelledPhoneNumber);
        $gather->say('Press any key to accept');

        $response->say('Sorry. Did not get your response');
        $response->hangup();

        return $response;
    }

    /**
     * Says message during connection between agent and customer (ET)
     *
     * @return \Illuminate\Http\Response
     */
    public function showConnectMessage(Request $request)
    {
        $response = new Twiml();
        $response->say('Connecting you to the extraterrestrial in distress');

        return $response;
    }
}
app/Http/Controllers/AgentCallController.php
Redirect a call to voicemail and end the call

app/Http/Controllers/AgentCallController.php

Now let's take a step back to see how to actually record the call.

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 to send the transcription to the agent when it's complete.

Notice 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.

Loading Code Samples...
Language
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Twilio\Twiml;

class AgentCallController extends Controller
{
    /**
     * Handles the callback after a call has finished
     *
     * @return \Illuminate\Http\Response
     */
    public function agentVoicemail(Request $request, $agentId)
    {
        $response = new Twiml();
        $callStatus = $request->input('DialCallStatus');

        if ($callStatus !== 'completed') {
            $response->say(
                'It appears that no agent is available. ' .
                'Please leave a message after the beep',
                ['voice' => 'alice', 'language' => 'en-GB']
            );

            $response->record(
                ['maxLength' => '20',
                 'method' => 'GET',
                 'action' => route('hangup', [], false),
                 'transcribeCallback' => route(
                     'store-recording', ['agent' => $agentId], false
                 )
                ]
            );

            $response->say(
                'No recording received. Goodbye',
                ['voice' => 'alice', 'language' => 'en-GB']
            );
            $response->hangup();

            return $response;
        }

        return "Ok";
    }

    /**
     * Replies with a hangup
     *
     * @return \Illuminate\Http\Response
     */
    public function showHangup()
    {
        $response = new Twiml();
        $response->say(
            'Thanks for your message. Goodbye',
            ['voice' => 'alice', 'language' => 'en-GB']
        );
        $response->hangup();

        return $response;
    }

    /**
     * Handles the callback from screening a call
     *
     * @return \Illuminate\Http\Response
     */
    public function screenCall(Request $request)
    {
        $customerPhoneNumber = $request->input('From');
        $spelledPhoneNumber = join(',', str_split($customerPhoneNumber));

        $response = new Twiml();
        $gather = $response->gather(
            ['numDigits' => 1,
             'action' => route('connect-message', [], false),
             'method' => 'GET'
            ]
        );
        $gather->say('You have an incoming call from: ');
        $gather->say($spelledPhoneNumber);
        $gather->say('Press any key to accept');

        $response->say('Sorry. Did not get your response');
        $response->hangup();

        return $response;
    }

    /**
     * Says message during connection between agent and customer (ET)
     *
     * @return \Illuminate\Http\Response
     */
    public function showConnectMessage(Request $request)
    {
        $response = new Twiml();
        $response->say('Connecting you to the extraterrestrial in distress');

        return $response;
    }
}
app/Http/Controllers/AgentCallController.php
Record a call

app/Http/Controllers/AgentCallController.php

Finally, we will see how to view an agent's voicemail.

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 View.

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

Loading Code Samples...
Language
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Recording;
use App\Agent;

class RecordingController extends Controller
{

    /**
     * Shows an index of existings recordings
     *
     * @return \Illuminate\Http\Response
     */
    public function indexByAgent(Request $request)
    {
        $agentNumber = $request->input('agentNumber');
        $agentNumberInE164Format = '+' . $agentNumber;

        $agent = Agent::where(['phone_number' => $agentNumberInE164Format])
               ->firstOrFail();
        $allRecordings = Recording::where(['agent_id' => $agent->id])->get();

        return response()->view(
            'recordings.index',
            ['recordings' => $allRecordings,
             'agent' => $agent]
        );
    }

    /**
     * Store a new recording from callback
     *
     * @return \Illuminate\Http\Response
     */
    public function storeRecording(Request $request, $agentId)
    {
        $newRecording = new Recording(
            ['caller_number' => $request->input('Caller'),
             'transcription' => $request->input('TranscriptionText'),
             'recording_url' => $request->input('RecordingUrl'),
             'agent_id'      => $agentId]
        );

        $newRecording->save();

        return "Recording saved";
    }
}
app/Http/Controllers/RecordingController.php
View an agent's voicemail recordings

app/Http/Controllers/RecordingController.php

That's it! We've just implemented an IVR with real Agents, call screening and voicemail.

Where to next?

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?

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!

Mario Celi
Kat King
Andrew Baker
Agustin Camino

Need some help?

We all do sometimes; code is hard. Get help now from our support team, or lean on the wisdom of the crowd browsing the Twilio tag on Stack Overflow.

1 / 1
Loading Code Samples...
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Agent;
use Twilio\Twiml;

class ExtensionController extends Controller
{
    /**
     * Responds with a <Dial> to the caller's planet
     *
     * @return \Illuminate\Http\Response
     */
    public function showExtensionConnection(Request $request)
    {
        $selectedOption = $request->input('Digits');

        try {
            $agent = $this->_getAgentForDigit($selectedOption);
        }
        catch (ModelNotFoundException $e){
            return redirect()->route('main-menu-redirect');
        }

        $numberToDial = $agent->phone_number;

        $response = new Twiml();
        $response->say(
            "You'll be connected shortly to your planet. " .
            $this->_thankYouMessage,
            ['voice' => 'alice', 'language' => 'en-GB']
        );

        $dialCommand = $response->dial(
            ['action' => route('agent-voicemail', ['agent' => $agent->id], false),
             'method' => 'POST']
        );
        $dialCommand->number(
            $numberToDial,
            ['url' => route('screen-call', [], false)]
        );

        return $response;
    }

    private function _getAgentForDigit($digit)
    {
        $planetExtensions = [
            '2' => 'Brodo',
            '3' => 'Dagobah',
            '4' => 'Oober'
        ];
        $planetExtensionExists = isset($planetExtensions[$digit]);

        if ($planetExtensionExists) {
            $agent = Agent::where(
                'extension', '=', $planetExtensions[$digit]
            )->firstOrFail();

            return $agent;
        } else {
            throw new ModelNotFoundException;
        }
    }
}
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Agent;
use Twilio\Twiml;

class ExtensionController extends Controller
{
    /**
     * Responds with a <Dial> to the caller's planet
     *
     * @return \Illuminate\Http\Response
     */
    public function showExtensionConnection(Request $request)
    {
        $selectedOption = $request->input('Digits');

        try {
            $agent = $this->_getAgentForDigit($selectedOption);
        }
        catch (ModelNotFoundException $e){
            return redirect()->route('main-menu-redirect');
        }

        $numberToDial = $agent->phone_number;

        $response = new Twiml();
        $response->say(
            "You'll be connected shortly to your planet. " .
            $this->_thankYouMessage,
            ['voice' => 'alice', 'language' => 'en-GB']
        );

        $dialCommand = $response->dial(
            ['action' => route('agent-voicemail', ['agent' => $agent->id], false),
             'method' => 'POST']
        );
        $dialCommand->number(
            $numberToDial,
            ['url' => route('screen-call', [], false)]
        );

        return $response;
    }

    private function _getAgentForDigit($digit)
    {
        $planetExtensions = [
            '2' => 'Brodo',
            '3' => 'Dagobah',
            '4' => 'Oober'
        ];
        $planetExtensionExists = isset($planetExtensions[$digit]);

        if ($planetExtensionExists) {
            $agent = Agent::where(
                'extension', '=', $planetExtensions[$digit]
            )->firstOrFail();

            return $agent;
        } else {
            throw new ModelNotFoundException;
        }
    }
}
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Agent;
use Twilio\Twiml;

class ExtensionController extends Controller
{
    /**
     * Responds with a <Dial> to the caller's planet
     *
     * @return \Illuminate\Http\Response
     */
    public function showExtensionConnection(Request $request)
    {
        $selectedOption = $request->input('Digits');

        try {
            $agent = $this->_getAgentForDigit($selectedOption);
        }
        catch (ModelNotFoundException $e){
            return redirect()->route('main-menu-redirect');
        }

        $numberToDial = $agent->phone_number;

        $response = new Twiml();
        $response->say(
            "You'll be connected shortly to your planet. " .
            $this->_thankYouMessage,
            ['voice' => 'alice', 'language' => 'en-GB']
        );

        $dialCommand = $response->dial(
            ['action' => route('agent-voicemail', ['agent' => $agent->id], false),
             'method' => 'POST']
        );
        $dialCommand->number(
            $numberToDial,
            ['url' => route('screen-call', [], false)]
        );

        return $response;
    }

    private function _getAgentForDigit($digit)
    {
        $planetExtensions = [
            '2' => 'Brodo',
            '3' => 'Dagobah',
            '4' => 'Oober'
        ];
        $planetExtensionExists = isset($planetExtensions[$digit]);

        if ($planetExtensionExists) {
            $agent = Agent::where(
                'extension', '=', $planetExtensions[$digit]
            )->firstOrFail();

            return $agent;
        } else {
            throw new ModelNotFoundException;
        }
    }
}
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Twilio\Twiml;

class AgentCallController extends Controller
{
    /**
     * Handles the callback after a call has finished
     *
     * @return \Illuminate\Http\Response
     */
    public function agentVoicemail(Request $request, $agentId)
    {
        $response = new Twiml();
        $callStatus = $request->input('DialCallStatus');

        if ($callStatus !== 'completed') {
            $response->say(
                'It appears that no agent is available. ' .
                'Please leave a message after the beep',
                ['voice' => 'alice', 'language' => 'en-GB']
            );

            $response->record(
                ['maxLength' => '20',
                 'method' => 'GET',
                 'action' => route('hangup', [], false),
                 'transcribeCallback' => route(
                     'store-recording', ['agent' => $agentId], false
                 )
                ]
            );

            $response->say(
                'No recording received. Goodbye',
                ['voice' => 'alice', 'language' => 'en-GB']
            );
            $response->hangup();

            return $response;
        }

        return "Ok";
    }

    /**
     * Replies with a hangup
     *
     * @return \Illuminate\Http\Response
     */
    public function showHangup()
    {
        $response = new Twiml();
        $response->say(
            'Thanks for your message. Goodbye',
            ['voice' => 'alice', 'language' => 'en-GB']
        );
        $response->hangup();

        return $response;
    }

    /**
     * Handles the callback from screening a call
     *
     * @return \Illuminate\Http\Response
     */
    public function screenCall(Request $request)
    {
        $customerPhoneNumber = $request->input('From');
        $spelledPhoneNumber = join(',', str_split($customerPhoneNumber));

        $response = new Twiml();
        $gather = $response->gather(
            ['numDigits' => 1,
             'action' => route('connect-message', [], false),
             'method' => 'GET'
            ]
        );
        $gather->say('You have an incoming call from: ');
        $gather->say($spelledPhoneNumber);
        $gather->say('Press any key to accept');

        $response->say('Sorry. Did not get your response');
        $response->hangup();

        return $response;
    }

    /**
     * Says message during connection between agent and customer (ET)
     *
     * @return \Illuminate\Http\Response
     */
    public function showConnectMessage(Request $request)
    {
        $response = new Twiml();
        $response->say('Connecting you to the extraterrestrial in distress');

        return $response;
    }
}
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Twilio\Twiml;

class AgentCallController extends Controller
{
    /**
     * Handles the callback after a call has finished
     *
     * @return \Illuminate\Http\Response
     */
    public function agentVoicemail(Request $request, $agentId)
    {
        $response = new Twiml();
        $callStatus = $request->input('DialCallStatus');

        if ($callStatus !== 'completed') {
            $response->say(
                'It appears that no agent is available. ' .
                'Please leave a message after the beep',
                ['voice' => 'alice', 'language' => 'en-GB']
            );

            $response->record(
                ['maxLength' => '20',
                 'method' => 'GET',
                 'action' => route('hangup', [], false),
                 'transcribeCallback' => route(
                     'store-recording', ['agent' => $agentId], false
                 )
                ]
            );

            $response->say(
                'No recording received. Goodbye',
                ['voice' => 'alice', 'language' => 'en-GB']
            );
            $response->hangup();

            return $response;
        }

        return "Ok";
    }

    /**
     * Replies with a hangup
     *
     * @return \Illuminate\Http\Response
     */
    public function showHangup()
    {
        $response = new Twiml();
        $response->say(
            'Thanks for your message. Goodbye',
            ['voice' => 'alice', 'language' => 'en-GB']
        );
        $response->hangup();

        return $response;
    }

    /**
     * Handles the callback from screening a call
     *
     * @return \Illuminate\Http\Response
     */
    public function screenCall(Request $request)
    {
        $customerPhoneNumber = $request->input('From');
        $spelledPhoneNumber = join(',', str_split($customerPhoneNumber));

        $response = new Twiml();
        $gather = $response->gather(
            ['numDigits' => 1,
             'action' => route('connect-message', [], false),
             'method' => 'GET'
            ]
        );
        $gather->say('You have an incoming call from: ');
        $gather->say($spelledPhoneNumber);
        $gather->say('Press any key to accept');

        $response->say('Sorry. Did not get your response');
        $response->hangup();

        return $response;
    }

    /**
     * Says message during connection between agent and customer (ET)
     *
     * @return \Illuminate\Http\Response
     */
    public function showConnectMessage(Request $request)
    {
        $response = new Twiml();
        $response->say('Connecting you to the extraterrestrial in distress');

        return $response;
    }
}
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Twilio\Twiml;

class AgentCallController extends Controller
{
    /**
     * Handles the callback after a call has finished
     *
     * @return \Illuminate\Http\Response
     */
    public function agentVoicemail(Request $request, $agentId)
    {
        $response = new Twiml();
        $callStatus = $request->input('DialCallStatus');

        if ($callStatus !== 'completed') {
            $response->say(
                'It appears that no agent is available. ' .
                'Please leave a message after the beep',
                ['voice' => 'alice', 'language' => 'en-GB']
            );

            $response->record(
                ['maxLength' => '20',
                 'method' => 'GET',
                 'action' => route('hangup', [], false),
                 'transcribeCallback' => route(
                     'store-recording', ['agent' => $agentId], false
                 )
                ]
            );

            $response->say(
                'No recording received. Goodbye',
                ['voice' => 'alice', 'language' => 'en-GB']
            );
            $response->hangup();

            return $response;
        }

        return "Ok";
    }

    /**
     * Replies with a hangup
     *
     * @return \Illuminate\Http\Response
     */
    public function showHangup()
    {
        $response = new Twiml();
        $response->say(
            'Thanks for your message. Goodbye',
            ['voice' => 'alice', 'language' => 'en-GB']
        );
        $response->hangup();

        return $response;
    }

    /**
     * Handles the callback from screening a call
     *
     * @return \Illuminate\Http\Response
     */
    public function screenCall(Request $request)
    {
        $customerPhoneNumber = $request->input('From');
        $spelledPhoneNumber = join(',', str_split($customerPhoneNumber));

        $response = new Twiml();
        $gather = $response->gather(
            ['numDigits' => 1,
             'action' => route('connect-message', [], false),
             'method' => 'GET'
            ]
        );
        $gather->say('You have an incoming call from: ');
        $gather->say($spelledPhoneNumber);
        $gather->say('Press any key to accept');

        $response->say('Sorry. Did not get your response');
        $response->hangup();

        return $response;
    }

    /**
     * Says message during connection between agent and customer (ET)
     *
     * @return \Illuminate\Http\Response
     */
    public function showConnectMessage(Request $request)
    {
        $response = new Twiml();
        $response->say('Connecting you to the extraterrestrial in distress');

        return $response;
    }
}
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Recording;
use App\Agent;

class RecordingController extends Controller
{

    /**
     * Shows an index of existings recordings
     *
     * @return \Illuminate\Http\Response
     */
    public function indexByAgent(Request $request)
    {
        $agentNumber = $request->input('agentNumber');
        $agentNumberInE164Format = '+' . $agentNumber;

        $agent = Agent::where(['phone_number' => $agentNumberInE164Format])
               ->firstOrFail();
        $allRecordings = Recording::where(['agent_id' => $agent->id])->get();

        return response()->view(
            'recordings.index',
            ['recordings' => $allRecordings,
             'agent' => $agent]
        );
    }

    /**
     * Store a new recording from callback
     *
     * @return \Illuminate\Http\Response
     */
    public function storeRecording(Request $request, $agentId)
    {
        $newRecording = new Recording(
            ['caller_number' => $request->input('Caller'),
             'transcription' => $request->input('TranscriptionText'),
             'recording_url' => $request->input('RecordingUrl'),
             'agent_id'      => $agentId]
        );

        $newRecording->save();

        return "Recording saved";
    }
}