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.
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
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.
After saying the text to the caller and retrieving their input, Twilio will send this input to our application.
action parameter takes an absolute or relative URL as a value - in our case, the
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 occurring after a
<Gather> are unreachable, unless the caller enters no digits.
Now that we have told Twilio where to send the caller's input, we can look at how to process that input.
This route handles processing the caller's input.
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.
If the caller chooses '2', we will take them to the Planet Directory where we need to collect more input.
If our callers choose to call their home planet we will read them the planet directory. This is akin to a typical "company directory" feature of most IVRs.
In this route, we grab the caller's selection from 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.
That's it! We've just implemented an IVR phone tree that will delight and serve your customers.
If you're a Node.js developer working with Twilio, you might want to check out these other tutorials:
Use Twilio and Twilio-powered Authy to implement account verification at the point of registration.
Use Twilio and Twilio-powered Authy OneTouch to implement two-factor authentication (2FA) in your web app
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!