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

4 Comments

  1. Markus says:

    Hi,

    I realize this is an older post, but I am unable to get tctweet to work, when I add the Jar file it fails to load Team City at all and I’m presented with the error here: http://pastebin.com/gtgNdizz

    If I remove it and restart TC everything begins working again. I am using the JAR found on github (in the source code).

    I’m running TeamCity: 6.5.4 (build 18046)

    I’d love to get this working, thanks!

    Markus

  2. orn says:

    Hi Markus, can you open an issue on Github for this, https://github.com/ornatwork/tc_plugins/issues as the code is maintained there. I will take a look.

  3. Morley says:

    Hi,

    Any chance this will be updated for TeamCity 7.1?

    I’ve followed the steps in the readme and I can’t seem to get it working.

    Thanks.

  4. orn says:

    Hi Morley, can you open an issue on Github for this, https://github.com/ornatwork/tc_plugins/issues as the code is maintained there. I will take a look. Also if you look in your TeamCity logs, can you see if you find errors that might give us some idea of what is going on ? The log dir is usually /TeamCityinstalldir/logs

    Cheers,

Leave a Reply