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

85 lines
2.7 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.springframework.social.twitter.api.impl.TwitterTemplate;
/**
* The Class TwitterClient.
*/
public class TwitterClient
{
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;
}
}
/**
* Send tweet.
*
* @param message
* the message
*/
public void tweet(final String message)
{
TwitterTemplate twitterClient = new TwitterTemplate(this.consumerKey, this.consumerSecret, this.accessToken, this.accessTokenSecret);
twitterClient.timelineOperations().updateStatus(message);
}
}