Thursday, July 24, 2008

OraTweetBot, an XMPP/Jabber listener for Twitter

OraTweetBot is an XMPP/Jabber bot built with Java that will listen for tweets and post them to Twitter or to a database. This is part of my pet project OraTweet which is built with Oracle APEX and will soon make it available through a distributable APEX package.

If you are tired of Twitter xmpp hook not working, you can use OraTweetBot to listen for your tweets and post them to Twitter.

If you want to go further you can hook the bot to post to your database, then your database can trigger the post to Twitter (see Post Updates to Twitter from APEX (PL/SQL)). The big advantage of this is that you can actually map twitter users to a users table on the db and send on their behalf.

Right now I'm only releasing the distributable form, if you want to get the source let me know on the comments. If there is a demmand I can create a sourceforge or google code project to host it.

Instructions:

1.-Download here OraTweetBot (unzip)
2.- Modify file conf.properties with your values.
3.- If you are planning to send tweet to a database create a Stored Procedure called INSERT_TWEET:


create or replace
PROCEDURE INSERT_TWEET( email IN VARCHAR2, tweet IN VARCHAR2, tresource IN VARCHAR2, createdon IN TIMESTAMP )
AS
BEGIN
INSERT INTO TWEET (EMAIL,TWEET,TRESOURCE,CREATEDON) VALUES (email,tweet, tresource, createdon );
COMMIT;
END INSERT_TWEET;

4.- Run it from command line
$java -jar OraTweetBot.jar
or to run in the background
$nuhup java -jar OraTweetBot.jar &
Run OraTweetBot preferably on an "always on" machine.

You can login with the same XMPP (gmail) account from different clients, so you can set up OraTweetBot with the same account you want to post tweets with.

Wednesday, July 23, 2008

Post Updates to Twitter from JAVA

Here is some sample java code you can use to post updates to Twitter from java. This is just a simple POST method, if you are looking for a full Twitter library head to Twitter Development Talk. I implemented this code on the OraTweetBot which I will make available soon.


import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;

public class Main {
public static void main(String[] args) {
SendTwitter(args[0]);
}
public static void SendTwitter(String msg ) {
try {
int proxyon = 1;
String proxyurl = "www-yourproxy.com";
int proxyport = 80;
String twitteruser = "username";
String twitterpass = "password";
SocketAddress addr = null;
java.net.Proxy proxy = null;
URLConnection connection = null;
String credentials = twitteruser + ":" + twitterpass;

if (proxyon == 1){
addr = new InetSocketAddress(proxyurl, proxyport);
proxy = new java.net.Proxy(java.net.Proxy.Type.HTTP, addr);
}

String encodecredentials = new sun.misc.BASE64Encoder().encode (credentials.getBytes());

URL url = new URL("http://twitter.com/statuses/update.xml");
String encodedData = URLEncoder.encode(msg, "UTF-8");

if (proxyon == 1){
connection = url.openConnection(proxy);
}
else{
connection = url.openConnection();
}

connection.setRequestProperty( "Authorization", "Basic " + encodecredentials);
connection.setDoOutput(true);

OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream());
out.write("status=" + encodedData);
out.close();

BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String decodedString;
while ((decodedString = in.readLine()) != null) {
System.out.println(decodedString);
}
in.close();

} catch (Exception e) {
System.out.println(e.toString());
}
}

}