For those who don't know what Twitter is, I'll provide two links that will help you to understand it. Wikipedia has a good explanation here: http://en.wikipedia.org/wiki/Twitter And here is Twitter's own page explaining what they are: http://twitter.com/about#about
So, if you've read these links, or if you're already familiar with Twitter, you've realized that this is primarily a social networking application, but that people have found other uses for it.
So, for starters, why would you want to do this?
Several uses that might be of interest to a U2 programmer include:
- Sending messages from a system and being able to monitor them elsewhere. A powerful and simple publish/subscribe model (with some limitations).
- Keeping customers or prospects aware of promotions, events and offers.
- Filtering Twitter Searches programmatically, to provide a short-list of interesting messages.
There's bound to be more, but that's a good starting point.
Next, I'm going to show how to get/put updates from/to Twitter directly from a UniBASIC program on Universe on *nix. The same concept will work on Universe on Windows, Unidata on anything and for that matter, any MultiValue (PICK) platform that lets you run a cmd line application and capture the results.
To get the maximum reusability, I've done much of the UniBASIC code as subroutines, effectively an API that you can call.
I've created a directory named "TWITTER" as a subdirectory in the same directory as your account VOC resides. In actual fact, I created a type 19 file called TWITTER (DIR file type in Unidata), which automatically created the TWITTER directory for me, but we won't need the U2 file pointer at this time. I use this directory as a scratch area, a place for the java classes that I use. On non-U2 systems you might need a MultiValue file called TWITTER and a directory called TWITTER.
The java components that I use are a command line java program called FWTwitterDirect.java and an open source library that I found referenced on the Twitter developer pages, called jtwitter.jar. You can see more about this library at http://www.winterwell.com/software/jtwitter.php. Our java file is listed below:
The syntax for the command, when run from the directory where your VOC resides, and with no CLASSPATH environment variable set, is as follows:
java -classpath TWITTER:TWITTER/jtwitter.jar FWTwitterDirect userId password ["text"]
Note that if the text contains spaces you'll need quotes around it. If you omit the text, we retrieve the last 20 tweets for the user and the user's friends.
Now for the UniBASIC API code:
Note that DIRECTION is passed in as either "GET" or "PUT".
For PUT, you must provide the value of your tweet in the TEXT variable. Remember to keep it to 140 bytes or we will truncate.
For GET, TEXT will be overwritten with a dynamic array containing up to 20 attributes. Each attribute is a tweet from the user themselves or from their friends, in date/time order, with the first one being the newest. For each line, it will be divided into multivalues where they are laid out as follows:
Multivalue 1 is the Twitter user name of the user that sent the tweet.
Multivalue 2 is the Twitter user's display name.
Multivalue 3 is the datetime of the tweet.
Multivalue 4 is the text of the tweet.
So, here is an example program that uses the API. Note that while this program is interactive, you can call the API from a program running in a phantom.
So, here are the pros and the cons:
On the pro side, you don't need any additional infrastructure, you can do everything from UniBASIC. We could extend the class file to provide different types of retrievals, in addition to the GET retrieval.
On the con side, your Universe server has to have Internet access (possible security issues), any temporary failure on Twitter's part will result in an error and a lost communication, and when you pull back tweets, you have to do your own parsing.
In my next post, I'm going to be removing the cons using FusionWare Integration Server with our Twitter Gateway technology preview. The new product is named FusionWare TRILL ™ (Twitter Reliable Intelligent Live Link).