This is the Test.cs as of 25 Jan 2010 (the exact same file you'd download as the current 0.0.2 release).
A similar file is included in the latest source code package, as well as many other examples and samples.

This is it. This is how TSL.NET looks and what you will be able to use to create your own apps.
More tests will be added and some refinements to the library but this is what you'll get in the library.

I'd appreciate it if people familiar with TwiML would verify the TwiML and comment on the mechanisms shown here.

using System;

// Tests v0.0.2 Alpha
// 25 Jan 2010
// Tony Gravagno
// Nebula Research and Development

/*
 * Changes from v0.0.1 to v0.0.2
 * - Added methods here.Now testing all verbs.
 * - All methods now return formatted XML.
 * - Added expected output in each test method.
 *      Actually generated during testing. Each method is now a real HowTo.
 * - All methods to-date function as expected. Many fixes and enhancements since 0.0.1.
 * - More classes and members documented with XML for IntelliSense.
 * - Added many helper methods and overloads to Response class for easier coding.
 */

namespace NebulaRnD.TwilioServer
{
    /*
     * These tests exercise various aspects of each class.
     * Some tests will test one constructor and other tests will test others.
     * The goal is to test all constructors and methods and to demonstrate
     * the versatility of the library.
     * 
     * The tests are not intended to process actual telephone calls but
     * to ensure that the library is allowing for proper construction of
     * verbs and rendering proper XML markup for execution.  Many objects
     * are created but never used.  The TwiML resulting from many of these
     * tests would not make sense in a live environment.  Again, the goal
     * is to ensure proper XML, not logical flow as in an application.
     * 
     * Check the Xml property on objects to see what Xml they generate.
     * Many tests create a Response  object and then render all components
     * with the Respond or Render method.
     * 
     * Please feel free to add new object instances and method invocations
     * to test specific features - and report any failures and fixes so that
     * we can incorporate all changes into the baseline for everyone!
     */

    /// <summary>
    /// See http://www.twilio.com/docs/api/2008-08-01/twiml/conference for examples
    /// that are implemented in this class    
    /// </summary>
    public static class TestConference
    {
        /// <summary>
        /// Simple Conference
        /// </summary>
        public static string Example1()
        {
            Response r = new Response();
            r.Append(new Dial(new Conference("1234")));
            r.Respond();
            return r.Xml;
            //<Response version="2008-08-01">
            //  <Dial>
            //    <Conference>1234</Conference>
            //  </Dial>
            //</Response>
        }

        /// <summary>
        /// Moderated conference
        /// </summary>
        /// <returns></returns>
        public static string Example2()
        {
            Response r = new Response();
            r.Append(
                new Dial(
                    new Conference("1234" ,
                        new ConferenceStartOnEnter(false)
                        )
                    )
                    );
            r.Respond(); // Note Render is same as Respond
            return r.Xml;
            //<Response version="2008-08-01">
            //  <Dial>
            //    <Conference startConferenceOnEnter="false">1234</Conference>
            //  </Dial>
            //</Response>
        }
        /// <summary>
        /// Join a conference muted
        /// </summary>
        public static string Example3()
        {
            Response r = new Response();
            r.Append(
                new Dial(
                    new Conference("SimpleRoom" ,
                        new ConferenceMuted(true)
                        )
                    )
                    );
            r.Respond();
            return r.Xml;
            //<Response version="2008-08-01">
            //  <Dial>
            //    <Conference muted="true">SimpleRoom</Conference>
            //  </Dial>
            //</Response>
        }
        /// <summary>
        /// Bridging calls
        /// </summary>
        public static string Example4()
        {
            Response r = new Response();
            r.Append(
                new Dial(
                    new Conference("NoMusicNoBeepRoom" ,
                        new ConferenceBeep(false) ,
                        new ConferenceStartOnEnter(true) ,
                        new ConferenceEndOnExit(true)
                        )
                    )
                    );
            r.Respond(); // Note Render is same as Respond
            return r.Xml;
            //<Response version="2008-08-01">
            //  <Dial>
            //    <Conference beep="false" startConferenceOnEnter="true" endConferenceOnExit="true">NoMusicNoBeepRoom</Conference>
            //  </Dial>
            //</Response>
        }
        /// <summary>
        /// Combining with Dial attributes. Exception expected and handled.
        /// </summary>
        public static string Example5()
        {
            Response r = new Response();
            try
            {
                // this is going to throw exception because a simple filename
                // is not a valid URL. However, we can tell the DialAction
                // constructor to avoid the URI validation.
                r.Append(
                    new Dial(
                        new Conference("LoveTwilio") ,
                        new DialAction("handleLeaveConference.aspx") ,
                        new DialMethod(HttpMethod.Post) ,
                        new DialHangupOnStar(true) ,
                        new DialTimeLimit(30)
                        )
                        );
            }
            catch ( TwilioException ex )
            {
                // be prepared to handle exceptions from every new object instantiation and operation
            }
            catch ( Exception ex )
            {
                // "unexpected" exceptions handled here.

                // In this case DialAction doesn't recognize a filename as a valid URI
                // and throws an exception.  In the constructor below, the DialAction
                // gets OverrideValidation set to true to avoid URI checking.
                // Would be nice to have a better way to handle this specific case.

                // Retry the DialAction - should work this time
                r.Append(
                  new Dial(
                      new Conference("LoveTwilio") ,
                      new DialAction("handleLeaveConference.aspx" , true) ,
                      new DialMethod(HttpMethod.Post) ,
                      new DialHangupOnStar(true) ,
                      new DialTimeLimit(30)
                      )
                      );
            }
            r.Respond(); // Note Render is same as Respond
            return r.Xml;
            //<Response version="2008-08-01">
            //  <Dial action="handleLeaveConference.aspx" method="POST" hangupOnStar="true" timeLimit="30">
            //    <Conference>LoveTwilio</Conference>
            //  </Dial>
            //</Response>
        }
    }

    public static class TestDial
    {
        /// <summary>
        /// Dial with many parameters
        /// </summary>
        public static string Test1()
        {
            try
            {
                Response r = new Response();
                Dial dial = new Dial("4155551212" ,
                    new DialAction("http://myserver.com/twilio/handler") ,
                    new DialMethod(HttpMethod.Post) ,
                    new DialTimeLimit(120) ,
                    new DialTimeout(20) ,
                    new DialHangupOnStar(false) ,
                    new DialCallerID("1234567890")
                    );
                r.Append(dial);
                r.Render(); // Note Render is same as Respond
                return r.Xml;
                //<Response version="2008-08-01">
                //  <Dial action="http://myserver.com/twilio/handler" method="POST"
                //        timeLimit="120" timeout="20" hangupOnStar="false"
                //        callerId="1234567890">4155551212</Dial>
                //</Response>
            }
            catch ( TwilioException ex )
            {
                // be prepared to handle exceptions from every new object instantiation and operation
            }
            catch ( Exception ex )
            {
                // unexpected exceptions
            }
            return "";
        }

        /// <summary>
        /// Dial using Number plus parameters
        /// </summary>
        public static string Test2()
        {
            try
            {
                Response r = new Response();
                Dial dial = new Dial(
                    new Number("1234567890" ,
                        new NumberSendDigits("ww123ww456") ,
                        new NumberUrl("http://myserver/twilio/play_to_called.aspx")) ,
                    new DialAction("http://myserver/twilio/do_this.aspx"));
                r.Append(dial);
                r.Render();
                return r.Xml;
                //<Response version="2008-08-01">
                //  <Dial action="http://myserver/twilio/do_this.aspx">
                //    <Number sendDigits="ww123ww456"
                //            url="http://myserver/twilio/play_to_called.aspx">1234567890</Number>
                //  </Dial>
                //</Response>
            }
            catch ( TwilioException ex )
            {
                // be prepared to handle exceptions from every new object instantiation and operation
            }
            catch ( Exception ex )
            {
                // unexpected exceptions
            }
            return "";
        }

        /// <summary>
        /// Use AddNumber on Dial, then Append Dial to Response
        /// </summary>
        public static string Test3()
        {
            try
            {
                Response r = new Response();
                Dial dial = new Dial();
                dial.AddNumber("9876543210" ,
                    new NumberUrl("http://foo"));
                r = new Response();
                r.Append(dial);
                r.Render();
                return r.Xml;
                //<Response version="2008-08-01">
                //  <Dial>
                //    <Number url="http://foo">9876543210</Number>
                //  </Dial>
                //</Response>
            }
            catch ( TwilioException ex )
            {
                // be prepared to handle exceptions from every new object instantiation and operation
            }
            catch ( Exception ex )
            {
                // unexpected exceptions
            }
            return "";
        }

        /// <summary>
        /// Add Conference to existing Dial, then Append Dial to Response
        /// </summary>
        /// <returns></returns>
        public static string Test4()
        {
            try
            {
                Response r = new Response();
                Dial dial = new Dial(); // named conference room added to existing Dial object
                dial.AddConference("Product Scheduling" ,
                    new ConferenceBeep() ,
                    new ConferenceMuted(false));
                r.Append(dial);
                r.Render();
                return r.Xml;
                //<Response version="2008-08-01">
                //  <Dial>
                //    <Conference beep="true" muted="false">Product Scheduling</Conference>
                //  </Dial>
                //</Response>
            }
            catch ( TwilioException ex )
            {
                // be prepared to handle exceptions from every new object instantiation and operation
            }
            catch ( Exception ex )
            {
                // unexpected exceptions
            }
            return "";
        }

        /// <summary>
        /// Try ConferenceWaitURL
        /// </summary>
        public static string Test5()
        {
            try
            {
                Response r = new Response();
                Dial dial = new Dial(); // Create a conference for a room number rather than name
                dial.AddConference(123 , new ConferenceWaitURL("http://ourserver.com/muzac.wav"));
                r.Append(dial);
                r.Render();
                return r.Xml;
                //<Response version="2008-08-01">
                //  <Dial>
                //    <Conference waitURL="http://ourserver.com/muzac.wav">123</Conference>
                //  </Dial>
                //</Response>
            }
            catch ( TwilioException ex )
            {
                // be prepared to handle exceptions from every new object instantiation and operation
            }
            catch ( Exception ex )
            {
                // unexpected exceptions
            }
            return "";
        }
    }

    public static class TestGather
    {
        /// <summary>
        /// Try Gather with many parameters
        /// </summary>
        public static string Test1()
        {
            try
            {
                Response r = new Response();
                Gather gather = new Gather(
                    new GatherAction("http://myserver/processkeypresses.aspx") ,
                    new GatherFinishOnKey("*") ,
                    new GatherMethod(HttpMethod.Get) ,
                    new GatherNumDigits(5) ,
                    new GatherTimeout(20)
                    );
                r.AddDial("1234567");
                r.Append(gather);
                r.Respond(); // Note Render is same as Respond
                return r.Xml;
                //<Response version="2008-08-01">
                //  <Dial>1234567</Dial>
                //  <Gather action="http://myserver/processkeypresses.aspx" finishOnKey="*" method="GET" numDigits="5" timeout="20" />
                //</Response>
            }
            catch ( TwilioException ex )
            {
                // be prepared to handle exceptions from every new object instantiation and operation
            }
            catch ( Exception ex )
            {
                // unexpected exceptions
            }
            return "";
        }
    }

    public static class TestHangup
    {
        /// <summary>
        /// Call, say something, hangup ... simulates reminder
        /// </summary>
        public static string Test1()
        {
            try
            {
                Response r = new Response();
                Hangup hangup = new Hangup();
                r.AddDial("1234567890" , new DialTimeout(30));
                r.AddSay("Don't forget to do something nice!" ,
                    new SayVoice(Voice.Woman));
                r.Append(hangup);
                r.Render(); // Note Render is same as Respond
                return r.Xml;
                //<Response version="2008-08-01">
                //  <Dial timeout="30">1234567890</Dial>
                //  <Say voice="woman">Don't forget to do something nice!</Say>
                //  <Hangup />
                //</Response>
            }
            catch ( TwilioException ex )
            {
                // be prepared to handle exceptions from every new object instantiation and operation
            }
            catch ( Exception ex )
            {
                // unexpected exceptions
            }
            return "";
        }
    }

    public static class TestNumber
    {
        /// <summary>
        /// Different approach to adding Number to Dial then Dial to Response
        /// </summary>
        public static string Test1()
        {
            try
            {
                Response r = new Response();
                Number number = new Number(
                    "1234567890" , new NumberSendDigits("ww123w12w9"));
                r.Append(new Dial(number , new DialTimeLimit(60)));
                r.Respond();
                return r.Xml;
                //<Response version="2008-08-01">
                //  <Dial timeLimit="60">
                //    <Number sendDigits="ww123w12w9">1234567890</Number>
                //  </Dial>
                //</Response>
            }
            catch ( TwilioException ex )
            {
                // be prepared to handle exceptions from every new object instantiation and operation
            }
            catch ( Exception ex )
            {
                // unexpected exceptions
            }
            return "";
        }

        /// <summary>
        /// More complex call with Number
        /// </summary>
        public static string Test2()
        {
            // While on the phone with party 1, call party 2 at the 222 number.
            // To get to that person through their PBX, wait .5+.5=1 second, press 1,
            // wait again, press 212 ... press 9 to finally connect.
            // When connected, execute the TwiML found at the aspx page.
            // The caller ID party 2 should see is the 987 number, assuming it's valid.
            try
            {
                Response r = new Response();
                Dial dial = new Dial(
                    new Number("2223334444" ,
                        new NumberSendDigits("ww1ww212ww33ww9") ,
                        new NumberUrl("http://myserver/twilio/to_called_party.aspx")
                    ) ,
                    new DialCallerID("9876543210")
                );
                r.Append(dial);
                r.Render();
                return r.Xml;
                //<Response version="2008-08-01">
                //  <Dial callerId="9876543210">
                //    <Number sendDigits="ww1ww212ww33ww9" url="http://myserver/twilio/to_called_party.aspx">2223334444</Number>
                //  </Dial>
                //</Response>
            }
            catch ( TwilioException ex )
            {
                // be prepared to handle exceptions from every new object instantiation and operation
            }
            catch ( Exception ex )
            {
                // unexpected exceptions
            }
            return "";
        }
    }

    public static class TestPause
    {
        /// <summary>
        /// Make two calls with Pause embedded
        /// </summary>
        public static string Test1()
        {
            try
            {
                Response r = new Response();
                r.AddDial("1234567890" , new DialAction("http://myserver/handlecall.aspx"));
                r.AddPause(new PauseLength(2));
                r.Append(new Hangup());
                r.Append(new Dial("2345678901" , null));
                r.Append(new Pause(3));
                r.AddSay("You are being called second." , new SayVoice(Voice.Man));
                r.Render();
                return r.Xml;
                //<Response version="2008-08-01">
                //  <Dial action="http://myserver/handlecall.aspx">1234567890</Dial>
                //  <Pause length="2" />
                //  <Hangup />
                //  <Dial>2345678901</Dial>
                //  <Pause length="3" />
                //  <Say voice="man">You are being called second.</Say>
                //</Response>
            }
            catch ( TwilioException ex )
            {
                // be prepared to handle exceptions from every new object instantiation and operation
            }
            catch ( Exception ex )
            {
                // unexpected exceptions
            }
            return "";
        }
    }

    public static class TestPlay
    {
        /// <summary>
        /// Make a call and Play a WAV file
        /// </summary>
        public static string Test1()
        {
            try
            {
                Response r = new Response();
                r.AddDial("3455677890");
                r.AddPlay("http://myserver/audio/outgoing1.wav" , new PlayLoop(2));
                r.Render();
                return r.Xml;
                //<Response version="2008-08-01">
                //  <Dial>3455677890</Dial>
                //  <Play loop="2">http://myserver/audio/outgoing1.wav</Play>
                //</Response>
            }
            catch ( TwilioException ex )
            {
                // be prepared to handle exceptions from every new object instantiation and operation
            }
            catch ( Exception ex )
            {
                // unexpected exceptions
            }
            return "";
        }
    }

    public static class TestRecord
    {
        /// <summary>
        /// More complex call to get someone's feedback
        /// </summary>
        public static string Test1()
        {
            try
            {
                Response r = new Response();
                r.AddDial("3455677890");
                r.Say("Record your message at the tone.");
                r.AddPlay("beep.mp3");
                r.AddRecord(
                    new RecordFinishOnKey("#*") ,
                    new RecordTimeout(3) ,
                    new RecordAction("process_recording.aspx" , true)
                    );
                r.Render();
                return r.Xml;
                //<Response version="2008-08-01">
                //  <Dial>3455677890</Dial>
                //  <Say>Record your message at the tone.</Say>
                //  <Play>beep.mp3</Play>
                //  <Record finishOnKey="#*" timeout="3" action="process_recording.aspx" />
                //</Response>
            }
            catch ( TwilioException ex )
            {
                // be prepared to handle exceptions from every new object instantiation and operation
            }
            catch ( Exception ex )
            {
                // unexpected exceptions
            }
            return "";
        }
    }

    public static class TestRedirect
    {
        /// <summary>
        /// Redirect and specify alternate HTTP method to call URL
        /// </summary>
        public static string Test1()
        {
            try
            {
                Response r = new Response();
                r.Say("We are transferring to another script.");
                Redirect redirect = new Redirect(
                    "http://myserver/anotherscript.aspx" ,
                    new RedirectMethod(HttpMethod.Get));
                r.Append(redirect);
                r.Render();
                return r.Xml;
                //<Response version="2008-08-01">
                //  <Say>We are transferring to another script.</Say>
                //  <Redirect method="GET">http://myserver/anotherscript.aspx</Redirect>
                //</Response>
            }
            catch ( TwilioException ex )
            {
                // be prepared to handle exceptions from every new object instantiation and operation
            }
            catch ( Exception ex )
            {
                // unexpected exceptions
            }
            return "";
        }
        /// <summary>
        /// Redirect with default HTTP method = Post
        /// </summary>
        public static string Test2()
        {
            try
            {
                Response r = new Response();
                r.Say("We are transferring to another script.");
                r.Redirect("http://myserver/anotherscript.aspx");
                r.Render();
                return r.Xml;
                //<Response version="2008-08-01">
                //  <Say>We are transferring to another script.</Say>
                //  <Redirect>http://myserver/anotherscript.aspx</Redirect>
                //</Response>
            }
            catch ( TwilioException ex )
            {
                // be prepared to handle exceptions from every new object instantiation and operation
            }
            catch ( Exception ex )
            {
                // unexpected exceptions
            }
            return "";
        }
    }

    public static class TestSay
    {
        /// <summary>
        /// Detailed Text-to-Speech with repeat
        /// </summary>
        /// <returns></returns>
        public static string Test1()
        {
            try
            {
                Response r = new Response();
                Say say = new Say("Hello World" ,
                    new SayVoice(Voice.Man) ,
                    new SayLanguage(Language.French) ,
                    new SayLoop(3)
                    );
                r.Append(say);
                r.Render();
                return r.Xml;
                //<Response version="2008-08-01">
                //  <Say voice="man" language="fr" loop="3">Hello World</Say>
                //</Response>
            }
            catch ( TwilioException ex )
            {
                // be prepared to handle exceptions from every new object instantiation and operation
            }
            catch ( Exception ex )
            {
                // unexpected exceptions
            }
            return "";
        }
    }
}

Last edited Mar 28, 2010 at 2:03 AM by TonyGravagno, version 3

Comments

No comments yet.