use of twitter4j.Twitter in project twitter4j by yusuke.
the class SendDirectMessage method main.
/**
* Usage: java twitter4j.examples.directMessage.DirectMessage [recipient screen name] [message]
*
* @param args String[]
*/
public static void main(String[] args) {
if (args.length < 2) {
System.out.println("Usage: java twitter4j.examples.directmessage.SendDirectMessage [recipient screen name] [message]");
System.exit(-1);
}
Twitter twitter = new TwitterFactory().getInstance();
try {
DirectMessage message = twitter.sendDirectMessage(args[0], args[1]);
System.out.println("Direct message successfully sent to " + message.getRecipientScreenName());
System.exit(0);
} catch (TwitterException te) {
te.printStackTrace();
System.out.println("Failed to send a direct message: " + te.getMessage());
System.exit(-1);
}
}
use of twitter4j.Twitter in project asterixdb by apache.
the class TwitterUtil method getTwitterService.
public static Twitter getTwitterService(Map<String, String> configuration) {
ConfigurationBuilder cb = getAuthConfiguration(configuration);
TwitterFactory tf = null;
try {
tf = new TwitterFactory(cb.build());
} catch (Exception e) {
if (LOGGER.isLoggable(Level.WARNING)) {
StringBuilder builder = new StringBuilder();
builder.append("Twitter Adapter requires the following config parameters\n");
builder.append(AuthenticationConstants.OAUTH_CONSUMER_KEY + "\n");
builder.append(AuthenticationConstants.OAUTH_CONSUMER_SECRET + "\n");
builder.append(AuthenticationConstants.OAUTH_ACCESS_TOKEN + "\n");
builder.append(AuthenticationConstants.OAUTH_ACCESS_TOKEN_SECRET + "\n");
LOGGER.warning(builder.toString());
LOGGER.warning("Unable to configure Twitter adapter due to incomplete/incorrect authentication credentials");
LOGGER.warning("For details on how to obtain OAuth authentication token, visit https://dev.twitter.com/oauth" + "/overview/application-owner-access-tokens");
}
}
Twitter twitter = tf.getInstance();
return twitter;
}
use of twitter4j.Twitter in project camel by apache.
the class TwitterComponentVerifier method verifyCredentials.
private void verifyCredentials(ResultBuilder builder, Map<String, Object> parameters) throws Exception {
try {
TwitterConfiguration configuration = setProperties(new TwitterConfiguration(), parameters);
Twitter twitter = configuration.getTwitter();
twitter.verifyCredentials();
} catch (TwitterException e) {
// verifyCredentials throws TwitterException when Twitter service or
// network is unavailable or if supplied credential is wrong
ResultErrorBuilder errorBuilder = ResultErrorBuilder.withHttpCodeAndText(e.getStatusCode(), e.getErrorMessage()).attribute("twitter.error.code", e.getErrorCode()).attribute("twitter.status.code", e.getStatusCode()).attribute("twitter.exception.code", e.getExceptionCode()).attribute("twitter.exception.message", e.getMessage()).attribute("twitter.exception.instance", e);
// https://dev.twitter.com/overview/api/response-codes
if (e.getErrorCode() == 89) {
errorBuilder.parameter("accessToken");
}
builder.error(errorBuilder.build());
}
}
use of twitter4j.Twitter in project opennms by OpenNMS.
the class MicroblogDMNotificationStrategy method send.
/**
* {@inheritDoc}
*/
@Override
public int send(List<Argument> arguments) {
Twitter svc = buildUblogService(arguments);
String destUser = findDestName(arguments);
DirectMessage response;
if (destUser == null || "".equals(destUser)) {
LOG.error("Cannot send a microblog DM notice to a user with no microblog username set. Either set a microblog username for this OpenNMS user or use the MicroblogUpdateNotificationStrategy instead.");
return 1;
}
// In case the user tried to be helpful, avoid a double @@
if (destUser.startsWith("@"))
destUser = destUser.substring(1);
String fullMessage = buildMessageBody(arguments);
LOG.debug("Dispatching microblog DM notification at base URL '{}' with destination user '{}' and message '{}'", svc.getConfiguration().getClientURL(), destUser, fullMessage);
try {
response = svc.sendDirectMessage(destUser, fullMessage);
} catch (TwitterException e) {
LOG.error("Microblog notification failed at service URL '{}' to destination user '{}'", svc.getConfiguration().getClientURL(), destUser, e);
return 1;
}
LOG.info("Microblog DM notification succeeded: DM sent with ID {}", response.getId());
return 0;
}
use of twitter4j.Twitter in project twicalico by moko256.
the class ShowUserActivity method onOptionsItemSelected.
@Override
public boolean onOptionsItemSelected(MenuItem item) {
ThrowableFunc throwableFunc = null;
Twitter twitter = GlobalApplication.twitter;
switch(item.getItemId()) {
case R.id.action_share:
startActivity(Intent.createChooser(new Intent().setAction(Intent.ACTION_SEND).setType("text/plain").putExtra(Intent.EXTRA_TEXT, getShareUrl()), getString(R.string.share)));
break;
case R.id.action_open_in_browser:
AppCustomTabsKt.launchChromeCustomTabs(this, getShareUrl());
break;
case R.id.action_create_follow:
throwableFunc = () -> twitter.createFriendship(user.getId());
break;
case R.id.action_destroy_follow:
throwableFunc = () -> twitter.destroyFriendship(user.getId());
break;
case R.id.action_create_mute:
throwableFunc = () -> twitter.createMute(user.getId());
break;
case R.id.action_destroy_mute:
throwableFunc = () -> twitter.destroyMute(user.getId());
break;
case R.id.action_create_block:
throwableFunc = () -> twitter.createBlock(user.getId());
break;
case R.id.action_destroy_block:
throwableFunc = () -> twitter.destroyBlock(user.getId());
break;
case R.id.action_destroy_follow_follower:
throwableFunc = () -> {
twitter.createBlock(user.getId());
twitter.destroyBlock(user.getId());
};
break;
case R.id.action_spam_report:
throwableFunc = () -> GlobalApplication.twitter.reportSpam(user.getId());
break;
}
if (throwableFunc != null) {
ThrowableFunc finalThrowableFunc = throwableFunc;
confirmDialog(item.getTitle(), getString(R.string.confirm_message), () -> runAsWorkerThread(finalThrowableFunc));
}
return super.onOptionsItemSelected(item);
}
Aggregations