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

IVR: Phone Tree with C# and ASP.NET MVC


ET Phone Home: IVR C# Example.

This ASP.NET MVC(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(link takes you to an external page) and others(link takes you to an external page) built IVR with Twilio. Also, find sample code for many web languages and frameworks on our IVR application page.


Respond to the Phone Call

respond-to-the-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(link takes you to an external page) 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

IVRPhoneTree.Web/Controllers/IVRController.cs


_30
using System.Web.Mvc;
_30
using Twilio.AspNet.Mvc;
_30
using Twilio.TwiML;
_30
using Twilio.TwiML.Voice;
_30
_30
namespace IVRPhoneTree.Web.Controllers
_30
{
_30
public class IVRController : TwilioController
_30
{
_30
// GET: IVR
_30
public ActionResult Index()
_30
{
_30
return View();
_30
}
_30
_30
// POST: IVR/Welcome
_30
[HttpPost]
_30
public TwiMLResult Welcome()
_30
{
_30
var response = new VoiceResponse();
_30
var gather = new Gather(action: Url.ActionUri("Show", "Menu"), numDigits: 1);
_30
gather.Say("Thank you for calling the E.T. Phone Home Service - the " +
_30
"adventurous alien's first choice in intergalactic travel. " +
_30
"Press 1 for directions, press 2 to make a call.");
_30
response.Append(gather);
_30
_30
return TwiML(response);
_30
}
_30
}
_30
}

After reading the text to the caller and retrieving their input, Twilio will send this input to our application.


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, the /menu/show 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

IVRPhoneTree.Web/Controllers/IVRController.cs


_30
using System.Web.Mvc;
_30
using Twilio.AspNet.Mvc;
_30
using Twilio.TwiML;
_30
using Twilio.TwiML.Voice;
_30
_30
namespace IVRPhoneTree.Web.Controllers
_30
{
_30
public class IVRController : TwilioController
_30
{
_30
// GET: IVR
_30
public ActionResult Index()
_30
{
_30
return View();
_30
}
_30
_30
// POST: IVR/Welcome
_30
[HttpPost]
_30
public TwiMLResult Welcome()
_30
{
_30
var response = new VoiceResponse();
_30
var gather = new Gather(action: Url.ActionUri("Show", "Menu"), numDigits: 1);
_30
gather.Say("Thank you for calling the E.T. Phone Home Service - the " +
_30
"adventurous alien's first choice in intergalactic travel. " +
_30
"Press 1 for directions, press 2 to make a call.");
_30
response.Append(gather);
_30
_30
return TwiML(response);
_30
}
_30
}
_30
}

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 ReturnInstructions to respond with TwiML that will Say directions to our caller's extraction point.

If the caller chooses 2 to call their home planet, 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.


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. This time, the action verb points to the planets route, which will switch 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

IVRPhoneTree.Web/Controllers/MenuController.cs


_58
using System;
_58
using System.Collections.Generic;
_58
using System.Web.Mvc;
_58
using Twilio.AspNet.Mvc;
_58
using Twilio.TwiML;
_58
using Twilio.TwiML.Voice;
_58
_58
namespace IVRPhoneTree.Web.Controllers
_58
{
_58
public class MenuController : ControllerBase
_58
{
_58
// POST: Menu/Show
_58
[HttpPost]
_58
public ActionResult Show(string digits)
_58
{
_58
var selectedOption = digits;
_58
var optionActions = new Dictionary<string, Func<ActionResult>>()
_58
{
_58
{"1", ReturnInstructions},
_58
{"2", Planets}
_58
};
_58
_58
return optionActions.ContainsKey(selectedOption) ?
_58
optionActions[selectedOption]() :
_58
RedirectWelcome();
_58
}
_58
_58
private TwiMLResult ReturnInstructions()
_58
{
_58
var response = new VoiceResponse();
_58
response.Say("To get to your extraction point, get on your bike and go down " +
_58
"the street. Then Left down an alley. Avoid the police cars. Turn left " +
_58
"into an unfinished housing development. Fly over the roadblock. Go " +
_58
"passed the moon. Soon after you will see your mother ship.",
_58
voice: Say.VoiceEnum.PollyAmy, language: "en-GB");
_58
_58
response.Say("Thank you for calling the E.T. Phone Home Service - the " +
_58
"adventurous alien's first choice in intergalactic travel. Good bye.");
_58
_58
response.Hangup();
_58
_58
return TwiML(response);
_58
}
_58
_58
private TwiMLResult Planets()
_58
{
_58
var response = new VoiceResponse();
_58
var gather = new Gather(action: Url.ActionUri("Interconnect", "PhoneExchange"), numDigits: 1);
_58
gather.Say("To call the planet Broh doe As O G, press 2. To call the planet " +
_58
"DuhGo bah, press 3. To call an oober asteroid to your location, press 4. To " +
_58
"go back to the main menu, press the star key ",
_58
voice: Say.VoiceEnum.PollyAmy, language: "en-GB", loop: 3);
_58
response.Append(gather);
_58
_58
return TwiML(response);
_58
}
_58
}
_58
}

Again we show some options to the caller and instruct Twilio to collect the caller's choice.


The Planet Directory: Connect the caller to another number

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

In this route, we grab the caller's selection off the request and store it in a variable called userOption. 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

IVRPhoneTree.Web/Controllers/PhoneExchangeController.cs


_34
using System.Collections.Generic;
_34
using System.Web.Mvc;
_34
using Twilio.AspNet.Mvc;
_34
using Twilio.TwiML;
_34
_34
namespace IVRPhoneTree.Web.Controllers
_34
{
_34
public class PhoneExchangeController : ControllerBase
_34
{
_34
// POST: PhoneExchange/Interconnect
_34
[HttpPost]
_34
public ActionResult Interconnect(string digits)
_34
{
_34
var userOption = digits;
_34
var optionPhones = new Dictionary<string, string>
_34
{
_34
{"2", "+19295566487"},
_34
{"3", "+17262043675"},
_34
{"4", "+16513582243"}
_34
};
_34
_34
return optionPhones.ContainsKey(userOption)
_34
? Dial(optionPhones[userOption]) : RedirectWelcome();
_34
}
_34
_34
private TwiMLResult Dial(string phoneNumber)
_34
{
_34
var response = new VoiceResponse();
_34
response.Dial(phoneNumber);
_34
_34
return TwiML(response);
_34
}
_34
}
_34
}

That's it! We've just implemented an IVR phone tree that will delight and serve your customers.


If you're a C# 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 voice call or SMS text messages. Learn how to create your own survey in ASP.NET MVC.

Call Tracking(link takes you to an external page)

Convert web traffic into phone calls with the click of a button.

Did this help?

did-this-help page anchor

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: