Skip to contentSkip to navigationSkip to topbar
Rate this page:
On this page

IVR: Phone Tree with PHP and Laravel


ET Phone Home.

This Laravel(link takes you to an external page) sample application is modeled after a typical call center experience with an IVR(link takes you to an external page), but with more Reese's Pieces(link takes you to an external page).

Stranded aliens can call a phone number and receive instructions on how to get out of earth safely or call their home planet(link takes you to an external page) 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(link takes you to an external page).

Read how Livestream used Twilio to build custom call routing logic and IVR messages.(link takes you to an external page)


Answering a Phone Call

answering-a-phone-call page anchor

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.

IVR Webhook Configuration.

If you don't already have a server configured to use as your webhook, ngrok(link takes you to an external page) is a great tool for testing webhooks locally.

With our Twilio number configured, we are prepared to respond to the Twilio request.


Respond to the Twilio request with TwiML

respond-to-the-twilio-request-with-twiml page anchor

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.

Respond with TwiML to gather an option from the caller

respond-with-twiml-to-gather-an-option-from-the-caller page anchor

app/Http/Controllers/IvrController.php


_158
<?php
_158
_158
namespace App\Http\Controllers;
_158
_158
use App\Http\Requests;
_158
use Illuminate\Http\Request;
_158
use Twilio\Twiml;
_158
_158
class IvrController extends Controller
_158
{
_158
public function __construct()
_158
{
_158
$this->_thankYouMessage = 'Thank you for calling the ET Phone Home' .
_158
' Service - the adventurous alien\'s first choice' .
_158
' in intergalactic travel.';
_158
_158
}
_158
_158
/**
_158
* Responds with a welcome message with instructions
_158
*
_158
* @return \Illuminate\Http\Response
_158
*/
_158
public function showWelcome()
_158
{
_158
$response = new Twiml();
_158
$gather = $response->gather(
_158
[
_158
'numDigits' => 1,
_158
'action' => route('menu-response', [], false)
_158
]
_158
);
_158
_158
$gather->say(
_158
'Thanks for calling the E T Phone Home Service.' .
_158
'Please press 1 for directions. Press 2 for a ' .
_158
'list of planets to call.',
_158
['loop' => 3]
_158
);
_158
_158
return $response;
_158
}
_158
_158
/**
_158
* Responds to selection of an option by the caller
_158
*
_158
* @return \Illuminate\Http\Response
_158
*/
_158
public function showMenuResponse(Request $request)
_158
{
_158
$selectedOption = $request->input('Digits');
_158
_158
switch ($selectedOption) {
_158
case 1:
_158
return $this->_getReturnInstructions();
_158
case 2:
_158
return $this->_getPlanetsMenu();
_158
}
_158
_158
$response = new Twiml();
_158
$response->say(
_158
'Returning to the main menu',
_158
['voice' => 'Polly.Amy', 'language' => 'en-GB']
_158
);
_158
$response->redirect(route('welcome', [], false));
_158
_158
return $response;
_158
}
_158
_158
/**
_158
* Responds with a <Dial> to the caller's planet
_158
*
_158
* @return \Illuminate\Http\Response
_158
*/
_158
public function showPlanetConnection(Request $request)
_158
{
_158
$response = new Twiml();
_158
$response->say(
_158
$this->_thankYouMessage,
_158
['voice' => 'Polly.Amy', 'language' => 'en-GB']
_158
);
_158
$response->say(
_158
"You'll be connected shortly to your planet",
_158
['voice' => 'Polly.Amy', 'language' => 'en-GB']
_158
);
_158
_158
$planetNumbers = [
_158
'2' => '+19295566487',
_158
'3' => '+17262043675',
_158
'4' => '+16513582243'
_158
];
_158
$selectedOption = $request->input('Digits');
_158
_158
$planetNumberExists = isset($planetNumbers[$selectedOption]);
_158
_158
if ($planetNumberExists) {
_158
$selectedNumber = $planetNumbers[$selectedOption];
_158
$response->dial($selectedNumber);
_158
_158
return $response;
_158
} else {
_158
$errorResponse = new Twiml();
_158
$errorResponse->say(
_158
'Returning to the main menu',
_158
['voice' => 'Polly.Amy', 'language' => 'en-GB']
_158
);
_158
$errorResponse->redirect(route('welcome', [], false));
_158
_158
return $errorResponse;
_158
}
_158
_158
}
_158
_158
_158
/**
_158
* Responds with instructions to mothership
_158
* @return Services_Twilio_Twiml
_158
*/
_158
private function _getReturnInstructions()
_158
{
_158
$response = new Twiml();
_158
$response->say(
_158
'To get to your extraction point, get on your bike and go down the' .
_158
' street. Then Left down an alley. Avoid the police cars. Turn left' .
_158
' into an unfinished housing development. Fly over the roadblock. Go' .
_158
' passed the moon. Soon after you will see your mother ship.',
_158
['voice' => 'Polly.Amy', 'language' => 'en-GB']
_158
);
_158
$response->say(
_158
$this->_thankYouMessage,
_158
['voice' => 'Polly.Amy', 'language' => 'en-GB']
_158
);
_158
_158
$response->hangup();
_158
_158
return $response;
_158
}
_158
_158
/**
_158
* Responds with instructions to choose a planet
_158
* @return Services_Twilio_Twiml
_158
*/
_158
private function _getPlanetsMenu()
_158
{
_158
$response = new Twiml();
_158
$gather = $response->gather(
_158
['numDigits' => '1', 'action' => route('planet-connection', [], false)]
_158
);
_158
$gather->say(
_158
'To call the planet Brodo Asogi, press 2. To call the planet' .
_158
' Dugobah, press 3. To call an Oober asteroid to your location,' .
_158
' press 4. To go back to the main menu, press the star key',
_158
['voice' => 'Polly.Amy', 'language' => 'en-GB']
_158
);
_158
_158
return $response;
_158
}
_158
}

Let's dig in to what happens with the caller's input next.


Where to send the caller's input

where-to-send-the-callers-input page anchor

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.

Send caller input to the intended route

send-caller-input-to-the-intended-route page anchor

app/Http/Controllers/IvrController.php


_158
<?php
_158
_158
namespace App\Http\Controllers;
_158
_158
use App\Http\Requests;
_158
use Illuminate\Http\Request;
_158
use Twilio\Twiml;
_158
_158
class IvrController extends Controller
_158
{
_158
public function __construct()
_158
{
_158
$this->_thankYouMessage = 'Thank you for calling the ET Phone Home' .
_158
' Service - the adventurous alien\'s first choice' .
_158
' in intergalactic travel.';
_158
_158
}
_158
_158
/**
_158
* Responds with a welcome message with instructions
_158
*
_158
* @return \Illuminate\Http\Response
_158
*/
_158
public function showWelcome()
_158
{
_158
$response = new Twiml();
_158
$gather = $response->gather(
_158
[
_158
'numDigits' => 1,
_158
'action' => route('menu-response', [], false)
_158
]
_158
);
_158
_158
$gather->say(
_158
'Thanks for calling the E T Phone Home Service.' .
_158
'Please press 1 for directions. Press 2 for a ' .
_158
'list of planets to call.',
_158
['loop' => 3]
_158
);
_158
_158
return $response;
_158
}
_158
_158
/**
_158
* Responds to selection of an option by the caller
_158
*
_158
* @return \Illuminate\Http\Response
_158
*/
_158
public function showMenuResponse(Request $request)
_158
{
_158
$selectedOption = $request->input('Digits');
_158
_158
switch ($selectedOption) {
_158
case 1:
_158
return $this->_getReturnInstructions();
_158
case 2:
_158
return $this->_getPlanetsMenu();
_158
}
_158
_158
$response = new Twiml();
_158
$response->say(
_158
'Returning to the main menu',
_158
['voice' => 'Polly.Amy', 'language' => 'en-GB']
_158
);
_158
$response->redirect(route('welcome', [], false));
_158
_158
return $response;
_158
}
_158
_158
/**
_158
* Responds with a <Dial> to the caller's planet
_158
*
_158
* @return \Illuminate\Http\Response
_158
*/
_158
public function showPlanetConnection(Request $request)
_158
{
_158
$response = new Twiml();
_158
$response->say(
_158
$this->_thankYouMessage,
_158
['voice' => 'Polly.Amy', 'language' => 'en-GB']
_158
);
_158
$response->say(
_158
"You'll be connected shortly to your planet",
_158
['voice' => 'Polly.Amy', 'language' => 'en-GB']
_158
);
_158
_158
$planetNumbers = [
_158
'2' => '+19295566487',
_158
'3' => '+17262043675',
_158
'4' => '+16513582243'
_158
];
_158
$selectedOption = $request->input('Digits');
_158
_158
$planetNumberExists = isset($planetNumbers[$selectedOption]);
_158
_158
if ($planetNumberExists) {
_158
$selectedNumber = $planetNumbers[$selectedOption];
_158
$response->dial($selectedNumber);
_158
_158
return $response;
_158
} else {
_158
$errorResponse = new Twiml();
_158
$errorResponse->say(
_158
'Returning to the main menu',
_158
['voice' => 'Polly.Amy', 'language' => 'en-GB']
_158
);
_158
$errorResponse->redirect(route('welcome', [], false));
_158
_158
return $errorResponse;
_158
}
_158
_158
}
_158
_158
_158
/**
_158
* Responds with instructions to mothership
_158
* @return Services_Twilio_Twiml
_158
*/
_158
private function _getReturnInstructions()
_158
{
_158
$response = new Twiml();
_158
$response->say(
_158
'To get to your extraction point, get on your bike and go down the' .
_158
' street. Then Left down an alley. Avoid the police cars. Turn left' .
_158
' into an unfinished housing development. Fly over the roadblock. Go' .
_158
' passed the moon. Soon after you will see your mother ship.',
_158
['voice' => 'Polly.Amy', 'language' => 'en-GB']
_158
);
_158
$response->say(
_158
$this->_thankYouMessage,
_158
['voice' => 'Polly.Amy', 'language' => 'en-GB']
_158
);
_158
_158
$response->hangup();
_158
_158
return $response;
_158
}
_158
_158
/**
_158
* Responds with instructions to choose a planet
_158
* @return Services_Twilio_Twiml
_158
*/
_158
private function _getPlanetsMenu()
_158
{
_158
$response = new Twiml();
_158
$gather = $response->gather(
_158
['numDigits' => '1', 'action' => route('planet-connection', [], false)]
_158
);
_158
$gather->say(
_158
'To call the planet Brodo Asogi, press 2. To call the planet' .
_158
' Dugobah, press 3. To call an Oober asteroid to your location,' .
_158
' press 4. To go back to the main menu, press the star key',
_158
['voice' => 'Polly.Amy', 'language' => 'en-GB']
_158
);
_158
_158
return $response;
_158
}
_158
}

Now that we have told Twilio where to send the caller's input, we can look at how to process that input.


The Main Menu: Process the caller's selection

the-main-menu-process-the-callers-selection page anchor

This route handles processing the caller's input.

If our caller chooses '1' for directions, we use the helper method _getReturnInstructions to respond with TwiML that will Say directions to our caller's extraction point.

If the caller chooses '2' to call their home planet, then we need to gather more input from them. We'll cover this in the next step.

If the caller enters anything else, we respond with a TwiML Redirect to the main menu.

Route caller to return instructions or main menu

route-caller-to-return-instructions-or-main-menu page anchor

app/Http/Controllers/IvrController.php


_158
<?php
_158
_158
namespace App\Http\Controllers;
_158
_158
use App\Http\Requests;
_158
use Illuminate\Http\Request;
_158
use Twilio\Twiml;
_158
_158
class IvrController extends Controller
_158
{
_158
public function __construct()
_158
{
_158
$this->_thankYouMessage = 'Thank you for calling the ET Phone Home' .
_158
' Service - the adventurous alien\'s first choice' .
_158
' in intergalactic travel.';
_158
_158
}
_158
_158
/**
_158
* Responds with a welcome message with instructions
_158
*
_158
* @return \Illuminate\Http\Response
_158
*/
_158
public function showWelcome()
_158
{
_158
$response = new Twiml();
_158
$gather = $response->gather(
_158
[
_158
'numDigits' => 1,
_158
'action' => route('menu-response', [], false)
_158
]
_158
);
_158
_158
$gather->say(
_158
'Thanks for calling the E T Phone Home Service.' .
_158
'Please press 1 for directions. Press 2 for a ' .
_158
'list of planets to call.',
_158
['loop' => 3]
_158
);
_158
_158
return $response;
_158
}
_158
_158
/**
_158
* Responds to selection of an option by the caller
_158
*
_158
* @return \Illuminate\Http\Response
_158
*/
_158
public function showMenuResponse(Request $request)
_158
{
_158
$selectedOption = $request->input('Digits');
_158
_158
switch ($selectedOption) {
_158
case 1:
_158
return $this->_getReturnInstructions();
_158
case 2:
_158
return $this->_getPlanetsMenu();
_158
}
_158
_158
$response = new Twiml();
_158
$response->say(
_158
'Returning to the main menu',
_158
['voice' => 'Polly.Amy', 'language' => 'en-GB']
_158
);
_158
$response->redirect(route('welcome', [], false));
_158
_158
return $response;
_158
}
_158
_158
/**
_158
* Responds with a <Dial> to the caller's planet
_158
*
_158
* @return \Illuminate\Http\Response
_158
*/
_158
public function showPlanetConnection(Request $request)
_158
{
_158
$response = new Twiml();
_158
$response->say(
_158
$this->_thankYouMessage,
_158
['voice' => 'Polly.Amy', 'language' => 'en-GB']
_158
);
_158
$response->say(
_158
"You'll be connected shortly to your planet",
_158
['voice' => 'Polly.Amy', 'language' => 'en-GB']
_158
);
_158
_158
$planetNumbers = [
_158
'2' => '+19295566487',
_158
'3' => '+17262043675',
_158
'4' => '+16513582243'
_158
];
_158
$selectedOption = $request->input('Digits');
_158
_158
$planetNumberExists = isset($planetNumbers[$selectedOption]);
_158
_158
if ($planetNumberExists) {
_158
$selectedNumber = $planetNumbers[$selectedOption];
_158
$response->dial($selectedNumber);
_158
_158
return $response;
_158
} else {
_158
$errorResponse = new Twiml();
_158
$errorResponse->say(
_158
'Returning to the main menu',
_158
['voice' => 'Polly.Amy', 'language' => 'en-GB']
_158
);
_158
$errorResponse->redirect(route('welcome', [], false));
_158
_158
return $errorResponse;
_158
}
_158
_158
}
_158
_158
_158
/**
_158
* Responds with instructions to mothership
_158
* @return Services_Twilio_Twiml
_158
*/
_158
private function _getReturnInstructions()
_158
{
_158
$response = new Twiml();
_158
$response->say(
_158
'To get to your extraction point, get on your bike and go down the' .
_158
' street. Then Left down an alley. Avoid the police cars. Turn left' .
_158
' into an unfinished housing development. Fly over the roadblock. Go' .
_158
' passed the moon. Soon after you will see your mother ship.',
_158
['voice' => 'Polly.Amy', 'language' => 'en-GB']
_158
);
_158
$response->say(
_158
$this->_thankYouMessage,
_158
['voice' => 'Polly.Amy', 'language' => 'en-GB']
_158
);
_158
_158
$response->hangup();
_158
_158
return $response;
_158
}
_158
_158
/**
_158
* Responds with instructions to choose a planet
_158
* @return Services_Twilio_Twiml
_158
*/
_158
private function _getPlanetsMenu()
_158
{
_158
$response = new Twiml();
_158
$gather = $response->gather(
_158
['numDigits' => '1', 'action' => route('planet-connection', [], false)]
_158
);
_158
$gather->say(
_158
'To call the planet Brodo Asogi, press 2. To call the planet' .
_158
' Dugobah, press 3. To call an Oober asteroid to your location,' .
_158
' press 4. To go back to the main menu, press the star key',
_158
['voice' => 'Polly.Amy', 'language' => 'en-GB']
_158
);
_158
_158
return $response;
_158
}
_158
}

If the caller chooses '2', we will take them to The Planet Directory where we need to collect more input.


The Planet Directory: Collect more input from the caller

the-planet-directory-collect-more-input-from-the-caller page anchor

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.

Collect more input from the caller via the Planet Directory

collect-more-input-from-the-caller-via-the-planet-directory page anchor

app/Http/Controllers/IvrController.php


_158
<?php
_158
_158
namespace App\Http\Controllers;
_158
_158
use App\Http\Requests;
_158
use Illuminate\Http\Request;
_158
use Twilio\Twiml;
_158
_158
class IvrController extends Controller
_158
{
_158
public function __construct()
_158
{
_158
$this->_thankYouMessage = 'Thank you for calling the ET Phone Home' .
_158
' Service - the adventurous alien\'s first choice' .
_158
' in intergalactic travel.';
_158
_158
}
_158
_158
/**
_158
* Responds with a welcome message with instructions
_158
*
_158
* @return \Illuminate\Http\Response
_158
*/
_158
public function showWelcome()
_158
{
_158
$response = new Twiml();
_158
$gather = $response->gather(
_158
[
_158
'numDigits' => 1,
_158
'action' => route('menu-response', [], false)
_158
]
_158
);
_158
_158
$gather->say(
_158
'Thanks for calling the E T Phone Home Service.' .
_158
'Please press 1 for directions. Press 2 for a ' .
_158
'list of planets to call.',
_158
['loop' => 3]
_158
);
_158
_158
return $response;
_158
}
_158
_158
/**
_158
* Responds to selection of an option by the caller
_158
*
_158
* @return \Illuminate\Http\Response
_158
*/
_158
public function showMenuResponse(Request $request)
_158
{
_158
$selectedOption = $request->input('Digits');
_158
_158
switch ($selectedOption) {
_158
case 1:
_158
return $this->_getReturnInstructions();
_158
case 2:
_158
return $this->_getPlanetsMenu();
_158
}
_158
_158
$response = new Twiml();
_158
$response->say(
_158
'Returning to the main menu',
_158
['voice' => 'Polly.Amy', 'language' => 'en-GB']
_158
);
_158
$response->redirect(route('welcome', [], false));
_158
_158
return $response;
_158
}
_158
_158
/**
_158
* Responds with a <Dial> to the caller's planet
_158
*
_158
* @return \Illuminate\Http\Response
_158
*/
_158
public function showPlanetConnection(Request $request)
_158
{
_158
$response = new Twiml();
_158
$response->say(
_158
$this->_thankYouMessage,
_158
['voice' => 'Polly.Amy', 'language' => 'en-GB']
_158
);
_158
$response->say(
_158
"You'll be connected shortly to your planet",
_158
['voice' => 'Polly.Amy', 'language' => 'en-GB']
_158
);
_158
_158
$planetNumbers = [
_158
'2' => '+19295566487',
_158
'3' => '+17262043675',
_158
'4' => '+16513582243'
_158
];
_158
$selectedOption = $request->input('Digits');
_158
_158
$planetNumberExists = isset($planetNumbers[$selectedOption]);
_158
_158
if ($planetNumberExists) {
_158
$selectedNumber = $planetNumbers[$selectedOption];
_158
$response->dial($selectedNumber);
_158
_158
return $response;
_158
} else {
_158
$errorResponse = new Twiml();
_158
$errorResponse->say(
_158
'Returning to the main menu',
_158
['voice' => 'Polly.Amy', 'language' => 'en-GB']
_158
);
_158
$errorResponse->redirect(route('welcome', [], false));
_158
_158
return $errorResponse;
_158
}
_158
_158
}
_158
_158
_158
/**
_158
* Responds with instructions to mothership
_158
* @return Services_Twilio_Twiml
_158
*/
_158
private function _getReturnInstructions()
_158
{
_158
$response = new Twiml();
_158
$response->say(
_158
'To get to your extraction point, get on your bike and go down the' .
_158
' street. Then Left down an alley. Avoid the police cars. Turn left' .
_158
' into an unfinished housing development. Fly over the roadblock. Go' .
_158
' passed the moon. Soon after you will see your mother ship.',
_158
['voice' => 'Polly.Amy', 'language' => 'en-GB']
_158
);
_158
$response->say(
_158
$this->_thankYouMessage,
_158
['voice' => 'Polly.Amy', 'language' => 'en-GB']
_158
);
_158
_158
$response->hangup();
_158
_158
return $response;
_158
}
_158
_158
/**
_158
* Responds with instructions to choose a planet
_158
* @return Services_Twilio_Twiml
_158
*/
_158
private function _getPlanetsMenu()
_158
{
_158
$response = new Twiml();
_158
$gather = $response->gather(
_158
['numDigits' => '1', 'action' => route('planet-connection', [], false)]
_158
);
_158
$gather->say(
_158
'To call the planet Brodo Asogi, press 2. To call the planet' .
_158
' Dugobah, press 3. To call an Oober asteroid to your location,' .
_158
' press 4. To go back to the main menu, press the star key',
_158
['voice' => 'Polly.Amy', 'language' => 'en-GB']
_158
);
_158
_158
return $response;
_158
}
_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.


The Planet Directory: Connect the caller to another number

the-planet-directory-connect-the-caller-to-another-number page anchor

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.

Connect to another number based on caller input

connect-to-another-number-based-on-caller-input page anchor

app/Http/Controllers/IvrController.php


_158
<?php
_158
_158
namespace App\Http\Controllers;
_158
_158
use App\Http\Requests;
_158
use Illuminate\Http\Request;
_158
use Twilio\Twiml;
_158
_158
class IvrController extends Controller
_158
{
_158
public function __construct()
_158
{
_158
$this->_thankYouMessage = 'Thank you for calling the ET Phone Home' .
_158
' Service - the adventurous alien\'s first choice' .
_158
' in intergalactic travel.';
_158
_158
}
_158
_158
/**
_158
* Responds with a welcome message with instructions
_158
*
_158
* @return \Illuminate\Http\Response
_158
*/
_158
public function showWelcome()
_158
{
_158
$response = new Twiml();
_158
$gather = $response->gather(
_158
[
_158
'numDigits' => 1,
_158
'action' => route('menu-response', [], false)
_158
]
_158
);
_158
_158
$gather->say(
_158
'Thanks for calling the E T Phone Home Service.' .
_158
'Please press 1 for directions. Press 2 for a ' .
_158
'list of planets to call.',
_158
['loop' => 3]
_158
);
_158
_158
return $response;
_158
}
_158
_158
/**
_158
* Responds to selection of an option by the caller
_158
*
_158
* @return \Illuminate\Http\Response
_158
*/
_158
public function showMenuResponse(Request $request)
_158
{
_158
$selectedOption = $request->input('Digits');
_158
_158
switch ($selectedOption) {
_158
case 1:
_158
return $this->_getReturnInstructions();
_158
case 2:
_158
return $this->_getPlanetsMenu();
_158
}
_158
_158
$response = new Twiml();
_158
$response->say(
_158
'Returning to the main menu',
_158
['voice' => 'Polly.Amy', 'language' => 'en-GB']
_158
);
_158
$response->redirect(route('welcome', [], false));
_158
_158
return $response;
_158
}
_158
_158
/**
_158
* Responds with a <Dial> to the caller's planet
_158
*
_158
* @return \Illuminate\Http\Response
_158
*/
_158
public function showPlanetConnection(Request $request)
_158
{
_158
$response = new Twiml();
_158
$response->say(
_158
$this->_thankYouMessage,
_158
['voice' => 'Polly.Amy', 'language' => 'en-GB']
_158
);
_158
$response->say(
_158
"You'll be connected shortly to your planet",
_158
['voice' => 'Polly.Amy', 'language' => 'en-GB']
_158
);
_158
_158
$planetNumbers = [
_158
'2' => '+19295566487',
_158
'3' => '+17262043675',
_158
'4' => '+16513582243'
_158
];
_158
$selectedOption = $request->input('Digits');
_158
_158
$planetNumberExists = isset($planetNumbers[$selectedOption]);
_158
_158
if ($planetNumberExists) {
_158
$selectedNumber = $planetNumbers[$selectedOption];
_158
$response->dial($selectedNumber);
_158
_158
return $response;
_158
} else {
_158
$errorResponse = new Twiml();
_158
$errorResponse->say(
_158
'Returning to the main menu',
_158
['voice' => 'Polly.Amy', 'language' => 'en-GB']
_158
);
_158
$errorResponse->redirect(route('welcome', [], false));
_158
_158
return $errorResponse;
_158
}
_158
_158
}
_158
_158
_158
/**
_158
* Responds with instructions to mothership
_158
* @return Services_Twilio_Twiml
_158
*/
_158
private function _getReturnInstructions()
_158
{
_158
$response = new Twiml();
_158
$response->say(
_158
'To get to your extraction point, get on your bike and go down the' .
_158
' street. Then Left down an alley. Avoid the police cars. Turn left' .
_158
' into an unfinished housing development. Fly over the roadblock. Go' .
_158
' passed the moon. Soon after you will see your mother ship.',
_158
['voice' => 'Polly.Amy', 'language' => 'en-GB']
_158
);
_158
$response->say(
_158
$this->_thankYouMessage,
_158
['voice' => 'Polly.Amy', 'language' => 'en-GB']
_158
);
_158
_158
$response->hangup();
_158
_158
return $response;
_158
}
_158
_158
/**
_158
* Responds with instructions to choose a planet
_158
* @return Services_Twilio_Twiml
_158
*/
_158
private function _getPlanetsMenu()
_158
{
_158
$response = new Twiml();
_158
$gather = $response->gather(
_158
['numDigits' => '1', 'action' => route('planet-connection', [], false)]
_158
);
_158
$gather->say(
_158
'To call the planet Brodo Asogi, press 2. To call the planet' .
_158
' Dugobah, press 3. To call an Oober asteroid to your location,' .
_158
' press 4. To go back to the main menu, press the star key',
_158
['voice' => 'Polly.Amy', 'language' => 'en-GB']
_158
);
_158
_158
return $response;
_158
}
_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:

Automated Survey(link takes you to an external page)

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

Click-to-call enables your company to convert web traffic into phone calls with the click of a button.

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(link takes you to an external page) and let us know what you build!


Rate this page: