List of usage examples for twitter4j StatusUpdate setInReplyToStatusId
public void setInReplyToStatusId(long inReplyToStatusId)
From source file:br.com.porcelli.hornetq.integration.twitter.outgoing.impl.OutgoingTwitterHandler.java
License:Apache License
@Override public HandleStatus handle(final MessageReference ref) { System.out.println("Entrando no handle()"); synchronized (this) { ref.handled();/*w ww.jav a2s . c o m*/ final ServerMessage message = ref.getMessage(); try { String text2publish; if (message.containsProperty(TwitterConstants.KEY_TEXT)) { text2publish = message.getStringProperty(TwitterConstants.KEY_TEXT); } else { text2publish = message.getBodyBuffer().readString(); } if (text2publish == null || text2publish.trim().length() <= 0) { log.error(ERROR_MESSAGE_NOT_FILLED); throw new Exception(ERROR_MESSAGE_NOT_FILLED); } else if (text2publish.length() > 140) { log.warn(ERROR_MESSAGE_SIZE_EXCEED); text2publish = text2publish.substring(0, 139); } MessageType type = MessageType.TWEET; try { if (message.containsProperty(TwitterConstants.KEY_MSG_TYPE)) { type = MessageType .valueOf(message.getStringProperty(TwitterConstants.KEY_MSG_TYPE).toUpperCase()); } } catch (Exception e) { log.warn(ERROR_INVALID_MESSAGE_TYPE); } if (type == MessageType.DM) { if (!message.containsProperty(TwitterConstants.KEY_TO_USER_ID) && !message.containsProperty(TwitterConstants.KEY_TO_USER_SCREEN_NAME)) { log.error(ERROR_DM_DESTINY_NOT_FOUND); throw new Exception(ERROR_DM_DESTINY_NOT_FOUND); } DirectMessage sentMessage = null; if (message.containsProperty(TwitterConstants.KEY_TO_USER_ID)) { int userId = -1; try { userId = message.getIntProperty(TwitterConstants.KEY_TO_USER_ID); } catch (PropertyConversionException e) { userId = Integer.valueOf(message.getStringProperty(TwitterConstants.KEY_TO_USER_ID)); } sentMessage = twitter.sendDirectMessage(userId, text2publish); dmSent.incrementAndGet(); totalSent.incrementAndGet(); } else if (message.containsProperty(TwitterConstants.KEY_TO_USER_SCREEN_NAME)) { String userScreenName = message.getStringProperty(TwitterConstants.KEY_TO_USER_SCREEN_NAME); sentMessage = twitter.sendDirectMessage(userScreenName, text2publish); dmSent.incrementAndGet(); totalSent.incrementAndGet(); } if (sentMessage != null && sentQueue != null) { final ServerMessage msg = buildMessage(sentQueue.getName().toString(), sentMessage); msg.setAddress(sentQueue.getName()); msg.setDurable(true); postOffice.route(msg, false); } } else { final StatusUpdate status = new StatusUpdate(text2publish); if (message.containsProperty(TwitterConstants.KEY_IN_REPLY_TO_STATUS_ID)) { long reply2StatusId = 0L; try { reply2StatusId = message.getLongProperty(TwitterConstants.KEY_IN_REPLY_TO_STATUS_ID); } catch (PropertyConversionException e) { reply2StatusId = Long .valueOf(message.getStringProperty(TwitterConstants.KEY_IN_REPLY_TO_STATUS_ID)); } status.setInReplyToStatusId(reply2StatusId); } if (message.containsProperty(TwitterConstants.KEY_GEO_LATITUDE) && message.containsProperty(TwitterConstants.KEY_GEO_LONGITUDE)) { double geolat = 0.0D; double geolong = 0.0D; try { geolat = message.getDoubleProperty(TwitterConstants.KEY_GEO_LATITUDE); } catch (PropertyConversionException e) { geolat = Double.valueOf(message.getStringProperty(TwitterConstants.KEY_GEO_LATITUDE)); } try { geolong = message.getDoubleProperty(TwitterConstants.KEY_GEO_LONGITUDE); } catch (PropertyConversionException e) { geolong = Double.valueOf(message.getStringProperty(TwitterConstants.KEY_GEO_LONGITUDE)); } status.setLocation(new GeoLocation(geolat, geolong)); } if (message.containsProperty(TwitterConstants.KEY_PLACE_ID)) { status.setPlaceId(message.getStringProperty(TwitterConstants.KEY_PLACE_ID)); } if (message.containsProperty(TwitterConstants.KEY_DISPLAY_COODINATES)) { boolean displayCoordinated = false; try { displayCoordinated = message .getBooleanProperty(TwitterConstants.KEY_DISPLAY_COODINATES); } catch (PropertyConversionException e) { displayCoordinated = Boolean .valueOf(message.getStringProperty(TwitterConstants.KEY_DISPLAY_COODINATES)); } status.setDisplayCoordinates(displayCoordinated); } Status sentMessage = twitter.updateStatus(status); tweetSent.incrementAndGet(); totalSent.incrementAndGet(); if (sentMessage != null && sentQueue != null) { final ServerMessage msg = buildMessage(sentQueue.getName().toString(), sentMessage); msg.setAddress(sentQueue.getName()); msg.setDurable(true); postOffice.route(msg, false); } } } catch (Exception e) { mbean.notifyException(e); log.error("Error sending message.", e); if (errorQueue != null) { final ServerMessage msg = message.copy(); msg.setAddress(errorQueue.getName()); msg.setDurable(true); try { postOffice.route(msg, false); } catch (Exception e1) { mbean.notifyException(e1); } } } try { queue.acknowledge(ref); } catch (Exception e) { mbean.notifyException(e); log.error("Error acknowledging message.", e); } return HandleStatus.HANDLED; } }
From source file:com.appspot.bitlyminous.handler.twitter.NearByHandler.java
License:Apache License
@Override public StatusUpdate execute(Status tweet) { GeoLocation geoLocation = tweet.getGeoLocation(); String text = getMentionText(tweet); if (geoLocation == null) { StatusUpdate reply = new StatusUpdate( ApplicationResources.getLocalizedString("com.appspot.bitlyminous.message.noLocation", new String[] { "@" + tweet.getUser().getScreenName() })); reply.setInReplyToStatusId(tweet.getId()); return reply; } else {//w w w .j a va 2 s. c o m try { GeoQuery query = new GeoQuery(geoLocation); List<Tip> nearbyTips = getFoursquareGateway() .getNearbyTips(new com.appspot.bitlyminous.entity.GeoLocation(geoLocation.getLatitude(), geoLocation.getLongitude()), 1); String status = buildTipsStatus(nearbyTips); if (nearbyTips.isEmpty()) { List<Venue> nearbyVenues = new ArrayList<Venue>(); if (text != null) { nearbyVenues = getFoursquareGateway().getNearbyVenues( new com.appspot.bitlyminous.entity.GeoLocation(geoLocation.getLatitude(), geoLocation.getLongitude()), text, 1); } status = status + buildVenuesStatus(nearbyVenues); if (nearbyVenues.isEmpty()) { List<Place> nearbyPlaces = getTwitterClient().getNearbyPlaces(query); status = status + buildPlacesStatus(nearbyPlaces); } } status = "@" + tweet.getUser().getScreenName() + " " + status; StatusUpdate reply = new StatusUpdate(status); reply.setInReplyToStatusId(tweet.getId()); return reply; } catch (Exception e) { logger.log(Level.SEVERE, "Error while getting nearby places", e); } return null; } }
From source file:com.appspot.bitlyminous.handler.twitter.RetweetRelatedTweetsHandler.java
License:Apache License
@Override public StatusUpdate execute(Status tweet) { try {/*from w ww . ja v a 2 s . c om*/ WebSearchQuery webSearchQuery = getGoogleWebSearchQuery(); webSearchQuery.withCustomeSearchEngineId(ApplicationConstants.GOOGLE_API_CSE); List<Url> urls = context.getUrls(); for (Url entity : urls) { // List<Tweet> relatedTweets = new ArrayList<Tweet>(); List<String> tags = entity.getTags(); if (!tags.isEmpty()) { List<WebResult> relatedTweets = webSearchQuery.withQuery(createQuery(tags)).list(); // List<Tweet> tweets = getTweetsFromCSE(results); // relatedTweets.addAll(tweets); relatedTweets = getBestMatches(tweet, relatedTweets, 1); if (!relatedTweets.isEmpty()) { StatusUpdate reply = new StatusUpdate(buildStatus(tweet, relatedTweets)); reply.setInReplyToStatusId(tweet.getId()); return reply; } } } } catch (Exception e) { logger.log(Level.SEVERE, "Error while persisting urls.", e); } return null; }
From source file:com.appspot.bitlyminous.handler.twitter.ScanLinkHandler.java
License:Apache License
@Override public StatusUpdate execute(Status tweet) { try {// w w w .jav a 2 s . c om List<String> shortUrls = extractBitlyUrls(tweet.getText()); if (!shortUrls.isEmpty()) { Set<Url> longUrls = getBitlyClient() .call(Bitly.expand(shortUrls.toArray(new String[shortUrls.size()]))); GoogleSafeBrowsingGateway gateway = getGoogleSafeBrowsingGateway(); for (Url url : longUrls) { if (!isEmpty(url.getLongUrl())) { if (gateway.isBlacklisted(url.getLongUrl())) { StatusUpdate reply = new StatusUpdate(ApplicationResources.getLocalizedString( "com.appspot.bitlyminous.message.badurl", new String[] { "@" + tweet.getUser().getScreenName(), trimText(tweet.getText(), 20), ApplicationConstants.GOOGLE_SAFE_BROWSING_REF_URL })); reply.setInReplyToStatusId(tweet.getId()); return reply; } if (gateway.isMalwarelisted(url.getLongUrl())) { StatusUpdate reply = new StatusUpdate(ApplicationResources.getLocalizedString( "com.appspot.bitlyminous.message.badurl", new String[] { "@" + tweet.getUser().getScreenName(), trimText(tweet.getText(), 20), ApplicationConstants.GOOGLE_SAFE_BROWSING_REF_URL })); reply.setInReplyToStatusId(tweet.getId()); return reply; } } } } } catch (Exception e) { logger.log(Level.SEVERE, "Error while scanning urls.", e); } return null; }
From source file:com.appspot.bitlyminous.handler.twitter.SearchHandler.java
License:Apache License
@Override public StatusUpdate execute(Status tweet) { GoogleSearchQueryFactory factory = GoogleSearchQueryFactory .newInstance(ApplicationConstants.GOOGLE_API_KEY); WebSearchQuery query = factory.newWebSearchQuery(); query.setReferrer(ApplicationConstants.GOOGLE_API_REFERER); PagedList<WebResult> response = query.withQuery(getMentionText(tweet)).list(); if (response.isEmpty()) { return null; } else {/*w ww . ja v a 2 s.co m*/ StatusUpdate update = new StatusUpdate( "@" + tweet.getUser().getScreenName() + " " + buildStatus(response.get(0))); update.setInReplyToStatusId(tweet.getId()); return update; } }
From source file:com.appspot.bitlyminous.handler.twitter.SendRelatedUrlsHandler.java
License:Apache License
@Override public StatusUpdate execute(Status tweet) { try {//w ww . j a v a2 s. c o m DeliciousGateway deliciousGateway = getDeliciousGateway(context.getVersion()); DiggGateway diggGateway = getDiggGateway(); List<Url> urls = context.getUrls(); for (Url entity : urls) { WebSearchQuery googleQuery = getGoogleWebSearchQuery(); googleQuery.withRelatedSite(entity.getUrl()); List<Url> relatedUrls = new ArrayList<Url>(); List<String> tags = entity.getTags(); if (!tags.isEmpty()) { for (String tag : tags) { try { relatedUrls.addAll(deliciousGateway.getPopularUrls(tag)); relatedUrls.addAll(convertToUrls(diggGateway.getPopularStories(tag, 5))); } catch (Exception e) { logger.log(Level.WARNING, "Error while getting related urls.", e); } } relatedUrls.addAll(convertToUrls(googleQuery.withQuery(createQuery(tags)).list())); relatedUrls = getBestMatches(entity, relatedUrls, 2); } if (!relatedUrls.isEmpty()) { StatusUpdate reply = new StatusUpdate( "@" + tweet.getUser().getScreenName() + " " + buildStatus(relatedUrls)); reply.setInReplyToStatusId(tweet.getId()); return reply; } } } catch (Exception e) { logger.log(Level.SEVERE, "Error while sending related urls.", e); } return null; }
From source file:com.appspot.bitlyminous.handler.twitter.TweetForMeHandler.java
License:Apache License
@Override public StatusUpdate execute(Status tweet) { try {//from w w w .ja v a 2 s.c o m User user = context.getUser(); if (user == null) { user = getUserByScreenName(tweet.getUser().getScreenName()); context.setUser(user); } if (user == null || user.getTwitterToken() == null || user.getTwitterSecret() == null) { StatusUpdate reply = new StatusUpdate( ApplicationResources.getLocalizedString("com.appspot.bitlyminous.message.notAuthenticated", new String[] { "@" + tweet.getUser().getScreenName(), "Twitter", shortenUrl(ApplicationConstants.TWITTER_CALLBACK_URL) })); reply.setInReplyToStatusId(tweet.getId()); return reply; } else { Twitter twitter = getTwitterClient(user.getTwitterToken().getValue(), user.getTwitterSecret().getValue()); String status = buildStatus(user.getUrls(), user.getTags()); if (status != null) { StatusUpdate reply = new StatusUpdate(status); reply.setInReplyToStatusId(tweet.getId()); twitter.updateStatus(reply); } return null; } } catch (Exception e) { logger.log(Level.SEVERE, "Error while tweeting on behalf of user.", e); } return null; }
From source file:com.daiv.android.twitter.services.SendTweet.java
License:Apache License
public boolean sendTweet(AppSettings settings, Context context) { try {/*from w w w .ja v a2 s .c o m*/ Twitter twitter = getTwitter(); if (remainingChars < 0 && !pwiccer) { // twitlonger goes here TwitLongerHelper helper = new TwitLongerHelper(message, twitter); helper.setInReplyToStatusId(tweetId); return helper.createPost() != 0; } else { twitter4j.StatusUpdate reply = new twitter4j.StatusUpdate(message); reply.setInReplyToStatusId(tweetId); if (!attachedUri.equals("")) { File outputDir = context.getCacheDir(); // context being the Activity pointer File f = File.createTempFile("compose", "picture", outputDir); Bitmap bitmap = getBitmapToSend(Uri.parse(attachedUri), context); ByteArrayOutputStream bos = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bos); byte[] bitmapdata = bos.toByteArray(); FileOutputStream fos = new FileOutputStream(f); fos.write(bitmapdata); fos.flush(); fos.close(); if (!settings.twitpic) { reply.setMedia(f); twitter.updateStatus(reply); return true; } else { TwitPicHelper helper = new TwitPicHelper(twitter, message, f, context); helper.setInReplyToStatusId(tweetId); return helper.createPost() != 0; } } else { // no picture twitter.updateStatus(reply); return true; } } } catch (Exception e) { e.printStackTrace(); return false; } }
From source file:com.daiv.android.twitter.utils.api_helper.TwitLongerHelper.java
License:Apache License
/** * posts the status onto Twitlonger, it then posts the shortened status (with link) to the user's twitter and updates the status on twitlonger * to include the posted status's id./*ww w . j a va 2 s . c o m*/ * * @return id of the status that was posted to twitter */ public long createPost() { TwitLongerStatus status = postToTwitLonger(); long statusId; try { Status postedStatus; StatusUpdate update = new StatusUpdate(status.getText()); if (replyToStatusId != 0) { update.setInReplyToStatusId(replyToStatusId); } if (location != null) { update.setLocation(location); } postedStatus = twitter.updateStatus(update); statusId = postedStatus.getId(); updateTwitlonger(status, statusId); } catch (Exception e) { e.printStackTrace(); statusId = 0; } // if zero, then it failed return statusId; }
From source file:com.daiv.android.twitter.utils.api_helper.TwitPicHelper.java
License:Apache License
/** * posts the status onto Twitlonger, it then posts the shortened status (with link) to the user's twitter and updates the status on twitlonger * to include the posted status's id.//from ww w . j a v a2s . co m * * @return id of the status that was posted to twitter */ public long createPost() { TwitPicStatus status = uploadToTwitPic(); Log.v("Test_twitpic", "past upload"); long statusId; try { Status postedStatus; StatusUpdate update = new StatusUpdate(status.getText()); if (replyToStatusId != 0) { update.setInReplyToStatusId(replyToStatusId); } if (location != null) { update.setLocation(location); } postedStatus = twitter.updateStatus(update); statusId = postedStatus.getId(); } catch (Exception e) { e.printStackTrace(); statusId = 0; } // if zero, then it failed return statusId; }