KeyLimeTie Blog

Twitterizer Simplifies .NET Integration with Twitter

By Peter Morano – 5/1/2009. Posted to Applications.

As Twitter continues to grow in popularity, integrating your applications will become an essential step in effectively reaching a community. Fortunately, connecting your .NET applications with Twitter can be a trivial task if you use the Twitterizer Framework, a .NET 2.0 library that provides an easy to use, object-oriented interface to twitter's online API. This article and sample application will show you how to take advantage of this interface.

Sample Application

Getting Started

There are several steps required to get started:

1. Register your application with Twitter: http://twitter.com/oauth_clients/new. You can skip this step, but any status posts your application makes to Twitter will appear as coming from Twitterizer.
 
2. Download the Twitterizer Framework:  http://code.google.com/p/twitterizer/ and add a reference in your project.

Taking Control of Twitter

The Twitter API allows programmatic access to updating, searching and deleting a user’s status, adding and removing friends and followers, and direct messaging other others. The following examples show some of these operations.

Establish a Connection
To establish a connection, you instantiate the Twitter object as shown below (If you skip step one above, you do not need to supply the Source parameter).

                Twitter twit = null;
                twit = new Twitter("twitteruser", "twitterpwd", "app_source");


Updating Your Status

                TwitterStatus ts = twit.Status.Update("My Message");


Search and Destroy

                TwitterStatusCollection myStatus = twit.Status.UserTimeline();
                Console.WriteLine("My Status");
                foreach (TwitterStatus status in myStatus)
                {
                    Console.WriteLine(status.Text);

                    if (status.Text == "My Message")
                        twit.Status.Destroy(status.ID);
                }
                Console.WriteLine();

Search for Another User’s Status
To get the status entries for another user, you can use the TwitterParameters object, setting optional parameter values.

                TwitterParameters paras = new TwitterParameters();
                paras.Add(TwitterParameterNames.ID, UsersID);
                paras.Add(TwitterParameterNames.Since,
                     Convert.ToDateTime("1/1/2009"));
                paras.Add(TwitterParameterNames.Page, 1);
                paras.Add(TwitterParameterNames.Count, 5);
                paras.Add(TwitterParameterNames.SinceID, 1);

                TwitterStatusCollection usersStatus =
                      twit.Status.UserTimeline(paras);

                Console.WriteLine("Status For Other User");
                foreach (TwitterStatus status in usersStatus)
                {
                    Console.WriteLine(string.Format("Status For User {0}: {1}", 
                          status.TwitterUser.ScreenName, status.Text));
                }
                Console.WriteLine();

Finding Out About Your Friends (and ditching one)

                TwitterUserCollection friends = twit.User.Friends();
                foreach (TwitterUser friend in friends)
                {
                    Console.WriteLine(friend.ScreenName.ToString());
                    Console.WriteLine(friend.Status.Text.ToString());
                    Console.WriteLine(friend.NumberOfFollowers.ToString());

                    //Remove a friend
                    friends.Remove(friend);
                }


Getting Your Friend’s Status

                paras = new TwitterParameters();
                paras.Add(TwitterParameterNames.ID, 36867666);

                TwitterStatusCollection friendsPosts =
                     twit.Status.FriendsTimeline(paras);

                Console.WriteLine("Status For Friends");
                foreach (TwitterStatus status in friendsPosts)
                {
                    if (status.TwitterUser.ScreenName != "petermorano")
                        Console.WriteLine(string.Format("Status For Friend {0}: {1}",
                              status.TwitterUser.ScreenName, status.Text));
                }


Following Someone

                TwitterUser user = new TwitterUser();
                user.ScreenName = "aplusk";
                twit.User.FollowUser(user);


Locating Followers

                TwitterUserCollection followers = twit.User.Followers();
                foreach (TwitterUser follower in followers)
                {
                    Console.WriteLine(follower.ScreenName.ToString());
                    Console.WriteLine(follower.Status.Text.ToString());
                    Console.WriteLine(follower.NumberOfFollowers.ToString());
                }


Handling Errors

The framework also includes a TwitterizerException object that returns exception and request data from the Twitter API. Exceptions can be thrown for a number of reasons, including failure to authenticate, trying to add a friend that is already a friend, and trying to alter someone else's status. The object contains a RequestData child object that contains the details of the original request.

            catch (TwitterizerException tex)
            {
                string message = string.Format("Exception: {0}",
                     tex.Message.ToString());
                Console.WriteLine(message);
            }


Conclusion and Uses

There are a number of ways that an application can leverage the power of Twitter, such as broadcasting changes in a product's price, letting others know that you have posted a new blog, or letting a group of users know that a task is complete. And because statuses can also be read, applications can be find out when another user or application updates their status.

Photos on Flickr

More Photos »

Search Blog


Archives