#java-jaが過疎ってるのでTwitterにRTしてみた!
まずは放置してたIRCライブラリを更新しました!
以前はメソッドの命名規約で分岐するようにしてたんですが、Java 5が普及したのでアノテーションがいいよね!ってことで@Replyアノテーションを導入したバージョンに生まれ変わりました!
アプリケーションのソースコードは次のとおりです。
rt-botが#java-jaに常駐して、みんなのメッセージをTwitterに流します。
逆にTwitterのタイムラインで#java-jaを含むメッセージが来たらIRCに流すほうが便利かな?意見求む!
/* * Copyright (c) 2009 tarchan. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * Neither the name of the tarchan nor the * names of its contributors may be used to endorse or promote products * derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY tarchan ``AS IS'' AND ANY * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL tarchan BE LIABLE FOR ANY * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ package com.mac.tarchan.ircbox; import java.io.IOException; import twitter4j.Twitter; import twitter4j.TwitterException; import com.mac.tarchan.irc.client.IRCClient; import com.mac.tarchan.irc.client.IRCMessage; import com.mac.tarchan.irc.client.Reply; /** * RT * * @author tarchan */ public class RT { /** IRC クライアント */ private IRCClient irc; /** Twitter ID */ private String twitterID; /** Twitter パスワード */ private String twitterPassword; /** * #java-ja の発言を Twitter に RT します。 * * @param args {Twitter ID} {Twitter Password} */ public static void main(String[] args) { try { new RT(args[0], args[1]); } catch (IOException x) { x.printStackTrace(); System.exit(1); } } /** * RTボットを起動します。 * * @param twitterID Twitter ID * @param twitterPassword Twitter パスワード * @throws IOException 入出力エラーが発生した場合 */ public RT(String twitterID, String twitterPassword) throws IOException { // Twitter のアカウント情報 this.twitterID = twitterID; this.twitterPassword = twitterPassword; // IRC のアカウント情報 irc = new IRCClient(); irc.addAllHandlers(this); irc.setProperty("irc.host", "irc.freenode.net"); irc.setProperty("irc.port", "6667"); irc.setProperty("irc.encoding", "UTF-8"); irc.setProperty("irc.nick.name", "rt-bot"); irc.setProperty("irc.real.name", "http://twitter.com/oauth_clients/details/12539"); irc.open(); } /** * デバッグログを表示します。 * * @param msg IRCメッセージ */ @Reply("ALL") public void debug(IRCMessage msg) { // if (msg.isNumelicReply() && msg.getNumber() >= 400) { System.out.println(String.format("[%s] %s", msg.getCommand(), msg.getTrail())); } } /** * #java-ja に JOIN した時にあいさつします。 * * @param msg IRCメッセージ */ @Reply("001") public void join(IRCMessage msg) { irc.postMessage(String.format("JOIN %s", "#java-ja")); msg("RT ボットを起動しました。"); } /** * メッセージを受け取ります。 * * @param msg IRCメッセージ */ @Reply("PRIVMSG") public void privmsg(IRCMessage msg) { rt(msg.getNick(), msg.getTrail()); } /** * Twitter に RT します。 * * <pre> * ex. tarchan: RT @{nick}: {msg} #java-ja * </pre> * * @param nick ニックネーム * @param str テキスト */ public void rt(String nick, String str) { String rt = String.format("RT @%s: %s #java-ja", nick, str); System.out.println(rt); try { Twitter twitter = new Twitter(twitterID, twitterPassword); twitter.updateStatus(rt); } catch (TwitterException x) { x.printStackTrace(); } } /** * IRC にメッセージを送信します。 * * @param str テキスト */ public void msg(String str) { irc.postMessage(String.format("NOTICE %s :%s", "#java-ja", str)); } }
以上!