I've been messing around with Twilio and their awesome set of TwiML API's this weekend and made a fun little C# Twilio library for creating simple text (and voice) based applications from MVC 3 sites.
(See Twilio's own how it works page for a way better explanation.)
Using TwilioSharp to Send TwiML Responses From MVC
As part of my handy dandy TwilioSharp helper library I've created a base TwiML Controller for easily creating TwiML Responses with a Fluent TwiMLBuilder Class.
The TwiML Controller exposes a TwiML method that is meant to emulate the ease of use of the Json method available in all MVC Controllers. The TwiML method takes a Func<TwiMLBuilder, TwiMLBuilder> that acts as your Response Factory method; this makes building complex responses easier by allowing for in line fluent declarations.
Full Fledged Example
You can view a full fledged Magic 8-Ball Answerizer 3000 example on GitHub (which is also live on AppHarbor until I run out of money in my Twilio Account). Including a more in depth example of using the Fluent TwiMLBuilder for answering phone calls.
How Twilio's TwiML API Works
An SMS comes in to one of your Twilio Phone Numbers.
Twilio Makes a POST or GET call to a URL you set up.
Your site provides a TwiML Response that Twilio parses and executes.
(See Twilio's own how it works page for a way better explanation.)
Using TwilioSharp to Send TwiML Responses From MVC
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Web.Mvc; | |
using _8Ball.Common; | |
using TwilioSharp.MVC3.Controllers; | |
using TwilioSharp.Request; | |
public class TextController : TwiMLController | |
{ | |
[HttpPost] | |
public ActionResult New(TextRequest request) | |
{ | |
var answer = "The Magical 8-Ball Says: " + Magic8BallAnswerizer3000.GetAnswer(); | |
return TwiML(response => response | |
.Sms(answer)); | |
// Alternatively: | |
//return TwiMLBuilder | |
// .Build() | |
// .Sms(answer) | |
// .ToTwiMLResult(); | |
} | |
} |
As part of my handy dandy TwilioSharp helper library I've created a base TwiML Controller for easily creating TwiML Responses with a Fluent TwiMLBuilder Class.
The TwiML Controller exposes a TwiML method that is meant to emulate the ease of use of the Json method available in all MVC Controllers. The TwiML method takes a Func<TwiMLBuilder, TwiMLBuilder> that acts as your Response Factory method; this makes building complex responses easier by allowing for in line fluent declarations.
Full Fledged Example
You can view a full fledged Magic 8-Ball Answerizer 3000 example on GitHub (which is also live on AppHarbor until I run out of money in my Twilio Account). Including a more in depth example of using the Fluent TwiMLBuilder for answering phone calls.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Web.Mvc; | |
using _8Ball.Common; | |
using TwilioSharp.MVC3.Controllers; | |
using TwilioSharp.Request; | |
public class CallController : TwiMLController | |
{ | |
[HttpPost] | |
public ActionResult New(CallRequest request) | |
{ | |
return TwiML(response => response | |
.Say("Thanks for calling the All Knowing Magical 8 Ball.") | |
.Say("Ask a Question after the Beep.") | |
.Say("Press Pound when done.") | |
.Record(Url.Action("Question"))); | |
} | |
[HttpPost] | |
public ActionResult Question(CallRecordRequest request) | |
{ | |
return TwiML(response => response | |
.Say("The Magical 8 Ball Says") | |
.Say(Magic8BallAnswerizer3000.GetAnswer()) | |
.Pause(1) | |
.GatherWhileSaying("Press Any Key To Ask Another Question. Or Pound to Exit.", | |
actionUrl: Url.Action("New"), | |
timeoutSeconds: 3) | |
.Say("Goodbye") | |
.Hangup()); | |
} | |
} |
No comments:
Post a Comment