Skip to contentSkip to navigationSkip to topbar
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

1
using System.Web.Mvc;
2
using Twilio.AspNet.Mvc;
3
using Twilio.TwiML;
4
using Twilio.TwiML.Voice;
5
6
namespace IVRPhoneTree.Web.Controllers
7
{
8
public class IVRController : TwilioController
9
{
10
// GET: IVR
11
public ActionResult Index()
12
{
13
return View();
14
}
15
16
// POST: IVR/Welcome
17
[HttpPost]
18
public TwiMLResult Welcome()
19
{
20
var response = new VoiceResponse();
21
var gather = new Gather(action: Url.ActionUri("Show", "Menu"), numDigits: 1);
22
gather.Say("Thank you for calling the E.T. Phone Home Service - the " +
23
"adventurous alien's first choice in intergalactic travel. " +
24
"Press 1 for directions, press 2 to make a call.");
25
response.Append(gather);
26
27
return TwiML(response);
28
}
29
}
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

1
using System.Web.Mvc;
2
using Twilio.AspNet.Mvc;
3
using Twilio.TwiML;
4
using Twilio.TwiML.Voice;
5
6
namespace IVRPhoneTree.Web.Controllers
7
{
8
public class IVRController : TwilioController
9
{
10
// GET: IVR
11
public ActionResult Index()
12
{
13
return View();
14
}
15
16
// POST: IVR/Welcome
17
[HttpPost]
18
public TwiMLResult Welcome()
19
{
20
var response = new VoiceResponse();
21
var gather = new Gather(action: Url.ActionUri("Show", "Menu"), numDigits: 1);
22
gather.Say("Thank you for calling the E.T. Phone Home Service - the " +
23
"adventurous alien's first choice in intergalactic travel. " +
24
"Press 1 for directions, press 2 to make a call.");
25
response.Append(gather);
26
27
return TwiML(response);
28
}
29
}
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

1
using System;
2
using System.Collections.Generic;
3
using System.Web.Mvc;
4
using Twilio.AspNet.Mvc;
5
using Twilio.TwiML;
6
using Twilio.TwiML.Voice;
7
8
namespace IVRPhoneTree.Web.Controllers
9
{
10
public class MenuController : ControllerBase
11
{
12
// POST: Menu/Show
13
[HttpPost]
14
public ActionResult Show(string digits)
15
{
16
var selectedOption = digits;
17
var optionActions = new Dictionary<string, Func<ActionResult>>()
18
{
19
{"1", ReturnInstructions},
20
{"2", Planets}
21
};
22
23
return optionActions.ContainsKey(selectedOption) ?
24
optionActions[selectedOption]() :
25
RedirectWelcome();
26
}
27
28
private TwiMLResult ReturnInstructions()
29
{
30
var response = new VoiceResponse();
31
response.Say("To get to your extraction point, get on your bike and go down " +
32
"the street. Then Left down an alley. Avoid the police cars. Turn left " +
33
"into an unfinished housing development. Fly over the roadblock. Go " +
34
"passed the moon. Soon after you will see your mother ship.",
35
voice: Say.VoiceEnum.PollyAmy, language: "en-GB");
36
37
response.Say("Thank you for calling the E.T. Phone Home Service - the " +
38
"adventurous alien's first choice in intergalactic travel. Good bye.");
39
40
response.Hangup();
41
42
return TwiML(response);
43
}
44
45
private TwiMLResult Planets()
46
{
47
var response = new VoiceResponse();
48
var gather = new Gather(action: Url.ActionUri("Interconnect", "PhoneExchange"), numDigits: 1);
49
gather.Say("To call the planet Broh doe As O G, press 2. To call the planet " +
50
"DuhGo bah, press 3. To call an oober asteroid to your location, press 4. To " +
51
"go back to the main menu, press the star key ",
52
voice: Say.VoiceEnum.PollyAmy, language: "en-GB", loop: 3);
53
response.Append(gather);
54
55
return TwiML(response);
56
}
57
}
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

1
using System.Collections.Generic;
2
using System.Web.Mvc;
3
using Twilio.AspNet.Mvc;
4
using Twilio.TwiML;
5
6
namespace IVRPhoneTree.Web.Controllers
7
{
8
public class PhoneExchangeController : ControllerBase
9
{
10
// POST: PhoneExchange/Interconnect
11
[HttpPost]
12
public ActionResult Interconnect(string digits)
13
{
14
var userOption = digits;
15
var optionPhones = new Dictionary<string, string>
16
{
17
{"2", "+19295566487"},
18
{"3", "+17262043675"},
19
{"4", "+16513582243"}
20
};
21
22
return optionPhones.ContainsKey(userOption)
23
? Dial(optionPhones[userOption]) : RedirectWelcome();
24
}
25
26
private TwiMLResult Dial(string phoneNumber)
27
{
28
var response = new VoiceResponse();
29
response.Dial(phoneNumber);
30
31
return TwiML(response);
32
}
33
}
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.

Need some help?

Terms of service

Copyright © 2024 Twilio Inc.