Added support for Twitter's OAuth, see TwitterOAuth.java for instructions.
This commit is contained in:
parent
64656550eb
commit
907c4c371b
10 changed files with 384 additions and 195 deletions
|
@ -94,8 +94,7 @@ public class Mobibot extends PircBot
|
|||
* The version strings.
|
||||
*/
|
||||
private static final String[] VERSION_STRS =
|
||||
{"Version: " + ReleaseInfo.getVersion() + '.' + ReleaseInfo.getBuildNumber() + " ("
|
||||
+ ISO_SDF.format(ReleaseInfo.getBuildDate()) + ')',
|
||||
{"Version: " + ReleaseInfo.getVersion() + '.' + ReleaseInfo.getBuildNumber() + " (" + ISO_SDF.format(ReleaseInfo.getBuildDate()) + ')',
|
||||
"Platform: " + System.getProperty("os.name") + " (" + System.getProperty("os.version") + ", "
|
||||
+ System.getProperty("os.arch") + ", " + System.getProperty("user.country") + ')',
|
||||
"Runtime: " + System.getProperty("java.runtime.name") + " (build "
|
||||
|
@ -471,19 +470,24 @@ public class Mobibot extends PircBot
|
|||
private String _jaikuUser = "";
|
||||
|
||||
/**
|
||||
* The Twitter password.
|
||||
* The Twitter consumer key.
|
||||
*/
|
||||
private String _twitterPwd = "";
|
||||
private String _twitterConsumerKey = "";
|
||||
|
||||
/**
|
||||
* The Twitter user.
|
||||
* The Twitter consumer secret.
|
||||
*/
|
||||
private String _twitterUser = "";
|
||||
private String _twitterConsumerSecret = "";
|
||||
|
||||
/**
|
||||
* The Twitter client id/source.
|
||||
* The Twitter token.
|
||||
*/
|
||||
private String _twitterSrc = "";
|
||||
private String _twitterToken = "";
|
||||
|
||||
/**
|
||||
* The Twitter token secret.
|
||||
*/
|
||||
private String _twitterTokenSecret = "";
|
||||
|
||||
/**
|
||||
* The history/backlogs array.
|
||||
|
@ -754,9 +758,10 @@ public class Mobibot extends PircBot
|
|||
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", "");
|
||||
final String tconsumerKey = p.getProperty("twitter-consumerKey");
|
||||
final String tconsumerSecret = p.getProperty("twitter-consumerSecret");
|
||||
final String ttoken = p.getProperty("twitter-token", "");
|
||||
final String ttokenSecret = p.getProperty("twitter-tokenSecret", "");
|
||||
|
||||
// Create the bot
|
||||
final Mobibot bot = new Mobibot(server, port, channel, logsDir);
|
||||
|
@ -796,10 +801,11 @@ public class Mobibot extends PircBot
|
|||
bot.setJaikuAuth(jname, jkey);
|
||||
}
|
||||
|
||||
if (isValidString(tname) && isValidString(tpwd))
|
||||
if (isValidString(tconsumerKey) && isValidString(tconsumerSecret) && isValidString(ttoken) && isValidString(
|
||||
ttokenSecret))
|
||||
{
|
||||
// Set the Twitter authentication
|
||||
bot.setTwitterAuth(tname, tpwd, tsrc);
|
||||
bot.setTwitterAuth(tconsumerKey, tconsumerSecret, ttoken, ttokenSecret);
|
||||
}
|
||||
|
||||
// Set the tags
|
||||
|
@ -1227,9 +1233,8 @@ public class Mobibot extends PircBot
|
|||
{
|
||||
if (_logger.isDebugEnabled())
|
||||
{
|
||||
_logger.debug(
|
||||
"Unable to reconnect to " + _ircServer + " after " + MAX_RECONNECT + " retries.",
|
||||
ex);
|
||||
_logger.debug("Unable to reconnect to " + _ircServer + " after " + MAX_RECONNECT + " retries.",
|
||||
ex);
|
||||
}
|
||||
|
||||
e.printStackTrace(System.err);
|
||||
|
@ -2490,11 +2495,18 @@ public class Mobibot extends PircBot
|
|||
*/
|
||||
private void twitterResponse(String sender, String message)
|
||||
{
|
||||
if (isValidString(_twitterPwd) && isValidString(_twitterUser))
|
||||
if (isValidString(_twitterConsumerKey) && isValidString(_twitterConsumerSecret) && isValidString(_twitterToken)
|
||||
&& isValidString(_twitterTokenSecret))
|
||||
{
|
||||
if (message.length() > 0)
|
||||
{
|
||||
new Thread(new Twitter(this, sender, _twitterUser, _twitterPwd, _twitterSrc, message)).start();
|
||||
new Thread(new Twitter(this,
|
||||
sender,
|
||||
_twitterConsumerKey,
|
||||
_twitterConsumerSecret,
|
||||
_twitterToken,
|
||||
_twitterTokenSecret,
|
||||
message)).start();
|
||||
}
|
||||
else
|
||||
{
|
||||
|
@ -3010,17 +3022,19 @@ public class Mobibot extends PircBot
|
|||
}
|
||||
|
||||
/**
|
||||
* Sets the Twitter user and password..
|
||||
* Sets the Twitter consumerSecret and password..
|
||||
*
|
||||
* @param user The Twitter user.
|
||||
* @param key The Twitter password.
|
||||
* @param source The Twitter source.
|
||||
* @param consumerKey The Twitter consumer key.
|
||||
* @param consumerSecret The Twitter consumer secret.
|
||||
* @param token The Twitter token.
|
||||
* @param tokenSecret The Twitter token secret.
|
||||
*/
|
||||
private void setTwitterAuth(String user, String key, String source)
|
||||
private void setTwitterAuth(String consumerKey, String consumerSecret, String token, String tokenSecret)
|
||||
{
|
||||
_twitterPwd = key;
|
||||
_twitterUser = user;
|
||||
_twitterSrc = source;
|
||||
_twitterConsumerKey = consumerKey;
|
||||
_twitterConsumerSecret = consumerSecret;
|
||||
_twitterToken = token;
|
||||
_twitterTokenSecret = tokenSecret;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/* Created by JReleaseInfo AntTask from Open Source Competence Group */
|
||||
/* Creation date Mon Sep 13 14:45:55 PDT 2010 */
|
||||
/* Creation date Mon Sep 13 17:42:54 PDT 2010 */
|
||||
package net.thauvin.erik.mobibot;
|
||||
|
||||
import java.util.Date;
|
||||
|
@ -20,21 +20,21 @@ public class ReleaseInfo {
|
|||
}
|
||||
|
||||
|
||||
/** buildDate (set during build process to 1284414355047L). */
|
||||
private static final Date buildDate = new Date(1284414355047L);
|
||||
/** buildDate (set during build process to 1284424974097L). */
|
||||
private static final Date buildDate = new Date(1284424974097L);
|
||||
|
||||
/**
|
||||
* Get buildDate (set during build process to Mon Sep 13 14:45:55 PDT 2010).
|
||||
* Get buildDate (set during build process to Mon Sep 13 17:42:54 PDT 2010).
|
||||
* @return Date buildDate
|
||||
*/
|
||||
public static Date getBuildDate() { return buildDate; }
|
||||
|
||||
|
||||
/**
|
||||
* Get buildNumber (set during build process to 5).
|
||||
* Get buildNumber (set during build process to 6).
|
||||
* @return int buildNumber
|
||||
*/
|
||||
public static int getBuildNumber() { return 5; }
|
||||
public static int getBuildNumber() { return 6; }
|
||||
|
||||
|
||||
/** project (set during build process to "mobibot"). */
|
||||
|
|
|
@ -9,6 +9,10 @@
|
|||
*/
|
||||
package net.thauvin.erik.mobibot;
|
||||
|
||||
import twitter4j.Status;
|
||||
import twitter4j.TwitterFactory;
|
||||
import twitter4j.http.AccessToken;
|
||||
|
||||
/**
|
||||
* Inserts presence information into Twitter.
|
||||
*
|
||||
|
@ -25,14 +29,14 @@ public class Twitter implements Runnable
|
|||
private final Mobibot _bot;
|
||||
|
||||
/**
|
||||
* The Twitter API password.
|
||||
* The Twitter consumer secret.
|
||||
*/
|
||||
private final String _pwd;
|
||||
private final String _consumerSecret;
|
||||
|
||||
/**
|
||||
* The Twitter user.
|
||||
* The Twitter consumer key.
|
||||
*/
|
||||
private final String _user;
|
||||
private final String _consumerKey;
|
||||
|
||||
/**
|
||||
* The Twitter message.
|
||||
|
@ -40,9 +44,14 @@ public class Twitter implements Runnable
|
|||
private final String _message;
|
||||
|
||||
/**
|
||||
* The Twitter source.
|
||||
* The Twitter access token.
|
||||
*/
|
||||
private final String _source;
|
||||
private final String _accessToken;
|
||||
|
||||
/**
|
||||
* The Twitter access token secret.
|
||||
*/
|
||||
private final String _accessTokenSecret;
|
||||
|
||||
/**
|
||||
* The nick of the person who sent the message.
|
||||
|
@ -54,17 +63,20 @@ public class Twitter implements Runnable
|
|||
*
|
||||
* @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 consumerKey The Twitter consumer key.
|
||||
* @param consumerSecret The Twitter consumer secret.
|
||||
* @param accessToken The Twitter access token.
|
||||
* @param accessTokenSecret The Twitter access token secret.
|
||||
* @param message The Twitter message.
|
||||
*/
|
||||
public Twitter(Mobibot bot, String sender, String user, String password, String source, String message)
|
||||
public Twitter(Mobibot bot, String sender, String consumerKey, String consumerSecret, String accessToken,
|
||||
String accessTokenSecret, String message)
|
||||
{
|
||||
_bot = bot;
|
||||
_user = user;
|
||||
_pwd = password;
|
||||
_source = source;
|
||||
_consumerKey = consumerKey;
|
||||
_consumerSecret = consumerSecret;
|
||||
_accessToken = accessToken;
|
||||
_accessTokenSecret = accessTokenSecret;
|
||||
_message = message;
|
||||
_sender = sender;
|
||||
}
|
||||
|
@ -73,16 +85,17 @@ public class Twitter implements Runnable
|
|||
{
|
||||
try
|
||||
{
|
||||
twitter4j.Twitter twitter = new twitter4j.Twitter(_user, _pwd);
|
||||
final twitter4j.Twitter twitter = new TwitterFactory().getOAuthAuthorizedInstance(_consumerKey,
|
||||
_consumerSecret,
|
||||
new AccessToken(
|
||||
_accessToken,
|
||||
_accessTokenSecret));
|
||||
|
||||
if (Mobibot.isValidString(_source))
|
||||
{
|
||||
twitter.setSource(_source);
|
||||
}
|
||||
final Status status = twitter.updateStatus(_message + " (" + _sender + ')');
|
||||
|
||||
twitter.update(_message + " (" + _sender + ')');
|
||||
|
||||
_bot.send(_sender, "You message was posted to http://twitter.com/" + _user);
|
||||
_bot.send(_sender,
|
||||
"You message was posted to http://twitter.com/" + twitter.getScreenName() + "/statuses/" + status
|
||||
.getId());
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
|
|
86
src/net/thauvin/erik/mobibot/TwitterOAuth.java
Normal file
86
src/net/thauvin/erik/mobibot/TwitterOAuth.java
Normal file
|
@ -0,0 +1,86 @@
|
|||
/*
|
||||
* @(#)TwitterOAuth.java
|
||||
*
|
||||
* Copyright (C) 2010 Erik C. Thauvin
|
||||
* All rights reserved.
|
||||
*/
|
||||
package net.thauvin.erik.mobibot;
|
||||
|
||||
import twitter4j.TwitterException;
|
||||
import twitter4j.TwitterFactory;
|
||||
import twitter4j.http.AccessToken;
|
||||
import twitter4j.http.RequestToken;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.InputStreamReader;
|
||||
|
||||
/**
|
||||
* The <code>TwitterOAuth</code> class.
|
||||
* <p/>
|
||||
* Go to <a href="http://twitter.com/oauth_clients/new">http://twitter.com/oauth_clients/new</a> to register your bot.
|
||||
* Then execute:
|
||||
* <p/>
|
||||
* <code>java -cp "mobibot.jar:../lib/*" net.thauvin.erik.mobibot.TwitterOAuth <consumerKey> <consumerSecret></code>
|
||||
* <p/>
|
||||
* and follow the prompts/instructions.
|
||||
*
|
||||
* @author <a href="mailto:erik@thauvin.net">Erik C. Thauvin</a>
|
||||
* @version $Revision$, $Date$
|
||||
* @created Sep 13, 2010
|
||||
* @since 1.0
|
||||
*/
|
||||
public class TwitterOAuth
|
||||
{
|
||||
public static void main(String args[])
|
||||
throws Exception
|
||||
{
|
||||
if (args.length == 2)
|
||||
{
|
||||
final twitter4j.Twitter twitter = new TwitterFactory().getInstance();
|
||||
twitter.setOAuthConsumer(args[0], args[1]);
|
||||
final RequestToken requestToken = twitter.getOAuthRequestToken();
|
||||
AccessToken accessToken = null;
|
||||
final BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
|
||||
while (null == accessToken)
|
||||
{
|
||||
System.out.println("Open the following URL and grant access to your account:");
|
||||
System.out.println(requestToken.getAuthorizationURL());
|
||||
System.out.print("Enter the PIN (if available) or just hit enter.[PIN]:");
|
||||
final String pin = br.readLine();
|
||||
try
|
||||
{
|
||||
if (pin.length() > 0)
|
||||
{
|
||||
accessToken = twitter.getOAuthAccessToken(requestToken, pin);
|
||||
}
|
||||
else
|
||||
{
|
||||
accessToken = twitter.getOAuthAccessToken();
|
||||
}
|
||||
}
|
||||
catch (TwitterException te)
|
||||
{
|
||||
if (401 == te.getStatusCode())
|
||||
{
|
||||
System.out.println("Unable to get the access token.");
|
||||
}
|
||||
else
|
||||
{
|
||||
te.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
System.out.println(
|
||||
"Please add the following to the bot's property file:" + "\n\n" + "twitter-consumerKey=" + args[0]
|
||||
+ '\n' + "twitter-consumerSecret=" + args[1] + '\n' + "twitter-token=" + accessToken.getToken()
|
||||
+ '\n' + "twitter-tokenSecret=" + accessToken.getTokenSecret());
|
||||
}
|
||||
else
|
||||
{
|
||||
System.out.println("Usage: " + TwitterOAuth.class.getName() + " <consumerKey> <consumerSecret>");
|
||||
}
|
||||
|
||||
System.exit(0);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue