We’re fortunate at Twilio to have an amazing community of developers that openly share their work for the rest of the
community to benefit from. One such developer is Tony Gravagno, President of Nebula Research and Development. Tony does a lot of work with
.NET and wanted to simplify working with the Twilio APIs. Existing open source options were incomplete so Tony started work
on Twilio Server Library (or TSL.NET for short).
TSL.NET covers both major use cases for working with Twilio: the REST API and TwiML responses. Lets take a look at a couple
examples of how the library works.
With complete coverage of the Twilio REST API, TSL.NET makes sending a text message as easy as two lines of code:
var twilio = new TwilioRestClient("AC12345", "authTokenGoesHere"); var message = twilio.SendSMS("555-111-2222", "555-333-4444", "SMS from TSL.NET", "http://example.com/callback");
You can also easily initiate a call:
var twilio = new TwilioRestClient("AC12345", "authTokenGoesHere"); var call = twilio.PlaceCall("555-111-2222", "555-333-4444", "http://example.com/call"); var callSid = call.Sid;
That’s just the beginning of what the TwilioRestClient supports. You can read more about the other features here.
TSL.NET originally started out focusing on TwiML generation inspired the PHP Helper Library. These libraries
are designed to make it easy to dynamically build TwiML from code. Let’s take a look at a simple example.
Let’s say we need some TwiML for a simple voicemail app:
<?xml version="1.0" encoding="UTF-8" ?> <Response> <Say>Hello, please leave a message.</Say> <Record action="record.aspx" maxLength="30" /> </Response>
Generating this can be accomplished with the following C#:
var response = new Response(); response.AddSay("Hello, please leave a message."); response.AddRecord(new RecordAction("record.aspx"), new RecordMaxLength(30)); response.Respond(); var xml = response.Xml;
If you’re using Webforms, you can return that string using Response.Write from your generic handler or codebehind (just make sure
you set the Response.ContentType = “application/xml”). If you’re using ASP.NET MVC, you can return the XML from a controller action.
Nesting of verbs is also supported. For instance to nest a <Say> verb inside a <Gather<>
var response = new Response(); var gather = new Gather(new GatherAction("digits.aspx"), new GatherNumDigits(5)); var say = new Say("Hello, please enter your five digit zip code."); gather.Append(say); response.Append(gather); response.Respond(); var xml = response.Xml;
Tony has done a great job of providing complete coverage for both the REST API and TwiML making it extremely easy to get started
building your Twilio-based application, or integrating voice and SMS functionality into existing .NET applications. To learn more about the project, visit the project home page on CodePlex, check out the documentation or download the latest version and play with some of the included examples.