TwilioSharp - Get started with Twilio quickly

  • ASP.Net MVC2
  • Visual Studio 2010
  • .NET 4

Make an outbound call


        public ActionResult PlaceCall()
        {
            var twilioApi = new TwilioApi();
            twilioApi.PlaceCall(
                Request.Form["PhoneNumber"], 
                new Uri(String.Format("{0}://{1}/{2}", Request.Url.Scheme,Request.Url.Authority,"/TwilioListener/Voice/OutBoundCallInstructions")));
            return RedirectToAction("Index");
        }

Give Twilio instructions on what to do once connected


        [TwilioFilter]
        public void OutBoundCallInstructions()
        {
            var output = new ResponseType()
                             {
                                 Items = new object[]
                                             {

                                                 new SayVerb() {Value = "hello world"},
                                                 new PlayVerb() {Value = "http://demo.twilio.com/hellomonkey/monkey.mp3"},
                                                 new GatherVerb() {
                                                     numDigits = 1,
                                                     action = "StopCallingMe",
                                                     method = MethodValues.POST,
                                                     Items = new object[]
                                                                 {
                                                                 new SayVerb(){Value="To not be called again, press 1."}
                                                                 }
                                                 }


                                             }

                             }.ToXml();
            Response.Write(output);
        }

Respond to user actions

        [TwilioFilter]
        public void StopCallingMe()
        {
            if (int.Parse(Request["Digits"]) != 1) return;
            
            var callRequest = (CallRequest) this.HttpContext.Items["CallRequest"];
            var output = new ResponseType()
                             {
                                 Items = new object[]
                                             {
                                                 new SayVerb()
                                                     {
                                                         Value =
                                                             "you asked to not be called again at " + callRequest.Called +
                                                             ", we won't thanks."
                                                     }
                                             }
                             }.ToXml();
            Response.Write(output);
        }

Get Started

  1. Sign up for Twilio
  2. Download source
  3. Edit web config file with details from Twilio Account screen (see below)
  4. Compile and deploy to webserver & relax


    <add key="Twilio.AccountSid" value=""/>
    <add key="Twilio.AuthToken" value=""/>
    <add key="Twilio.OutboundNumber" value=""/>

Last edited Aug 17, 2010 at 2:27 PM by simnova, version 4