How to Build a Conference Line with Twilio, ASP.NET Core, and C#

March 27, 2020
Written by

How to Build a Conference Line with Twilio and C#

Another conference call, another app, another PIN, another log-in. Joining conference calls should be as simple as dialing a phone number, without needing to enter random conference IDs. In this post, we will walk through how you can build a conference line that anyone can join, using Twilio with C# and ASP.NET Core.

Developer Environment Setup

Let's make sure you have the software you need to build this conference line. For this, you will need:

Create the ASP.NET Core Project

mkdir C:\Code\Conference & cd C:\Code\Conference
dotnet new webapi
dotnet add package Twilio
code .
exit

It will create an ASP.NET Core Web API project, open it in Visual Studio Code, add the Twilio helper library, and close your command window.

If you're installing the .NET Core SDK for the first time, it is recommended that you run the below command:

dotnet dev-certs https --trust

It creates a self signed certificate as various default ASP.NET Core project templates include redirects for all HTTP calls to use HTTPS.

Create a Route to Receive the Incoming Call

Next you'll create an API route by adding a new controller to the Controllers directory. With your project open in Visual Studio Code, create a new file in the Controllers directory named CallController.cs. Copy the below code into that file:

using Microsoft.AspNetCore.Mvc;
using Twilio.TwiML;
using Twilio.TwiML.Voice;

namespace Conference.Controllers
{
    [ApiController]
    [Route("api/[controller]")]
    public class CallController : ControllerBase
    {
        [HttpGet,HttpPost]
        public IActionResult Post()
        {
            var response = new VoiceResponse();
            var say = new Say("Please wait while we dial you into the call.");
            var dial = new Dial().Conference("my-conference-room");
            response.Append(say).Append(dial);

            return Content(response.ToString(), "application/xml");
        }
    }
}

There are a few things happening here:

  • The [Route("api/[controller]")] creates the URL path `https://localhost:5001/api/call`
  • The [HttpGet,HttpPost] attribute sets up the Post() method to execute whenever HTTP Post requests are issued to the URL
  • It creates a VoiceResponse containing a Say verb and a Dial verb that adds the caller to a conference room named `my-conference-room`
  • Finally it returns the VoiceResponse object as XML representing valid TwiML that Twilio understands and can interpret.

Running Your App

With all of this in place, it's time to run the app. In the VS Code terminal window, type dotnet run. This will both compile your application and run it on ports 5000 & 5001.

For Twilio to reach your application, you can either deploy this code or run ngrok, which will provide a URL that allows Twilio to call into your local machine. In a new terminal window, type ngrok http https://localhost:5001. This should give you a Forwarding URL to use in Twilio's console:

Ngrok

Before we update the Twilio console, open a browser window and visit your ngrok URL/api/call. For instance, using the above url - you would visit https://e698b5b3.ngrok.io/api/call and should see:

<?xml version="1.0" encoding="utf-8"?>
<Response>
    <Say>Please wait while we dial you into the call.</Say>
    <Dial>
        <Conference>my-conference-room</Conference>
    </Dial>
</Response>

Connect Your ASP.NET Core App to Twilio

With your app running and an ngrok URL, head over to the Twilio console where you can purchase and configure a phone number. After you purchase a phone number, head to the Voice & Fax area of the console and update the incoming webhook to point to your ngrok url as in the photo below:

Twilio Phone Numbers Console

Give It a Try

Now that you know how to create a basic conference call, try building off of it with other features from our Conference TwiML docs such as moderating a conference call, changing the wait music, or perhaps the entry voice. If you want to explore more of what Twilio has to offer, definitely check out TwilioQuest where you can learn to unlock the power of communications with code. Feel free to reach out and share what you build. I can't wait to see what you build.