Sending Tweets from Java for my Baseball Pool

Lat year, I showed how I used a simple Java App to read the MLB stats feed for my office baseball pool.  This year I have added the feature to tweet out a message to my Twitter account when a team scores 13 runs.  I used the fabulously simple twitter4j API to hook in to twitter.

I also needed to go to the Twitter Apps site and generate some keys for Twitter4j to use.  I used ConfigurationBuilder to enter the key values in to my code, but there are several different ways, all of which you can find on the twitter4j site.

import twitter4j.Status;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.conf.ConfigurationBuilder;
ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setDebugEnabled(true)
.setOAuthConsumerKey("my-values-removed")
.setOAuthConsumerSecret("my-values-removed")
.setOAuthAccessToken("my-values-removed")
.setOAuthAccessTokenSecret("my-values-removed");
TwitterFactory tf = new TwitterFactory(cb.build());
Twitter twitter = tf.getInstance();

Then it is as simple as posting a tweet saying someone scored 13 runs…

if (homescore == 13) {
  scored13 = true;
  thirteen.append (hometeam + " ");
}
if (awayscore == 13) {
  scored13 = true;
  thirteen.append (awayteam + " ");
}
if (scored13) {
  summary.append("Scored 13 Runs Last Night: "+ thirteen + "\t \n");
  Status status = twitter.updateStatus(summary + " #mymlb13runpool");
}