Adeed automatic posting of channel links to Twitter.
This commit is contained in:
parent
92576f9496
commit
fd6c1309e3
4 changed files with 49 additions and 29 deletions
|
@ -37,6 +37,9 @@ tell-max-size=50
|
||||||
# Twitter handle to receive channel join notifications
|
# Twitter handle to receive channel join notifications
|
||||||
#twitter-handle=
|
#twitter-handle=
|
||||||
|
|
||||||
|
# Automatically post links to twitter
|
||||||
|
#twitter-auto-post=true
|
||||||
|
|
||||||
#
|
#
|
||||||
# Create custom search engine at: https://cse.google.com/
|
# Create custom search engine at: https://cse.google.com/
|
||||||
# and get API key from: https://console.developers.google.com/
|
# and get API key from: https://console.developers.google.com/
|
||||||
|
|
|
@ -46,21 +46,22 @@ public final class Constants {
|
||||||
* The connect/read timeout in ms.
|
* The connect/read timeout in ms.
|
||||||
*/
|
*/
|
||||||
public static final int CONNECT_TIMEOUT = 5000;
|
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.
|
* Default locale.
|
||||||
*/
|
*/
|
||||||
public static final Locale LOCALE = Locale.getDefault();
|
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.
|
* Disables the default constructor.
|
||||||
|
|
|
@ -176,6 +176,8 @@ public class Mobibot extends PircBot {
|
||||||
// The tell object.
|
// The tell object.
|
||||||
private final Tell tell;
|
private final Tell tell;
|
||||||
// The Twitter handle for channel join notifications.
|
// The Twitter handle for channel join notifications.
|
||||||
|
// Automatically post links to Twitter
|
||||||
|
private final boolean twitterAutoPost;
|
||||||
private final String twitterHandle;
|
private final String twitterHandle;
|
||||||
// The Twitter module.
|
// The Twitter module.
|
||||||
private final Twitter twitterModule;
|
private final Twitter twitterModule;
|
||||||
|
@ -284,6 +286,7 @@ public class Mobibot extends PircBot {
|
||||||
twitterModule = new Twitter();
|
twitterModule = new Twitter();
|
||||||
MODULES.add(twitterModule);
|
MODULES.add(twitterModule);
|
||||||
twitterHandle = p.getProperty(Constants.TWITTER_HANDLE_PROP, "");
|
twitterHandle = p.getProperty(Constants.TWITTER_HANDLE_PROP, "");
|
||||||
|
twitterAutoPost = Boolean.parseBoolean(p.getProperty(Constants.TWITTER_AUTOPOST_PROP, "false"));
|
||||||
|
|
||||||
MODULES.add(new War());
|
MODULES.add(new War());
|
||||||
MODULES.add(new Weather2());
|
MODULES.add(new Weather2());
|
||||||
|
@ -970,6 +973,20 @@ public class Mobibot extends PircBot {
|
||||||
pinboard.addPost(entry);
|
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);
|
saveEntries(isBackup);
|
||||||
|
|
||||||
if (Constants.NO_TITLE.equals(entry.getTitle())) {
|
if (Constants.NO_TITLE.equals(entry.getTitle())) {
|
||||||
|
|
|
@ -48,12 +48,12 @@ import twitter4j.conf.ConfigurationBuilder;
|
||||||
* @since 1.0
|
* @since 1.0
|
||||||
*/
|
*/
|
||||||
public final class Twitter extends ThreadedModule {
|
public final class Twitter extends ThreadedModule {
|
||||||
// The property keys.
|
// Property keys
|
||||||
static final String CONSUMER_KEY_PROP = "twitter-consumerKey";
|
static final String CONSUMER_KEY_PROP = "twitter-consumerKey";
|
||||||
static final String CONSUMER_SECRET_PROP = "twitter-consumerSecret";
|
static final String CONSUMER_SECRET_PROP = "twitter-consumerSecret";
|
||||||
static final String TOKEN_PROP = "twitter-token";
|
static final String TOKEN_PROP = "twitter-token";
|
||||||
static final String TOKEN_SECRET_PROP = "twitter-tokenSecret";
|
static final String TOKEN_SECRET_PROP = "twitter-tokenSecret";
|
||||||
// The twitter command.
|
// Twitter command
|
||||||
private static final String TWITTER_CMD = "twitter";
|
private static final String TWITTER_CMD = "twitter";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -90,18 +90,15 @@ public final class Twitter extends ThreadedModule {
|
||||||
final boolean isDm) throws ModuleException {
|
final boolean isDm) throws ModuleException {
|
||||||
try {
|
try {
|
||||||
final ConfigurationBuilder cb = new ConfigurationBuilder();
|
final ConfigurationBuilder cb = new ConfigurationBuilder();
|
||||||
cb.setDebugEnabled(true)
|
cb.setDebugEnabled(true).setOAuthConsumerKey(consumerKey).setOAuthConsumerSecret(consumerSecret)
|
||||||
.setOAuthConsumerKey(consumerKey)
|
.setOAuthAccessToken(token).setOAuthAccessTokenSecret(tokenSecret);
|
||||||
.setOAuthConsumerSecret(consumerSecret)
|
|
||||||
.setOAuthAccessToken(token)
|
|
||||||
.setOAuthAccessTokenSecret(tokenSecret);
|
|
||||||
|
|
||||||
final TwitterFactory tf = new TwitterFactory(cb.build());
|
final TwitterFactory tf = new TwitterFactory(cb.build());
|
||||||
final twitter4j.Twitter twitter = tf.getInstance();
|
final twitter4j.Twitter twitter = tf.getInstance();
|
||||||
|
|
||||||
if (!isDm) {
|
if (!isDm) {
|
||||||
final Status status = twitter.updateStatus(message + " (" + handle + ')');
|
final Status status = twitter.updateStatus(message);
|
||||||
return new NoticeMessage("You message was posted to http://twitter.com/" + twitter.getScreenName()
|
return new NoticeMessage("You message was posted to https://twitter.com/" + twitter.getScreenName()
|
||||||
+ "/statuses/" + status.getId());
|
+ "/statuses/" + status.getId());
|
||||||
} else {
|
} else {
|
||||||
final DirectMessage dm = twitter.sendDirectMessage(handle, message);
|
final DirectMessage dm = twitter.sendDirectMessage(handle, message);
|
||||||
|
@ -134,7 +131,8 @@ public final class Twitter extends ThreadedModule {
|
||||||
* @return The {@link Message} to send back.
|
* @return The {@link Message} to send back.
|
||||||
* @throws ModuleException If an error occurs while posting.
|
* @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),
|
return twitterPost(properties.get(CONSUMER_KEY_PROP),
|
||||||
properties.get(CONSUMER_SECRET_PROP),
|
properties.get(CONSUMER_SECRET_PROP),
|
||||||
properties.get(TOKEN_PROP),
|
properties.get(TOKEN_PROP),
|
||||||
|
@ -150,7 +148,8 @@ public final class Twitter extends ThreadedModule {
|
||||||
@Override
|
@Override
|
||||||
void run(final Mobibot bot, final String sender, final String cmd, final String message) {
|
void run(final Mobibot bot, final String sender, final String cmd, final String message) {
|
||||||
try {
|
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) {
|
} catch (ModuleException e) {
|
||||||
bot.getLogger().warn(e.getDebugMessage(), e);
|
bot.getLogger().warn(e.getDebugMessage(), e);
|
||||||
bot.send(sender, e.getMessage());
|
bot.send(sender, e.getMessage());
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue