Twilio SMS Messages In Non-web .net Application with Twilio REST API (c#)

cell-phoneTwilio is a great SMS (text message) gateway to use for sending and receiving SMS messages. The process is super simple and flexible to get started using (the Twilio REST API helper library is great). However, in my case, I wanted to access the API through a “windows form” application, but the API needs a little something extra to get this to function. In my experience, a proxy. Below are the steps from start to finish to retrieve messages from your Twilio account:

1. Get a Twilio account.
2. In Visual Studio – run this in Package Manager Console: PM> Install-Package Twilio
3. Use the sample code below to get some of your SMS messages:

using System;
using System.Net;
using Twilio;

namespace testSMS
{
    internal class getSMS
    {
        public void parseSMS()
        {

            var twilio = new TwilioRestClient("yourSID", "yourToken");
            //The below is deeded to remove the json error:
            //"Unexpected character encountered while parsing value: <. Path '', line 0, position 0."

            WebRequest wreq = WebRequest.Create("https://api.twilio.com/");
            IWebProxy iprox = wreq.Proxy;
            iprox.Credentials = CredentialCache.DefaultCredentials;
            twilio.Proxy = iprox;

            SmsMessageResult msgs = twilio.ListSmsMessages();

            foreach (var smsmsg in msgs.SMSMessages)
            {
                if (smsmsg.Status == "received")
                {

                   //do something with messages, ie: smsmsg.Body
                }
            }

        }
    }
}
Twilio SMS Messages In Non-web .net Application with Twilio REST API (c#)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s