Twilio for .NET Developers Part 5: Twilio Client, MVC and WebMatrix Helper Libraries

February 07, 2012
Written by

Twilio Bug Logo

This is a series of blog posts that introduce and walk you through using the different .NET Helper Libraries that Twilio provides.

These libraries simplify using the Twilio REST API for .NET developers, and provide as set of utilities that make it easy to work with TwiML and Twilio Client. Take a look at part one, twothree and four of the series.

Twilio Client Helper, MVC Helper and WebMatrix Helper Libraries

In this post we’ll look at the other .NET helper libraries that you can use to integrate Twilio into your applications.  While in the last two posts we’ve looked at the two primary helper libraries which make it simple to use the Twilio REST API and to generate TwiML, there are three additional libraries that provide even more specialized help for .NET developers.

Twilio.Client

Twilio.Client makes it easy to add VoIP to your web, iOS and Android apps. With it, you can make and receive calls from the browser and mobile apps with a few lines of code.  The Twilio.Client.Capability helper library contains API’s that allow you to generate capability tokens to use with Twilio Client.  Capability tokens help secure the communication between your application and Twilio and allow you control access to various features of Twilio from Twilio.Client.  You can read more about Capability tokens here: http://www.twilio.com/docs/client/capability-tokens

To use the Twilio.Client.Capability helper library, add it as a project reference using NuGet:

Once you’ve added the reference you can create an instance of the TwilioCapability class, passing your accounts Account Sid and Auth Token into the constructor.  The instance of the TwilioCapability class exposes three methods, AllowClientIncoming, AllowClientOutgoing and GenerateToken.

The AllowClientIncoming and AllowClientOutgoing methods allow you to control whether or not the Twilio Client should accept incoming calls, or allow outgoing calls.

using Twilio;
var capability =new TwilioCapability (accountSid, authToken);
capability.AllowClientIncoming;
capability.AllowClientOutgoing(appSid);
token = capability.GenerateToken();

Calling the GenerateToken method causes the TwilioClient class to generate an encoded token string that is used to sign the its communication between the browser and Twilio, and to tell Twilio which options have been set in the capability token.  Once you’ve generated the capability token you can inject this into the Twilio Client Javascript:

MVC Helper

The Twilio.MVC library includes a number of classes that are useful to developers using ASP.NET MVC.

The TwiMLResult class provides a simple way to return TwiML content from a Controller.  The class allows you to provide TwiML from a TwilioResponse object and will ensure that the Controllers HTTP response has its ContentType header is set correctly to application/xml.

To use the TwiMLResult class, create new instance, providing the TwiML to its constructor:

using Twilio;
using Twilio.TwiML;
public class PhoneController : Controller
{
public Welcome()
{
var response = new TwilioResponse();
response.Say("Welcome Monkey");

return new TwiMLResult (response);
}
}

The TwilioController class extends the standard ASP.NET MVC Controller class with a single method named TwiML, which returns a new TwiMLResult.  Using this method provides a shortcut to manually creating the TwiMLResult instance.

To use the TwilioController, simply change the default Controller template to derive from TwilioController:

using Twilio;
using Twilio.TwiML;
public class PhoneController : TwilioController
{
public ActionResult Welcome()
{
var response = new TwilioResponse();
response.Say("Welcome Monkey");
return TwiML(response);
}

}

Finally, the ValidateRequestAttribute class allows you ensure that requests made to this controller are actually coming from Twilio.  To do this, simply mark the controller class or the Action method with this attribute, providing your AuthToken as an attribute parameter, and when requests arrive they will be verified that they are coming from Twilio using the Twilio security rules, which are defined here: http://www.twilio.com/docs/security

Below is an example of using the attribute on the controller class.   In this case a request to any action in the controller will be validated prior to the action method executing:

using Twilio;
using Twilio.TwiML;

[ValidateRequest</span>("AUTHTOKENXXXXXXXXXXXXXXX")]
public class PhoneController : TwilioController
{
public ActionResult LookupCaller(string From, string To, string CallSid)
{
var response = new TwilioResponse();

var caller = Caller.Locate(From);
if (caller == null)
{
response.Say("Sorry.  We don't recognize you.");
}
>else
{
response.Say(<span style="color: #800000;">"Welcome back Monkey"</span>);
}
return TwiML(response);
}
}

Also, as mentioned above, instead of an entire controller, you can mark specific action methods within a controller with the validation attribute:

using Twilio;
using Twilio.TwiML;
using Twilio.TwiML.Mvc;

public class PhoneController :TwilioController
{
[ValidateRequest("AUTHTOKENXXXXXXXXXXXXXXXXXXX")]
public ActionResult LookupCaller(string From, string To, string CallSid)
{
...
}
}

WebMatrix Helper

Finally, Twilio publishes a helper library that contains several a WebMatrix Helper that developers using the new WebMatrix development environment can use.   The Helper library contains methods for easily sending SMS messages or initiating outbound calls.  Below is an example of using the Helper to sending an SMS message using the C#/Razor syntax:

@using Twilio.WebMatrix;
@{
if (IsPost) {
Twilio.AccountSid="ACXXXXXXXXXXXXXXXXXXXXXXXXXX";
>Twilio.AuthToken = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";

Twilio.SendSms("+1XXXXXXXXXX", "+1YYYYYYYYYY", Request["body"], "");
}
}

Send a SMS Message

Over the last several posts we have looked at all of the helper libraries, in the next post we will look at how you can create an application that uses these helper libraries to create multi-step flows for both SMS message conversations and voice call conversations.

Read more of the .NET series:
Twilio for .NET Developers Part 1: Introducing the Helper Libraries
Twilio for .NET Developers Part 2: Adding Twilio Helper Libraries to your Project
Twilio for .NET Developers Part 3: Using the Twilio REST API Helper Library
Twilio for .NET Developers Part 4: Using the Twilio.TwiML Helper Library

This series is written by Twilio Developer Evangelist Devin Rader. As a co-founder of the St. Louis .NET User Group, a current board member of the Central New Jersey .NET User Group and a former INETA board member, he’s an active supporter of the .NET developer community. He’s also the co-author or technical editor of numerous books on .NET including Wrox’s Professional Silverlight 4 and Wrox’s Professional ASP.NET 4. Follow Devin on Twitter @devinrader