Added a basic Twitter posting implementation.

This commit is contained in:
Erik C. Thauvin 2008-09-11 03:41:07 +00:00
parent 563f58db1e
commit 3386138fff
18 changed files with 439 additions and 32 deletions

View file

@ -190,6 +190,11 @@ public class Mobibot extends PircBot
* The Jaiku command.
*/
private static final String JAIKU_CMD = "jaiku";
/**
* The Twitter command.
*/
private static final String TWITTER_CMD = "twitter";
/**
* The math command.
*/
@ -447,6 +452,21 @@ public class Mobibot extends PircBot
*/
private String _jaikuUser = "";
/**
* The Twitter password.
*/
private String _twitterPwd = "";
/**
* The Twitter user.
*/
private String _twitterUser = "";
/**
* The Twitter client id/source.
*/
private String _twitterSrc = "";
/**
* The history/backlogs array.
*/
@ -715,6 +735,11 @@ public class Mobibot extends PircBot
final String jname = p.getProperty("jaiku-user");
final String jkey = p.getProperty("jaiku-key");
// Get the Twitter properties
final String tname = p.getProperty("twitter-user");
final String tpwd = p.getProperty("twitter-pwd");
final String tsrc = p.getProperty("twitter-src", "");
// Create the bot
final Mobibot bot = new Mobibot(server, port, channel, logsDir);
@ -753,6 +778,12 @@ public class Mobibot extends PircBot
bot.setJaikuAuth(jname, jkey);
}
if (isValidString(tname) && isValidString(tpwd))
{
// Set the Twitter authentication
bot.setTwitterAuth(tname, tpwd, tsrc);
}
// Set the tags
bot.setTags(tags);
@ -934,6 +965,11 @@ public class Mobibot extends PircBot
send(sender, "To post to Jaiku:");
send(sender, DOUBLE_INDENT + bold(getNick() + ": " + JAIKU_CMD + " <message>"));
}
else if (lcTopic.endsWith(TWITTER_CMD))
{
send(sender, "To post to Twitter:");
send(sender, DOUBLE_INDENT + bold(getNick() + ": " + TWITTER_CMD + " <message>"));
}
else if (lcTopic.endsWith(RECAP_CMD))
{
send(sender, "To list the last 10 public channel messages:");
@ -1054,7 +1090,7 @@ public class Mobibot extends PircBot
send(sender,
DOUBLE_INDENT + bold(SPELL_CMD + ' ' + STOCK_CMD + ' ' + HELP_TAGS_KEYWORD + ' ' + TIME_CMD + ' '
+ USERS_CMD + ' ' + VIEW_CMD));
send(sender, DOUBLE_INDENT + bold(JAIKU_CMD + ' ' + WEATHER_CMD));
send(sender, DOUBLE_INDENT + bold(JAIKU_CMD + ' ' + TWITTER_CMD + ' ' + WEATHER_CMD));
if (isOp(sender))
{
@ -1391,6 +1427,10 @@ public class Mobibot extends PircBot
{
jaikuResponse(sender, args);
}
else if (cmd.startsWith(TWITTER_CMD))
{
twitterResponse(sender, args);
}
else if (cmd.startsWith(SPELL_CMD))
{
spellResponse(sender, args);
@ -2404,6 +2444,31 @@ public class Mobibot extends PircBot
}
}
/**
* Posts a message to Twitter.
*
* @param sender The sender's nick.
* @param message The message.
*/
private void twitterResponse(String sender, String message)
{
if (isValidString(_twitterPwd) && isValidString(_twitterUser))
{
if (message.length() > 0)
{
new Thread(new Twitter(this, sender, _twitterUser, _twitterPwd, _twitterSrc, message)).start();
}
else
{
helpResponse(sender, TWITTER_CMD);
}
}
else
{
send(sender, "The Twitter posting facility is disabled.");
}
}
/**
* Responds with the bot's information.
*
@ -2441,12 +2506,8 @@ public class Mobibot extends PircBot
*/
private boolean isIgnoredNick(String nick)
{
if (isValidString(nick))
{
return _ignoredNicks.contains(nick.toLowerCase());
}
return isValidString(nick) && _ignoredNicks.contains(nick.toLowerCase());
return false;
}
/**
@ -2893,6 +2954,20 @@ public class Mobibot extends PircBot
_jaikuUser = user;
}
/**
* Sets the Twitter user and password..
*
* @param user The Twitter user.
* @param key The Twitter password.
* @param source The Twitter source.
*/
private void setTwitterAuth(String user, String key, String source)
{
_twitterPwd = key;
_twitterUser = user;
_twitterSrc = source;
}
/**
* Sets the ident password.
*

View file

@ -1,5 +1,5 @@
/* Created by JReleaseInfo AntTask from Open Source Competence Group */
/* Creation date Thu Oct 11 18:41:48 PDT 2007 */
/* Creation date Wed Sep 10 20:04:51 PDT 2008 */
package net.thauvin.erik.mobibot;
import java.util.Date;
@ -20,21 +20,21 @@ public class ReleaseInfo {
}
/** buildDate (set during build process to 1192153308645L). */
private static final Date buildDate = new Date(1192153308645L);
/** buildDate (set during build process to 1221102291797L). */
private static final Date buildDate = new Date(1221102291797L);
/**
* Get buildDate (set during build process to Thu Oct 11 18:41:48 PDT 2007).
* Get buildDate (set during build process to Wed Sep 10 20:04:51 PDT 2008).
* @return Date buildDate
*/
public static Date getBuildDate() { return buildDate; }
/**
* Get buildNumber (set during build process to 0).
* Get buildNumber (set during build process to 3).
* @return int buildNumber
*/
public static int getBuildNumber() { return 0; }
public static int getBuildNumber() { return 3; }
/** project (set during build process to "mobibot"). */

View file

@ -0,0 +1,93 @@
/*
* @(#)Jaiku.java
*
* Copyright (C) 2007 Erik C. Thauvin
* All rights reserved.
*
* $Id$
*
*/
package net.thauvin.erik.mobibot;
/**
* Inserts presence information into Twitter.
*
* @author <a href="mailto:erik@thauvin.net">Erik C. Thauvin</a>
* @version $Revision$, $Date$
* @created Sept 10, 2008
* @since 1.0
*/
public class Twitter implements Runnable
{
/**
* The bot.
*/
private final Mobibot _bot;
/**
* The Twitter API password.
*/
private final String _pwd;
/**
* The Twitter user.
*/
private final String _user;
/**
* The Twitter message.
*/
private final String _message;
/**
* The Twitter source.
*/
private final String _source;
/**
* The nick of the person who sent the message.
*/
private final String _sender;
/**
* Creates a new Twitter object.
*
* @param bot The bot.
* @param sender The nick of the person who sent the message.
* @param user The Twitter user.
* @param password The Twitter password.
* @param source The Twitter client id/source.
* @param message The Twitter message.
*/
public Twitter(Mobibot bot, String sender, String user, String password, String source, String message)
{
_bot = bot;
_user = user;
_pwd = password;
_source = source;
_message = message;
_sender = sender;
}
public final void run()
{
try
{
twitter4j.Twitter twitter = new twitter4j.Twitter(_user, _pwd);
if (Mobibot.isValidString(_source))
{
twitter.setSource(_source);
}
twitter.update(_message + " (" + _sender + ')');
_bot.send(_sender, "You message was posted to http://twitter.com/" + _user);
}
catch (Exception e)
{
_bot.getLogger().warn("Unable to post to Twitter: " + _message, e);
_bot.send(_sender, "An error has occurred: " + e.getMessage());
}
}
}