Monday, December 3, 2012

Twitter Scheduler Application

I'm quite new to Twitter, but believe it is a powerful tool for small businesses such as mine.  I wanted to make it even more powerful for my software testing consultancy business, without finding it taking up too much of my time.

The functionality I wanted was to be able to set up my planned tweets first thing in the morning, before the 'real' work began - and then leave some sort of application or robot working in the background to actually send the tweets when I wanted them sent.

This enables me to write a few tweets and get them sent at key points during the day (or night) for my target markets - for example when the West Coast USA is finishing the working day - I'm UK based.

There are commercial tools which have this functionality, but they are paid for and also I am told quite fiddly to set up.  Plus I wanted to write my own!

It turns out it is very easy....

I wrote the application in Java using the twitter4j library.  I needed only a handful of classes:

I had a Tweet class which had fields such as message, dateTimeToSend and hasBeenSent.

I had a TweetScheduleReader which utilised the Java CSV library.  This read in my planned tweets from a CSV file - reading in a list of messages, the time to send them and a flag as to whether they had been sent yet.  This gets converted into a List of Tweets held in memory.

Then there is a class with a main method which just called the TimerTask run method hence:

timer.schedule(timerTask, new Date(), DELAY);

So starting from the moment this line of code is called, every DELAY (in my case 1 minute), the timetask run method is scheduled.  So what gets run?  See below.

I had a class which extended TimerTask and the run method was this:


public void run() {

//Login to twitter
Twitter twitter = new TwitterFactory().getInstance();
twitter.setOAuthConsumer(CONSUMER_KEY, CONSUMER_KEY_SECRET);
String accessToken = getSavedAccessToken();
String accessTokenSecret = getSavedAccessTokenSecret();
AccessToken oathAccessToken = new AccessToken(accessToken,accessTokenSecret);

twitter.setOAuthAccessToken(oathAccessToken);

//read the list of tweets from CSV
List<Tweet> tweetsToSend = scheduleReader.getTweets();
if (tweetsToSend == null || tweetsToSend.size() == 0) {
scheduleReader.readTweetsFromCSV();
tweetsToSend = scheduleReader.getTweets();
}

Date currentTime = new Date();
List<Tweet> updatedTweetToSend = new ArrayList<Tweet>();
for (Tweet tweet : tweetsToSend) {
if (currentTime.after(tweet.getDateTimeToSend())) {
if (!tweet.isHasBeenSent()) {
//send it!
try {
                                                //I appended the date to make the tweet unique - else it gets rejected by             //Twitter
twitter.updateStatus(tweet.getMessage() + " " + new Date());
} catch (TwitterException e) {
e.printStackTrace();
}
//mark it as sent
tweet.setHasBeenSent(true);

}

}
updatedTweetToSend.add(tweet);
}

//update the tweets in memory marking them as sent if appropriate
scheduleReader.setTweets(updatedTweetToSend);

}

I found this tutorial very useful regarding the authorisation: http://www.javacodegeeks.com/2011/10/java-twitter-client-with-twitter4j.html





No comments:

Post a Comment