Parsing an SMS Message from Twilio

The explosion of text messaging is a foregone conclusion, and apps have been catching up to the tidal wave for some time now.  For those of you doing a lot of cloud-based development, you’re probably familiar with Twilio as one of the best / only cloud Voice and SMS messaging providers at least for Windows Azure.  It’s all good though, if you spend any time on their web site you see that they spend an enormous amount of time spinning up developer love to ensure those of who don’t do “that telephony stuff’ for a living can still muddle our way through relatively easily.

One of the cool things that you can do with Twilio is hook into SMS messages in your web application.  That means getting a phone number from Twilio that’s SMS enabled, and then configuring it so that incoming text messages are sent to an endpoint in your web application.  That’s good, simple configuration only.  However what is very difficult to find on the Twilio site (and initially stumped even the fine folks at Twilio Sales) was documentation that describes the payload of that SMS message so you know how to actually use it when it arrives in your web application.  Well while I was waiting for a link to documentation, I ended up just writing a Web API endpoint for it and then examining the payload.  I used that to “normalize” the data into a class, so I thought I would share it here.

Twilio fortunately does use a simple “key=value” nomenclature in the payload so it makes it pretty easy to create a class to do this.  Here’s what your class looks like to map all the attributes in the incoming SMS message:

public class TwilioIncomingSmsMessage

{

public string ToCountry { get; set; }

public string ToState { get; set; }

public string SmsMessageSid { get; set; }

public int NumMedia { get; set; }

public string ToCity { get; set; }

public string FromZip { get; set; }

public string SmsSid { get; set; }

public string FromState { get; set; }

public string SmsStatus { get; set; }

public string FromCity { get; set; }

public string Body { get; set; }

public string FromCountry { get; set; }

public string To { get; set; }

public string ToZip { get; set; }

public int NumSegments { get; set; }

public string MessageSid { get; set; }

public string AccountSid { get; set; }

public string From { get; set; }

public string ApiVersion { get; set; }

}

Web API provides all the awesome magic of mapping the incoming payload to your class – you just need to create a method signature like this (the return type of course is entirely up to you):

public HttpResponseMessage PushButtonSMS(TwilioIncomingSmsMessage msg)

Hope that helps someone who might be wanting to consume incoming SMS messages in your application.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s