Archive for June 2011

TeamCity Twitter plugin, part 2

I had to fix an old plugin I made some time back, I did actually post about it here Java Authenticated POST. So why bring it up again, a good question I say. As Twitter changed their authentication sometime late last year ( Nov 2010 ? ). All programs using the old authentication stopped working. I hadn’t had / given myself time to look at the broken plugin. What the TeamCity twitter plugin does it to tweet when the TeamCity server starts and ends builds. If you follow that account you get notified on Twitter without having to open up your email box to see how your builds are doing.
I went googling about the new authentication and found a nice snippet on StackOverflow The snippet uses Twitter4J library to make the task really simple, check them out. The best part is that Twitter4J only needs Java 1.4 to run. That’s great as my TeamCity server only has Java 1.5 and not the newer 1.6, further as an added bonus their core jar is only 280k, that’s all I needed. Below is my code modified from the StackOverflow sample. It works fine if you are only posting to one account. The tokens and keys can be found on your Twitter app page. If you need to post for different users from your application then your code needs to take that into account. If you want the source of the plugin or all 3 plugins I have created, you can run over to GitHub and pick it up. I made it public domain, open source, whatever you want to call it.

import twitter4j.Twitter;
import twitter4j.TwitterFactory;
import twitter4j.conf.*;;

   // Updates the twitter account
   public static void sendTweet( String psText )
   {
           try
           {
                        // Configuration
                        ConfigurationBuilder confbuilder = new ConfigurationBuilder();
                        confbuilder.setOAuthAccessToken(CxGlobal.ACCESS_TOKEN_KEY)
                        .setOAuthAccessTokenSecret(CxGlobal.ACCESS_TOKEN_SECRET)
                        .setOAuthConsumerKey(CxGlobal.CONSUMER_KEY)
                        .setOAuthConsumerSecret(CxGlobal.CONSUMER_SECRET);
                                               
                        // get the interaction object
                        Twitter twitter = new TwitterFactory(confbuilder.build()).getInstance();
                        // send
                        twitter4j.Status status = twitter.updateStatus( psText );
                        //
                        //System.out.println("*** Response=" + status.getText() );
           }
           catch( Exception ex )
           {
               System.out.println( “***ERROR=” + stack2string( ex ));
               LOG.info( “***ERROR=” + stack2string( ex ) );
           }
   }

Edit, the 3 plugins have now been added to the official TeamCity PlugIn page