Automatically Tweet your Blogs – Why and How (Java example)
In this blog we will discuss some of the reasons why people tweet their blogs and one cool way to do it with Java.
Why tweet your blogs?
The reasons are many. To count a few:
1. It helps spread the word about your blog to a wide set of audience.
2. Tweets have an inherent tendency to spread easily and so they are a perfect tool to spread some viral news.
3. Short texts can be written to create curiosity amongst the reader and make him not only click a link and read the blog but get involved. That’s why SMS’s are so popular!
4. Good tweets can always help you build a good group of members for your blog!
5. And, last but not the least: tweeting is free! Who doesn’t like any free way of promoting anything?
So how it’s done?
Unfortunately many people don’t know that twitter APIs are very well maintained and if you spend a day understanding them and writing your own twitter bot you can save 100s of dollars that would otherwise go in the pocket of companies that sell so called “twitter bots” for ridiculous price.
Prerequisites
Although twitter APIs come in almost any programming language, in this tutorial we will focus on Java. All you’ll need is basic understanding of Java and any editor of your choice. We are not going to discuss how you are going to install and configure JDK or install the editor or the whole App end to end. We are assuming you have gone through the good old “HelloWorld” sample of writing a web application or anything of that sort for that matter. Now that if you know all that stuff and really want to know how to automate twitter from your blog, you may proceed to read further; otherwise, take a step back and first go learn Java!
What all you’ll need?
All you’ll need is RSS Utils (rssutils.jar) and a good twitter API for Java. We will focus on twitter4j. It’s the first one mentioned in twitter’s own documentation, is well documented and is even available in maven repository!
Pulling feed from your blog – using RSS Utils:
You can read how to use RSS Utils here: http://java.sun.com/developer/technicalArticles/javaserverpages/rss_utilities/
RSS Utils allows you to easily read your blog’s RSS feed. If you are using wordpress or any other famous blogging engine, most probably your rss feed url will be http://your-blog-url/?feed=rss
Then you can create a Java Rss object like so:
RssParser parser = RssParserFactory.createDefault();
Rss rss = parser.parse(new URL”<rss feed url of your blog>”);
And read all the items in its Channel object. Every item will give you your blog’s title, description and url. Using this you can create a string; truncate it to fit in the 140 characters because anything beyond that will freak out twitter.
Once you have the string to be tweeted, now comes the tweeting part.
Tweeting a message to twitter:
For this we will use twitter4j. You can read all about twitter4j here: http://twitter4j.org/en/index.html
Keep in mind that twitter doesn’t support Basic Authentication any more. So most of the documentation related to basic auth in twitter4j and most of the APIs related to that are now useless. They all are deprecated (or will be deprecated). If you still use them, twitter will throw an error telling you about the same. So directly move on and learn about the OAuth (point 7 on this page: http://twitter4j.org/en/code-examples.html). This example shows you how to register an application using your twitter account and how to use the secure token to publish a message.
If you get stuck anywhere, don’t hesitate to ask here! We will be more than happy to help!
cheers! and happy twitting/blogging!
December 8, 2010 at 3:55 pm Comments (0)










