This Laravel sample application is modeled after a typical call center experience with an IVR, 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 to make this work.
To run this sample app yourself, download the code and follow the instructions on GitHub.
Read how Livestream used Twilio to build custom call routing logic and IVR messages.
To initiate the phone tree, we need to configure one of our Twilio numbers to send our web application an HTTP request when we get an incoming call.
Click on one of your numbers and configure the Voice URL to point to our app. In our code the route will be /ivr/welcome
.
If you don't already have a server configured to use as your webhook, ngrok is a great tool for testing webhooks locally.
With our Twilio number configured, we are prepared to respond to the Twilio request.
Our Twilio number is now configured to send HTTP requests to this controller on any incoming voice calls. Our app responds with TwiML to tell Twilio what to do in response to the message.
In this case we tell Twilio to Gather
the input from the caller and we Say
a welcome message.
app/Http/Controllers/IvrController.php
1<?php23namespace App\Http\Controllers;45use App\Http\Requests;6use Illuminate\Http\Request;7use Twilio\Twiml;89class IvrController extends Controller10{11public function __construct()12{13$this->_thankYouMessage = 'Thank you for calling the ET Phone Home' .14' Service - the adventurous alien\'s first choice' .15' in intergalactic travel.';1617}1819/**20* Responds with a welcome message with instructions21*22* @return \Illuminate\Http\Response23*/24public function showWelcome()25{26$response = new Twiml();27$gather = $response->gather(28[29'numDigits' => 1,30'action' => route('menu-response', [], false)31]32);3334$gather->say(35'Thanks for calling the E T Phone Home Service.' .36'Please press 1 for directions. Press 2 for a ' .37'list of planets to call.',38['loop' => 3]39);4041return $response;42}4344/**45* Responds to selection of an option by the caller46*47* @return \Illuminate\Http\Response48*/49public function showMenuResponse(Request $request)50{51$selectedOption = $request->input('Digits');5253switch ($selectedOption) {54case 1:55return $this->_getReturnInstructions();56case 2:57return $this->_getPlanetsMenu();58}5960$response = new Twiml();61$response->say(62'Returning to the main menu',63['voice' => 'Polly.Amy', 'language' => 'en-GB']64);65$response->redirect(route('welcome', [], false));6667return $response;68}6970/**71* Responds with a <Dial> to the caller's planet72*73* @return \Illuminate\Http\Response74*/75public function showPlanetConnection(Request $request)76{77$response = new Twiml();78$response->say(79$this->_thankYouMessage,80['voice' => 'Polly.Amy', 'language' => 'en-GB']81);82$response->say(83"You'll be connected shortly to your planet",84['voice' => 'Polly.Amy', 'language' => 'en-GB']85);8687$planetNumbers = [88'2' => '+19295566487',89'3' => '+17262043675',90'4' => '+16513582243'91];92$selectedOption = $request->input('Digits');9394$planetNumberExists = isset($planetNumbers[$selectedOption]);9596if ($planetNumberExists) {97$selectedNumber = $planetNumbers[$selectedOption];98$response->dial($selectedNumber);99100return $response;101} else {102$errorResponse = new Twiml();103$errorResponse->say(104'Returning to the main menu',105['voice' => 'Polly.Amy', 'language' => 'en-GB']106);107$errorResponse->redirect(route('welcome', [], false));108109return $errorResponse;110}111112}113114115/**116* Responds with instructions to mothership117* @return Services_Twilio_Twiml118*/119private function _getReturnInstructions()120{121$response = new Twiml();122$response->say(123'To get to your extraction point, get on your bike and go down the' .124' street. Then Left down an alley. Avoid the police cars. Turn left' .125' into an unfinished housing development. Fly over the roadblock. Go' .126' passed the moon. Soon after you will see your mother ship.',127['voice' => 'Polly.Amy', 'language' => 'en-GB']128);129$response->say(130$this->_thankYouMessage,131['voice' => 'Polly.Amy', 'language' => 'en-GB']132);133134$response->hangup();135136return $response;137}138139/**140* Responds with instructions to choose a planet141* @return Services_Twilio_Twiml142*/143private function _getPlanetsMenu()144{145$response = new Twiml();146$gather = $response->gather(147['numDigits' => '1', 'action' => route('planet-connection', [], false)]148);149$gather->say(150'To call the planet Brodo Asogi, press 2. To call the planet' .151' Dugobah, press 3. To call an Oober asteroid to your location,' .152' press 4. To go back to the main menu, press the star key',153['voice' => 'Polly.Amy', 'language' => 'en-GB']154);155156return $response;157}158}
Let's dig in to what happens with the caller's input next.
The gather's action
parameter takes an absolute or relative URL as a value. In our case, this is the menu-response
route.
When the caller has finished entering digits, Twilio will make a GET
or POST
request to this URL including a Digits
parameter with the number our caller chose.
After making this request, Twilio will continue the current call using the TwiML received in your response. Any TwiML verbs occuring after a <Gather>
are unreachable, unless the caller enters no digits.
app/Http/Controllers/IvrController.php
1<?php23namespace App\Http\Controllers;45use App\Http\Requests;6use Illuminate\Http\Request;7use Twilio\Twiml;89class IvrController extends Controller10{11public function __construct()12{13$this->_thankYouMessage = 'Thank you for calling the ET Phone Home' .14' Service - the adventurous alien\'s first choice' .15' in intergalactic travel.';1617}1819/**20* Responds with a welcome message with instructions21*22* @return \Illuminate\Http\Response23*/24public function showWelcome()25{26$response = new Twiml();27$gather = $response->gather(28[29'numDigits' => 1,30'action' => route('menu-response', [], false)31]32);3334$gather->say(35'Thanks for calling the E T Phone Home Service.' .36'Please press 1 for directions. Press 2 for a ' .37'list of planets to call.',38['loop' => 3]39);4041return $response;42}4344/**45* Responds to selection of an option by the caller46*47* @return \Illuminate\Http\Response48*/49public function showMenuResponse(Request $request)50{51$selectedOption = $request->input('Digits');5253switch ($selectedOption) {54case 1:55return $this->_getReturnInstructions();56case 2:57return $this->_getPlanetsMenu();58}5960$response = new Twiml();61$response->say(62'Returning to the main menu',63['voice' => 'Polly.Amy', 'language' => 'en-GB']64);65$response->redirect(route('welcome', [], false));6667return $response;68}6970/**71* Responds with a <Dial> to the caller's planet72*73* @return \Illuminate\Http\Response74*/75public function showPlanetConnection(Request $request)76{77$response = new Twiml();78$response->say(79$this->_thankYouMessage,80['voice' => 'Polly.Amy', 'language' => 'en-GB']81);82$response->say(83"You'll be connected shortly to your planet",84['voice' => 'Polly.Amy', 'language' => 'en-GB']85);8687$planetNumbers = [88'2' => '+19295566487',89'3' => '+17262043675',90'4' => '+16513582243'91];92$selectedOption = $request->input('Digits');9394$planetNumberExists = isset($planetNumbers[$selectedOption]);9596if ($planetNumberExists) {97$selectedNumber = $planetNumbers[$selectedOption];98$response->dial($selectedNumber);99100return $response;101} else {102$errorResponse = new Twiml();103$errorResponse->say(104'Returning to the main menu',105['voice' => 'Polly.Amy', 'language' => 'en-GB']106);107$errorResponse->redirect(route('welcome', [], false));108109return $errorResponse;110}111112}113114115/**116* Responds with instructions to mothership117* @return Services_Twilio_Twiml118*/119private function _getReturnInstructions()120{121$response = new Twiml();122$response->say(123'To get to your extraction point, get on your bike and go down the' .124' street. Then Left down an alley. Avoid the police cars. Turn left' .125' into an unfinished housing development. Fly over the roadblock. Go' .126' passed the moon. Soon after you will see your mother ship.',127['voice' => 'Polly.Amy', 'language' => 'en-GB']128);129$response->say(130$this->_thankYouMessage,131['voice' => 'Polly.Amy', 'language' => 'en-GB']132);133134$response->hangup();135136return $response;137}138139/**140* Responds with instructions to choose a planet141* @return Services_Twilio_Twiml142*/143private function _getPlanetsMenu()144{145$response = new Twiml();146$gather = $response->gather(147['numDigits' => '1', 'action' => route('planet-connection', [], false)]148);149$gather->say(150'To call the planet Brodo Asogi, press 2. To call the planet' .151' Dugobah, press 3. To call an Oober asteroid to your location,' .152' press 4. To go back to the main menu, press the star key',153['voice' => 'Polly.Amy', 'language' => 'en-GB']154);155156return $response;157}158}
Now that we have told Twilio where to send the caller's input, we can look at how to process that input.
If our callers choose to call their home planet we will give them the planet directory. This is akin to a typical "company directory" feature of most IVRs.
In our TwiML response we again use a Gather
verb to receive our caller's input. The action
verb points this time to the planets
route, which will alter our response based on what the caller chooses.
app/Http/Controllers/IvrController.php
1<?php23namespace App\Http\Controllers;45use App\Http\Requests;6use Illuminate\Http\Request;7use Twilio\Twiml;89class IvrController extends Controller10{11public function __construct()12{13$this->_thankYouMessage = 'Thank you for calling the ET Phone Home' .14' Service - the adventurous alien\'s first choice' .15' in intergalactic travel.';1617}1819/**20* Responds with a welcome message with instructions21*22* @return \Illuminate\Http\Response23*/24public function showWelcome()25{26$response = new Twiml();27$gather = $response->gather(28[29'numDigits' => 1,30'action' => route('menu-response', [], false)31]32);3334$gather->say(35'Thanks for calling the E T Phone Home Service.' .36'Please press 1 for directions. Press 2 for a ' .37'list of planets to call.',38['loop' => 3]39);4041return $response;42}4344/**45* Responds to selection of an option by the caller46*47* @return \Illuminate\Http\Response48*/49public function showMenuResponse(Request $request)50{51$selectedOption = $request->input('Digits');5253switch ($selectedOption) {54case 1:55return $this->_getReturnInstructions();56case 2:57return $this->_getPlanetsMenu();58}5960$response = new Twiml();61$response->say(62'Returning to the main menu',63['voice' => 'Polly.Amy', 'language' => 'en-GB']64);65$response->redirect(route('welcome', [], false));6667return $response;68}6970/**71* Responds with a <Dial> to the caller's planet72*73* @return \Illuminate\Http\Response74*/75public function showPlanetConnection(Request $request)76{77$response = new Twiml();78$response->say(79$this->_thankYouMessage,80['voice' => 'Polly.Amy', 'language' => 'en-GB']81);82$response->say(83"You'll be connected shortly to your planet",84['voice' => 'Polly.Amy', 'language' => 'en-GB']85);8687$planetNumbers = [88'2' => '+19295566487',89'3' => '+17262043675',90'4' => '+16513582243'91];92$selectedOption = $request->input('Digits');9394$planetNumberExists = isset($planetNumbers[$selectedOption]);9596if ($planetNumberExists) {97$selectedNumber = $planetNumbers[$selectedOption];98$response->dial($selectedNumber);99100return $response;101} else {102$errorResponse = new Twiml();103$errorResponse->say(104'Returning to the main menu',105['voice' => 'Polly.Amy', 'language' => 'en-GB']106);107$errorResponse->redirect(route('welcome', [], false));108109return $errorResponse;110}111112}113114115/**116* Responds with instructions to mothership117* @return Services_Twilio_Twiml118*/119private function _getReturnInstructions()120{121$response = new Twiml();122$response->say(123'To get to your extraction point, get on your bike and go down the' .124' street. Then Left down an alley. Avoid the police cars. Turn left' .125' into an unfinished housing development. Fly over the roadblock. Go' .126' passed the moon. Soon after you will see your mother ship.',127['voice' => 'Polly.Amy', 'language' => 'en-GB']128);129$response->say(130$this->_thankYouMessage,131['voice' => 'Polly.Amy', 'language' => 'en-GB']132);133134$response->hangup();135136return $response;137}138139/**140* Responds with instructions to choose a planet141* @return Services_Twilio_Twiml142*/143private function _getPlanetsMenu()144{145$response = new Twiml();146$gather = $response->gather(147['numDigits' => '1', 'action' => route('planet-connection', [], false)]148);149$gather->say(150'To call the planet Brodo Asogi, press 2. To call the planet' .151' Dugobah, press 3. To call an Oober asteroid to your location,' .152' press 4. To go back to the main menu, press the star key',153['voice' => 'Polly.Amy', 'language' => 'en-GB']154);155156return $response;157}158}
Again we show some options to the caller and instruct Twilio to collect the caller's choice. Let's look at that route next.
In this controller, we grab the caller's selection off the request and store it in a variable called $selectedOption
. We then use a Dial
verb with the appropriate phone number to connect our caller to their home planet.
The current numbers are hardcoded, but they could also be read from a database or from a file.
app/Http/Controllers/IvrController.php
1<?php23namespace App\Http\Controllers;45use App\Http\Requests;6use Illuminate\Http\Request;7use Twilio\Twiml;89class IvrController extends Controller10{11public function __construct()12{13$this->_thankYouMessage = 'Thank you for calling the ET Phone Home' .14' Service - the adventurous alien\'s first choice' .15' in intergalactic travel.';1617}1819/**20* Responds with a welcome message with instructions21*22* @return \Illuminate\Http\Response23*/24public function showWelcome()25{26$response = new Twiml();27$gather = $response->gather(28[29'numDigits' => 1,30'action' => route('menu-response', [], false)31]32);3334$gather->say(35'Thanks for calling the E T Phone Home Service.' .36'Please press 1 for directions. Press 2 for a ' .37'list of planets to call.',38['loop' => 3]39);4041return $response;42}4344/**45* Responds to selection of an option by the caller46*47* @return \Illuminate\Http\Response48*/49public function showMenuResponse(Request $request)50{51$selectedOption = $request->input('Digits');5253switch ($selectedOption) {54case 1:55return $this->_getReturnInstructions();56case 2:57return $this->_getPlanetsMenu();58}5960$response = new Twiml();61$response->say(62'Returning to the main menu',63['voice' => 'Polly.Amy', 'language' => 'en-GB']64);65$response->redirect(route('welcome', [], false));6667return $response;68}6970/**71* Responds with a <Dial> to the caller's planet72*73* @return \Illuminate\Http\Response74*/75public function showPlanetConnection(Request $request)76{77$response = new Twiml();78$response->say(79$this->_thankYouMessage,80['voice' => 'Polly.Amy', 'language' => 'en-GB']81);82$response->say(83"You'll be connected shortly to your planet",84['voice' => 'Polly.Amy', 'language' => 'en-GB']85);8687$planetNumbers = [88'2' => '+19295566487',89'3' => '+17262043675',90'4' => '+16513582243'91];92$selectedOption = $request->input('Digits');9394$planetNumberExists = isset($planetNumbers[$selectedOption]);9596if ($planetNumberExists) {97$selectedNumber = $planetNumbers[$selectedOption];98$response->dial($selectedNumber);99100return $response;101} else {102$errorResponse = new Twiml();103$errorResponse->say(104'Returning to the main menu',105['voice' => 'Polly.Amy', 'language' => 'en-GB']106);107$errorResponse->redirect(route('welcome', [], false));108109return $errorResponse;110}111112}113114115/**116* Responds with instructions to mothership117* @return Services_Twilio_Twiml118*/119private function _getReturnInstructions()120{121$response = new Twiml();122$response->say(123'To get to your extraction point, get on your bike and go down the' .124' street. Then Left down an alley. Avoid the police cars. Turn left' .125' into an unfinished housing development. Fly over the roadblock. Go' .126' passed the moon. Soon after you will see your mother ship.',127['voice' => 'Polly.Amy', 'language' => 'en-GB']128);129$response->say(130$this->_thankYouMessage,131['voice' => 'Polly.Amy', 'language' => 'en-GB']132);133134$response->hangup();135136return $response;137}138139/**140* Responds with instructions to choose a planet141* @return Services_Twilio_Twiml142*/143private function _getPlanetsMenu()144{145$response = new Twiml();146$gather = $response->gather(147['numDigits' => '1', 'action' => route('planet-connection', [], false)]148);149$gather->say(150'To call the planet Brodo Asogi, press 2. To call the planet' .151' Dugobah, press 3. To call an Oober asteroid to your location,' .152' press 4. To go back to the main menu, press the star key',153['voice' => 'Polly.Amy', 'language' => 'en-GB']154);155156return $response;157}158}
That's it! We've just implemented an IVR phone tree that will delight and serve your customers.
If you're a PHP developer working with Twilio, you might enjoy these other tutorials:
Instantly collect structured data from your users with a survey conducted over a call or an SMS text message. Learn how to create your own survey in PHP.
Click-to-call enables your company to convert web traffic into phone calls with the click of a button.