Quantcast
Channel: Clarity Blogs » git
Viewing all articles
Browse latest Browse all 2

Roll your own Text Messaging Apps with Twilio and MVC 3

$
0
0

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.

How Twilio’s TwiML API Works

See the Twilio How It Works page for more info

  1. An SMS comes in to one of your Twilio Phone Numbers.
  2. Twilio Makes a POST or GET call to a URL you set up.
  3. 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

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.

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();
    }

}

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 that acts as your Response Factory method; this makes building complex responses easier by allowing for in line fluent declarations.

Full Fledged Example – 8 Ball Answerizer 3000

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.

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());
    }
}

(The Magical 8-Ball Answerizer 3000 on AppHarbor)

Now PlayingTommy Tutone – 867-5309 / Jenny


Viewing all articles
Browse latest Browse all 2

Trending Articles