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.

C#

1

2

3

4

5

usingTwilio;

var capability=newTwilioCapability(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:

C#

1

2

3

4

5

6

7

8

9

10

11

12

usingTwilio;

usingTwilio.TwiML;

publicclassPhoneController:Controller

{

publicWelcome()

{

varresponse=newTwilioResponse();

response.Say("Welcome Monkey");

returnnewTwiMLResult(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:

C#

1

2

3

4

5

6

7

8

9

10

11

12

usingTwilio;

usingTwilio.TwiML;

publicclassPhoneController:TwilioController

{

publicActionResultWelcome()

{

varresponse=newTwilioResponse();

response.Say("Welcome Monkey");

returnTwiML(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:

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

1

2

3

4

5

6

7

8

9

10

11

12

using Twilio;

using Twilio.TwiML;

using Twilio.TwiML.Mvc;

publicclassPhoneController:TwilioController

{

[ValidateRequest("AUTHTOKENXXXXXXXXXXXXXXXXXXX")]

publicActionResultLookupCaller(stringFrom,stringTo,stringCallSid)

{

...

}

}

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:

C#

1

2

3

4

5

6

7

8

9

@usingTwilio.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.

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