157 lines
4.9 KiB
Java
157 lines
4.9 KiB
Java
/**
|
|
* Copyright (C) 2017-2018 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 java.io.File;
|
|
|
|
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.UploadedMedia;
|
|
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 Status tweet(final String message) throws TwitterException
|
|
{
|
|
Status result;
|
|
|
|
if (StringUtils.isBlank(message))
|
|
{
|
|
logger.info("Empty message => tweet aborted.");
|
|
result = null;
|
|
}
|
|
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();
|
|
|
|
result = twitter.updateStatus(message);
|
|
logger.info("Tweet result [" + result.getText() + "].");
|
|
}
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
public Status tweet(final String message, final File image) throws TwitterException
|
|
{
|
|
Status result;
|
|
|
|
if (StringUtils.isBlank(message))
|
|
{
|
|
logger.info("Empty message => tweet aborted.");
|
|
result = null;
|
|
}
|
|
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();
|
|
|
|
result = twitter.updateStatus("Test 1");
|
|
UploadedMedia um = twitter.uploadMedia(new File("/home/cpm/C/Hebdobot/TestConf/revue-hebdomadaire.png"));
|
|
result = twitter.updateStatus("Test 2");
|
|
result = twitter.updateStatus("Test 3");
|
|
|
|
logger.info("Tweet result [" + result.getText() + "].");
|
|
}
|
|
// if ((imageFile != null) && (imageFile.exists()))
|
|
// {
|
|
// twitter
|
|
// }
|
|
|
|
//
|
|
return result;
|
|
}
|
|
}
|