Articles Blog

How to Make and Receive Phone Calls using C# and ASP.NET

How to Make and Receive Phone Calls using C# and ASP.NET


This C# Voice Quickstart covers both inbound and outbound phone calls. We’re going to use a Guide you can find in the Twilio Docs. There are links down in the description if you want to follow along. For everything we’re going to do we need a phone number so let’s get one. We’ll start in the Phone number console and I’ll search for a 267 area code number and I’ll buy the first Philadelphia area number I find. I’ll click Buy This Number, and then Setup Number. We’ll come back to phone number setup later. Let’s start by talking about outbound calls. Your app makes a POST request to Twilio. Twilio places a call to your phone. When the call is connected, Twilio requests some TwiML instructions from your application. If the TwiML looks like this, then Twilio speaks some text back to your phone. Let’s write some code to make that work. In Visual Studio 2017 I will create a new ASP.NET web application targeting .NET Framework 4.5.2 using the MVC template. Once that’s done being created we will use the NuGet Package Manager to Install-Package Twilio that’s the REST API helper library for C#. And then we’ll also Install-Package Twilio.AspNet.MVC. That provides some helpers for our ASP.NET application. Next, we’ll create a new Empty MVC 5 Controller and we’ll name it PhoneController. This is gonna hold the logic for our Twilio application. Inside of here we’ll declare our dependencies. We gonna use System.Configuration to pull in AppSettings and then I’ll pull in all of the Twilio namespaces that we’ll be using for our application. I’ll change the type of Controller to TwilioController. That provides some helpers for returning TwiML for our Inbound case, but first let’s make an Outbound phone call. I’ll declare an Action Method called MakeCall. Inside of here we need some credentials. Namely, the Account SID and Auth Token from our Twilio account. You can find those inside of the Twilio Console. These look like this but I have mine stashed for safekeeping inside of my Web.config in the AppSettings section. We can use the ConfigurationManager.AppSettings to look those up. So, I’ll grab the TwilioAccountSid and then we’ll do the same thing for the TwilioAuthToken. Then we can take those credentials and initialize the Twilio REST client passing them into the Init method. Next we’re gonna need some phone numbers. The `to` phone number is going to be my cell phone. Phone numbers look like this when you work with them in Twilio but I have my phone number stashed away in AppSettings so I don’t have to share it with you. So we’ll pull it from there. The `from` phone number is going to be the Twilio phone number that we purchased earlier in the Phone Numbers Console so I’ll paste that in there. Now that we have our phone numbers we can create a call using the CallResource.Create method. We’ll pass in the `to` number and the `from` number and then a `url` that Twilio will request some TwiML instructions from when the phone call is connected. That’s gonna be http://demo.twilio.com/docs/voice.xml We’ll look at that in a bit. Then we’ll return the call’s ID so that we know the call was connected. We’ll run our server and hop over to the browser and once it’s up and running we’ll go to /phone/makecall and a call will be placed to my cellphone. Robot voice: Thanks for trying our documentation Enjoy! ♪ ♪ Rickroll ♪ ♪ ♪ ♪ Rickroll ♪ ♪ ♪ ♪ Rickroll ♪ ♪ Thanks Twilio. Who doesn’t love a Rickroll? Let’s check out the TwiML to see how that happened. There’s athat does text-to-speech and reads out “Thanks for trying our documentation. Enjoy!” and then atag that plays that lovely classic Rickroll MP3. Now let’s talk about inbound calls. A user places a phone call to your Twilio number. Twilio makes an HTTP request to your app which returns some TwiML and if the TwiML looks like this, then Twilio speaks some text back to the user. Let’s write the server code to make that happen. We’ll spin up our server.js file and Back in our PhoneController we’ll create an HttpPost route with an Action Method called ReceiveCall(). This is what Twilio is gonna reach out to with an HTTP request so we’ll create a VoiceResponse to respond to that request. Inside of the response we’llusing text-to-speech “Hello from your pals at Twilio. Have fun!” Then, we’ll return the TwiML representation (it’s just XML) of that response. And we’ll run our server. There’s one problem with our server. It’s running on localhost and we want Twilio to be able to reach it but it can’t reach localhost. So we’re gonna use ngrok to create an HTTP tunnel. There’s a nice extension inside of Visual Studio that you can add to make this easier. You’ll go to Tools->Start ngrok tunnel… and it’ll create an HTTP tunnel with a publicly addressable URL to the port that’s running on localhost. We’ll take that URL back over to the Phone Number console and paste it into the “A call comes in” webhook and add /phone/receivecall. Then I can take my phone back out and place a call back to our Twilio phone number to hear the response. Robot voice: “Hello from your pals at Twilio! Have fun. We will have fun because now we know how to make and receive phone calls using Twilio and C#. That’s gonna do it for this video. Thank you so much for watching. If you enjoyed this, please click or tap the Twilio logo in the bottom left to subscribe so you know when we make more. If you wanna get the code there’s a button in the bottom right for that. If you want to watch another video, I picked one just for you Until next time, I’m outta here. ♪♪♪ music ♪♪♪ ♪♪♪ music ♪♪♪

7 thoughts on “How to Make and Receive Phone Calls using C# and ASP.NET”

  1. Hi!
    Could you please tell me if there is a way to provide toll free service to indian customers. Right now twilio numbers are not available in India. Is there a way to buy a toll free international number?

  2. Glad i had the mind to check your youtube channel doing it from the docs on my own i had it all wrong working but this order seems to be what i needed to see Thank you for the tutorial

  3. I keep getting this error that says "The source phone number provided, ***********, is not yet verified for your account. You may only make calls from phone numbers that you've verified or purchased from Twilio" and I followed exactly what you did and have the number bought in Twilio.

  4. How can I do like this, but with a friend like Facebook, WhatsApp and viper but with the camera video Connect

  5. thank you for such a informative tutorial and i am facing an issue when call from android mobile browser automatically loudspeaker on when call connect i want to change and connect call on mouthpiece so my call privacy can maintain and i also post this problem on twilio website may be i am missing some parameters that's why this happening please help me if you know anything

Leave a Reply

Your email address will not be published. Required fields are marked *