An easy way to receive an SMS message with C# and Twilio

September 19, 2016
Written by

abGDePnfoTx4bsSslpAt6wXsVKsaZZcH4jtmcKmU04VNaW6CFJJheJhOTiYUDgoPl7geigbfOk8hw_UNr-GTzhPw0i4JCesZKKmNfoSRob41s_pIvKd7FRRlu1-gDuzQJYaKnEeo

I already showed you how to send an SMS message with C# in 30 seconds. Today I’m going to show you how to receive an SMS message with C# and Twilio. Here’s all the code you’ll need.

using System.Web.Mvc;
using Twilio.TwiML;
using Twilio.TwiML.Mvc;

namespace ReceiveSms.Controllers
{
    public class SmsController : TwilioController
    {
        // POST: Sms/Message
        [HttpPost] 
        public ActionResult Message(string From, string Body)
        {
            var twiml = new TwilioResponse();
            var message = twiml.Message($"Hello {From}. You said {Body}");
            return TwiML(message);
        }
    }
}

If you’d like to see an explanation of how the code above works, watch this short video, or just keep reading.

When someone texts your Twilio number, Twilio makes an HTTP request to your app with details about the SMS passed in the request body. Let’s use use .NET MVC to handle Twilio’s request and send a response.

Let’s make sure we have the necessary libraries in our project by opening up the Package Manager Console in Visual Studio and installing the following Twilio .NET libraries.

Install-Package Twilio
Install-Package Twilio.Mvc
Install-Package Twilio.TwiML

With all our dependencies installed we need to make sure our Controller extends TwilioController as this will make things easier when returning TwiML later.

public class SmsController : TwilioController
{
}

We then create an endpoint called message to accept a POST request with a From telephone number and a message Body.

public class SmsController : TwilioController
{
    // POST: Sms/Message
    [HttpPost] 
    public ActionResult Message(string From, string Body)
    {
       // rest of code will go here
    }
}

When Twilio makes an HTTP request to your app, it expects a response in the form of TwiML, a simple set of XML tags that relay instructions back to Twilio. We will use the libraries we installed to build the response easily.


public class SmsController : TwilioController
{
    // POST: Sms/Message
    [HttpPost] 
    public ActionResult Message(string From, string Body)
    {
        var twiml = new TwilioResponse();
        var message = twiml.Message($"Hello {From}. You said {Body}");
        return TwiML(message);
    }
}

Run the project by clicking the Start button with the green arrow at the top, and you will notice your browser opens up automatically. Make a note of the port the application is running on.

This app needs a publicly accessible URL, so you’ll either need to deploy it to the cloud or use a tool like ngrok to open a tunnel to your local development environment from your command line. We already wrote a blog post showing how to get started with ngrok.

ngrok http 51343 -host-header="localhost:51343"

Ngrok will provide you with a url that points to the application running on your localhost.

Now that we’ve built our application we need to connect it up to a Twilio number so that it can receive SMS messages and make requests to our app.

Setup Twilio

Sign up for a free Twilio account if you don’t have one.

Buy a phone number, then click Setup Number. Scroll to the Messaging section and find the line that says “A Message Comes In.”

twilio dashboard - A Message Comes In.

Fill in the full path to your endpoint (i.e., https://yourserver.com/sms/message) and click Save.

Now, send an SMS to your shiny new phone number number and get stoked when you receive the  customized response you just wrote.

Next Steps

If you’d like to learn more about how to use Twilio and .NET together, check out :


And if you’d like to chat more about it, hit me up on Twitter @marcos_placona or by email on marcos@twilio.com.