Automatically sending tweets using TweetSharp

One of the sites I run for the Dutch .NET community is .NET Events in the Netherlands which lists all sorts of community events for, as the name suggests, Dutch .NET developers. The site has been around for a while but always forced people to either go to the site or use in iCalendar feed so see what is happening. Some time ago I decided that these should also be tweeted so people using Twitter would be notified of upcoming events.

There are several ways to automatically send tweets to Twitter and I choose to use TweetSharp as that seemed to be one of the more popular libraries around.

 

The first step is registering an application with Twitter. This is done using this page, make sure the access type is set to Read & Write so you can sent updates. This will give us the first two bits of information we need to send Twitter massages, the Consumer key and the Consumer secret.

image

 

Because I only needed to use a single client there is no need to implement the complete OAuth workflow to determine the authenticated access token. Instead I can just copy them from the Twitter site by clicking the My Access Token link to get to the following page where the Access Token (oauth_token) and the Access Token Secret (oauth_token_secret) are the last to pieces of information we need. These tokens don’t expire until we explicitly do so ourselves so we can keep on using these in our program

image

 

With these pieces in place sending a Twitter update is a breeze

private static void PostTweet(string twitterStatus)


{


    var clientInfo = GetClientInfo();


    var twitterStatuses = FluentTwitter.CreateRequest(clientInfo)


        .AuthenticateWith(_oauthToken, _oauthTokenSecret)


        .Statuses();


 


    var tw = twitterStatuses.Update(twitterStatus);


 


    var rsp = tw.Request();


 


    if (rsp.IsTwitterError == false)


    {


        var status = rsp.AsStatus();


        Console.WriteLine("Tweeting: {0}", status.Text);


    }


    else


    {


        var error = rsp.AsError();


        Console.ForegroundColor = ConsoleColor.Red;


        Console.WriteLine(error.ErrorMessage);


        Console.ResetColor();


    }


}


 


private static TwitterClientInfo GetClientInfo()


{


    var clientInfo = new TwitterClientInfo()


    {


        ConsumerKey = _consumerKey,


        ConsumerSecret = _consumerSecret


    };


 


    return clientInfo;


}

 

Don’t forget all required using statements as some of the methods like AuthenticateWith() and Update() called are extensions methods.

using TweetSharp;


using TweetSharp.Twitter.Extensions;


using TweetSharp.Twitter.Fluent;



Using the twitterStatuses you can also loop over the existing tweets, delete or retweet the just like you can in the user interface or Twitter.



 



Enjoy!



[f1]
[f2]

One thought on “Automatically sending tweets using TweetSharp

  1. Can you please explain what are you doing in each step, so it will be easy to understand to change it according to my needs.

    I appreciate your help. Thanks

    Pavi

Leave a Reply

Your email address will not be published. Required fields are marked *


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>