diff --git a/properties/mobibot.properties b/properties/mobibot.properties index 1932bd0..0a64786 100644 --- a/properties/mobibot.properties +++ b/properties/mobibot.properties @@ -37,6 +37,9 @@ tell-max-size=50 # Twitter handle to receive channel join notifications #twitter-handle= +# Automatically post links to twitter +#twitter-auto-post=true + # # Create custom search engine at: https://cse.google.com/ # and get API key from: https://console.developers.google.com/ diff --git a/src/main/java/net/thauvin/erik/mobibot/Constants.java b/src/main/java/net/thauvin/erik/mobibot/Constants.java index 4160517..af65e31 100644 --- a/src/main/java/net/thauvin/erik/mobibot/Constants.java +++ b/src/main/java/net/thauvin/erik/mobibot/Constants.java @@ -46,21 +46,22 @@ public final class Constants { * The connect/read timeout in ms. */ public static final int CONNECT_TIMEOUT = 5000; - - /** - * The empty title string. - */ - public static final String NO_TITLE = "No Title"; - - /** - * The Twitter handle property key. - */ - public static final String TWITTER_HANDLE_PROP = "twitter-handle"; - /** * Default locale. */ public static final Locale LOCALE = Locale.getDefault(); + /** + * The empty title string. + */ + public static final String NO_TITLE = "No Title"; + /** + * The Twitter handle property key. + */ + public static final String TWITTER_HANDLE_PROP = "twitter-handle"; + /** + * The twitter post flag property key. + */ + public static final String TWITTER_AUTOPOST_PROP = "twitter-auto-post"; /** * Disables the default constructor. diff --git a/src/main/java/net/thauvin/erik/mobibot/Mobibot.java b/src/main/java/net/thauvin/erik/mobibot/Mobibot.java index 97cf66e..f0b275f 100644 --- a/src/main/java/net/thauvin/erik/mobibot/Mobibot.java +++ b/src/main/java/net/thauvin/erik/mobibot/Mobibot.java @@ -176,6 +176,8 @@ public class Mobibot extends PircBot { // The tell object. private final Tell tell; // The Twitter handle for channel join notifications. + // Automatically post links to Twitter + private final boolean twitterAutoPost; private final String twitterHandle; // The Twitter module. private final Twitter twitterModule; @@ -284,6 +286,7 @@ public class Mobibot extends PircBot { twitterModule = new Twitter(); MODULES.add(twitterModule); twitterHandle = p.getProperty(Constants.TWITTER_HANDLE_PROP, ""); + twitterAutoPost = Boolean.parseBoolean(p.getProperty(Constants.TWITTER_AUTOPOST_PROP, "false")); MODULES.add(new War()); MODULES.add(new Weather2()); @@ -970,6 +973,20 @@ public class Mobibot extends PircBot { pinboard.addPost(entry); } + // Post link to twitter + if (twitterAutoPost && twitterModule.isEnabled()) { + final String msg = title + ' ' + link + " via " + sender + " on " + getChannel(); + new Thread(() -> { + try { + twitterModule.post(twitterHandle, msg, false); + } catch (ModuleException e) { + if (logger.isWarnEnabled()) { + logger.warn("Failed to post link on twitter.", e); + } + } + }).start(); + } + saveEntries(isBackup); if (Constants.NO_TITLE.equals(entry.getTitle())) { diff --git a/src/main/java/net/thauvin/erik/mobibot/modules/Twitter.java b/src/main/java/net/thauvin/erik/mobibot/modules/Twitter.java index 9b7d9c6..1b7af6e 100644 --- a/src/main/java/net/thauvin/erik/mobibot/modules/Twitter.java +++ b/src/main/java/net/thauvin/erik/mobibot/modules/Twitter.java @@ -48,12 +48,12 @@ import twitter4j.conf.ConfigurationBuilder; * @since 1.0 */ public final class Twitter extends ThreadedModule { - // The property keys. + // Property keys static final String CONSUMER_KEY_PROP = "twitter-consumerKey"; static final String CONSUMER_SECRET_PROP = "twitter-consumerSecret"; static final String TOKEN_PROP = "twitter-token"; static final String TOKEN_SECRET_PROP = "twitter-tokenSecret"; - // The twitter command. + // Twitter command private static final String TWITTER_CMD = "twitter"; /** @@ -90,19 +90,16 @@ public final class Twitter extends ThreadedModule { final boolean isDm) throws ModuleException { try { final ConfigurationBuilder cb = new ConfigurationBuilder(); - cb.setDebugEnabled(true) - .setOAuthConsumerKey(consumerKey) - .setOAuthConsumerSecret(consumerSecret) - .setOAuthAccessToken(token) - .setOAuthAccessTokenSecret(tokenSecret); + cb.setDebugEnabled(true).setOAuthConsumerKey(consumerKey).setOAuthConsumerSecret(consumerSecret) + .setOAuthAccessToken(token).setOAuthAccessTokenSecret(tokenSecret); final TwitterFactory tf = new TwitterFactory(cb.build()); final twitter4j.Twitter twitter = tf.getInstance(); if (!isDm) { - final Status status = twitter.updateStatus(message + " (" + handle + ')'); - return new NoticeMessage("You message was posted to http://twitter.com/" + twitter.getScreenName() - + "/statuses/" + status.getId()); + final Status status = twitter.updateStatus(message); + return new NoticeMessage("You message was posted to https://twitter.com/" + twitter.getScreenName() + + "/statuses/" + status.getId()); } else { final DirectMessage dm = twitter.sendDirectMessage(handle, message); return new NoticeMessage(dm.getText()); @@ -134,14 +131,15 @@ public final class Twitter extends ThreadedModule { * @return The {@link Message} to send back. * @throws ModuleException If an error occurs while posting. */ - public Message post(final String handle, final String message, final boolean isDm) throws ModuleException { + public Message post(final String handle, final String message, final boolean isDm) + throws ModuleException { return twitterPost(properties.get(CONSUMER_KEY_PROP), - properties.get(CONSUMER_SECRET_PROP), - properties.get(TOKEN_PROP), - properties.get(TOKEN_SECRET_PROP), - handle, - message, - isDm); + properties.get(CONSUMER_SECRET_PROP), + properties.get(TOKEN_PROP), + properties.get(TOKEN_SECRET_PROP), + handle, + message, + isDm); } /** @@ -150,7 +148,8 @@ public final class Twitter extends ThreadedModule { @Override void run(final Mobibot bot, final String sender, final String cmd, final String message) { try { - bot.send(sender, post(sender, message, false).getMessage()); + bot.send(sender, + post(sender, message + " (by " + sender + " on " + bot.getChannel() + ')', false).getMessage()); } catch (ModuleException e) { bot.getLogger().warn(e.getDebugMessage(), e); bot.send(sender, e.getMessage());