agirbot/src/org/april/hebdobot/twitter/TwitterClient.java

112 lines
3.6 KiB
Java

/**
* Copyright (C) 2017 Christian Pierre MOMON <cmomon@april.org>
*
* This file is part of (April) Hebdobot.
*
* Hebdobot is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Hebdobot is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Hebdobot. If not, see <http://www.gnu.org/licenses/>
*/
package org.april.hebdobot.twitter;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import twitter4j.Status;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.conf.ConfigurationBuilder;
/**
* The Class TwitterClient.
*/
public class TwitterClient
{
private static final Logger logger = LoggerFactory.getLogger(TwitterClient.class);
private String consumerKey;
private String consumerSecret;
private String accessToken;
private String accessTokenSecret;
/**
* Instantiates a new twitter client.
*
* @param consumerKey
* the consumer key
* @param consumerSecret
* the consumer secret
* @param accessToken
* the access token
* @param accessTokenSecret
* the access token secret
*/
public TwitterClient(final String consumerKey, final String consumerSecret, final String accessToken, final String accessTokenSecret)
{
if (StringUtils.isBlank(consumerKey))
{
throw new IllegalArgumentException("Invalid blank consumer key.");
}
else if (StringUtils.isBlank(consumerSecret))
{
throw new IllegalArgumentException("Invalid blank consumer secret.");
}
else if (StringUtils.isBlank(accessToken))
{
throw new IllegalArgumentException("Invalid blank access token.");
}
else if (StringUtils.isBlank(accessTokenSecret))
{
throw new IllegalArgumentException("Invalid blank access token secret.");
}
else
{
this.consumerKey = consumerKey;
this.consumerSecret = consumerSecret;
this.accessToken = accessToken;
this.accessTokenSecret = accessTokenSecret;
}
}
/**
* Tweet.
*
* @param message
* the message
* @throws TwitterException
* the twitter exception
*/
public void tweet(final String message) throws TwitterException
{
if (StringUtils.isBlank(message))
{
logger.info("Empty message => tweet aborted.");
}
else
{
ConfigurationBuilder config = new ConfigurationBuilder();
config.setDebugEnabled(true);
config.setOAuthConsumerKey(this.consumerKey);
config.setOAuthConsumerSecret(this.consumerSecret);
config.setOAuthAccessToken(this.accessToken);
config.setOAuthAccessTokenSecret(this.accessTokenSecret);
Twitter twitter = new TwitterFactory(config.build()).getInstance();
Status status = twitter.updateStatus(message);
logger.info("Tweet result [" + status.getText() + "].");
}
}
}