Added twitter notification on join.
This commit is contained in:
parent
474a31bdee
commit
8f408d1f46
4 changed files with 170 additions and 34 deletions
|
@ -34,6 +34,9 @@ tell-max-size=50
|
||||||
#twitter-token=
|
#twitter-token=
|
||||||
#twitter-tokenSecret=
|
#twitter-tokenSecret=
|
||||||
|
|
||||||
|
# Twitter handle to receive channel join notifications
|
||||||
|
#twitter-handle=
|
||||||
|
|
||||||
#
|
#
|
||||||
# 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/
|
||||||
|
|
|
@ -287,7 +287,11 @@ public class Mobibot extends PircBot {
|
||||||
MODULES.add(new Lookup());
|
MODULES.add(new Lookup());
|
||||||
MODULES.add(new Ping());
|
MODULES.add(new Ping());
|
||||||
MODULES.add(new StockQuote());
|
MODULES.add(new StockQuote());
|
||||||
MODULES.add(new Twitter());
|
|
||||||
|
twitterModule = new Twitter();
|
||||||
|
MODULES.add(twitterModule);
|
||||||
|
twitterHandle = p.getProperty(Constants.TWITTER_HANDLE_PROP, "");
|
||||||
|
|
||||||
MODULES.add(new War());
|
MODULES.add(new War());
|
||||||
MODULES.add(new Weather2());
|
MODULES.add(new Weather2());
|
||||||
MODULES.add(new WorldTime());
|
MODULES.add(new WorldTime());
|
||||||
|
@ -919,19 +923,18 @@ public class Mobibot extends PircBot {
|
||||||
*/
|
*/
|
||||||
public final void joinChannel() {
|
public final void joinChannel() {
|
||||||
joinChannel(ircChannel);
|
joinChannel(ircChannel);
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
if (twitterModule.isEnabled() && Utils.isValidString(twitterHandle)) {
|
||||||
* {@inheritDoc}
|
new Thread(() -> {
|
||||||
*/
|
try {
|
||||||
@Override
|
twitterModule.post(
|
||||||
protected final void onAction(final String sender,
|
twitterHandle,
|
||||||
final String login,
|
getName() + ReleaseInfo.VERSION + " has joined " + ircChannel,
|
||||||
final String hostname,
|
true);
|
||||||
final String target,
|
} catch (ModuleException e) {
|
||||||
final String action) {
|
logger.warn(e.getMessage(), e);
|
||||||
if (target.equals(ircChannel)) {
|
}
|
||||||
storeRecap(sender, action, true);
|
}).start();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -33,6 +33,9 @@
|
||||||
package net.thauvin.erik.mobibot.modules;
|
package net.thauvin.erik.mobibot.modules;
|
||||||
|
|
||||||
import net.thauvin.erik.mobibot.Mobibot;
|
import net.thauvin.erik.mobibot.Mobibot;
|
||||||
|
import net.thauvin.erik.mobibot.msg.Message;
|
||||||
|
import net.thauvin.erik.mobibot.msg.NoticeMessage;
|
||||||
|
import twitter4j.DirectMessage;
|
||||||
import twitter4j.Status;
|
import twitter4j.Status;
|
||||||
import twitter4j.TwitterFactory;
|
import twitter4j.TwitterFactory;
|
||||||
import twitter4j.conf.ConfigurationBuilder;
|
import twitter4j.conf.ConfigurationBuilder;
|
||||||
|
@ -46,10 +49,10 @@ import twitter4j.conf.ConfigurationBuilder;
|
||||||
*/
|
*/
|
||||||
public final class Twitter extends ThreadedModule {
|
public final class Twitter extends ThreadedModule {
|
||||||
// The property keys.
|
// The property keys.
|
||||||
private static final String CONSUMER_KEY_PROP = "twitter-consumerKey";
|
static final String CONSUMER_KEY_PROP = "twitter-consumerKey";
|
||||||
private static final String CONSUMER_SECRET_PROP = "twitter-consumerSecret";
|
static final String CONSUMER_SECRET_PROP = "twitter-consumerSecret";
|
||||||
private static final String TOKEN_PROP = "twitter-token";
|
static final String TOKEN_PROP = "twitter-token";
|
||||||
private static final String TOKEN_SECRET_PROP = "twitter-tokenSecret";
|
static final String TOKEN_SECRET_PROP = "twitter-tokenSecret";
|
||||||
// The twitter command.
|
// The twitter command.
|
||||||
private static final String TWITTER_CMD = "twitter";
|
private static final String TWITTER_CMD = "twitter";
|
||||||
|
|
||||||
|
@ -64,6 +67,50 @@ public final class Twitter extends ThreadedModule {
|
||||||
properties.put(TOKEN_SECRET_PROP, "");
|
properties.put(TOKEN_SECRET_PROP, "");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Post on Twitter.
|
||||||
|
*
|
||||||
|
* @param consumerKey The consumer key.
|
||||||
|
* @param consumerSecret The consumer secret.
|
||||||
|
* @param token The token.
|
||||||
|
* @param tokenSecret The token secret.
|
||||||
|
* @param handle The Twitter handle (dm) or nickname.
|
||||||
|
* @param message The message to post.
|
||||||
|
* @param isDm The direct message flag.
|
||||||
|
* @return The {@link Message} to send back.
|
||||||
|
* @throws ModuleException If an error occurs while posting.
|
||||||
|
*/
|
||||||
|
static Message twitterPost(final String consumerKey,
|
||||||
|
final String consumerSecret,
|
||||||
|
final String token,
|
||||||
|
final String tokenSecret,
|
||||||
|
final String handle,
|
||||||
|
final String message,
|
||||||
|
final boolean isDm) throws ModuleException {
|
||||||
|
try {
|
||||||
|
final ConfigurationBuilder cb = new ConfigurationBuilder();
|
||||||
|
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());
|
||||||
|
} else {
|
||||||
|
final DirectMessage dm = twitter.sendDirectMessage(handle, message);
|
||||||
|
return new NoticeMessage(dm.getText());
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new ModuleException("twitterPost(" + message + ")", "An error has occurred: " + e.getMessage(), e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@inheritDoc}
|
* {@inheritDoc}
|
||||||
*/
|
*/
|
||||||
|
@ -77,29 +124,35 @@ public final class Twitter extends ThreadedModule {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Post on Twitter.
|
||||||
|
*
|
||||||
|
* @param handle The Twitter handle (dm) or nickname.
|
||||||
|
* @param message The message to post.
|
||||||
|
* @param isDm The direct message flag.
|
||||||
|
* @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 {
|
||||||
|
return twitterPost(properties.get(CONSUMER_KEY_PROP),
|
||||||
|
properties.get(CONSUMER_SECRET_PROP),
|
||||||
|
properties.get(TOKEN_PROP),
|
||||||
|
properties.get(TOKEN_SECRET_PROP),
|
||||||
|
handle,
|
||||||
|
message,
|
||||||
|
isDm);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Posts to twitter.
|
* Posts to twitter.
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
void run(final Mobibot bot, final String sender, final String message) {
|
void run(final Mobibot bot, final String sender, final String message) {
|
||||||
try {
|
try {
|
||||||
final ConfigurationBuilder cb = new ConfigurationBuilder();
|
bot.send(sender, post(sender, message, false).getMessage());
|
||||||
cb.setDebugEnabled(true)
|
} catch (ModuleException e) {
|
||||||
.setOAuthConsumerKey(properties.get(CONSUMER_KEY_PROP))
|
bot.getLogger().warn(e.getDebugMessage(), e);
|
||||||
.setOAuthConsumerSecret(properties.get(CONSUMER_SECRET_PROP))
|
bot.send(sender, e.getMessage());
|
||||||
.setOAuthAccessToken(properties.get(TOKEN_PROP))
|
|
||||||
.setOAuthAccessTokenSecret(properties.get(TOKEN_SECRET_PROP));
|
|
||||||
final TwitterFactory tf = new TwitterFactory(cb.build());
|
|
||||||
final twitter4j.Twitter twitter = tf.getInstance();
|
|
||||||
|
|
||||||
final Status status = twitter.updateStatus(message + " (" + sender + ')');
|
|
||||||
|
|
||||||
bot.send(sender,
|
|
||||||
"You message was posted to http://twitter.com/" + twitter.getScreenName() + "/statuses/" + status
|
|
||||||
.getId());
|
|
||||||
} catch (Exception e) {
|
|
||||||
bot.getLogger().warn("Unable to post to Twitter: " + message, e);
|
|
||||||
bot.send(sender, "An error has occurred: " + e.getMessage());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,77 @@
|
||||||
|
/*
|
||||||
|
* TwitterTest.java
|
||||||
|
*
|
||||||
|
* Copyright (c) 2004-2019, Erik C. Thauvin (erik@thauvin.net)
|
||||||
|
* 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 this project 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 THE COPYRIGHT HOLDERS AND CONTRIBUTORS "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 THE COPYRIGHT HOLDER OR CONTRIBUTORS 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 net.thauvin.erik.mobibot.modules;
|
||||||
|
|
||||||
|
import net.thauvin.erik.mobibot.Constants;
|
||||||
|
import org.testng.annotations.Test;
|
||||||
|
|
||||||
|
import java.net.InetAddress;
|
||||||
|
import java.net.UnknownHostException;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The <code>TwitterTest</code> class.
|
||||||
|
*
|
||||||
|
* @author <a href="https://erik.thauvin.net/" target="_blank">Erik C. Thauvin</a>
|
||||||
|
* @created 2019-04-19
|
||||||
|
* @since 1.0
|
||||||
|
*/
|
||||||
|
public class TwitterTest {
|
||||||
|
private String getCi() {
|
||||||
|
if ("true".equals(System.getenv("CIRCLECI"))) {
|
||||||
|
return "CircleCI";
|
||||||
|
} else if ("true".equals(System.getenv("TRAVIS"))) {
|
||||||
|
return "Travis CI";
|
||||||
|
} else {
|
||||||
|
try {
|
||||||
|
return InetAddress.getLocalHost().getHostName();
|
||||||
|
} catch (UnknownHostException e) {
|
||||||
|
return "Unknown Host";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testPostTwitter() throws ModuleException {
|
||||||
|
final String msg = "Testing Twitter API from " + getCi();
|
||||||
|
assertThat(Twitter.twitterPost(
|
||||||
|
LocalProperties.getProperty(Twitter.CONSUMER_KEY_PROP),
|
||||||
|
LocalProperties.getProperty(Twitter.CONSUMER_SECRET_PROP),
|
||||||
|
LocalProperties.getProperty(Twitter.TOKEN_PROP),
|
||||||
|
LocalProperties.getProperty(Twitter.TOKEN_SECRET_PROP),
|
||||||
|
LocalProperties.getProperty(Constants.TWITTER_HANDLE_PROP),
|
||||||
|
msg,
|
||||||
|
true).getMessage()).as("twitterPost(" + msg + ')').isEqualTo(msg);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue