/* Author: Alessandro Pellegrini * This class posts a Birthday Thank you on your Facebook wall * You must donwload Facebook4J for this to work. * Before running this script, log in to Facebook! * You must configure static variables in the class for it to work correctly! * Compile with: * javac -cp facebook4j-core-2.0.4.jar BirthdayThanks.java * Run with (on Windows): * java -cp facebook4j-core-2.0.4.jar;. BirthdayThanks * Check if the Facebook4J core jar has the correct name! * There is a dryRun flag which prevents the program from actually posting * to your Facebook wall. Set it to false after having tried its behaviour! * ...and Happy Birthday! :) */ import facebook4j.Facebook; import facebook4j.FacebookException; import facebook4j.FacebookFactory; import facebook4j.ResponseList; import facebook4j.Post; import facebook4j.Reading; import facebook4j.auth.AccessToken; public class BirthdayThanks { // Get an access token from https://developers.facebook.com/tools/explorer // You must specify the following permissions when getting the token: // read_stream, publish_actions, publish_stream private static final String accessTokenString = "TOKEN HERE!"; // This is the reply that will be posted to your birthday posts private static final String ThankYouMessage = "Thank you %s! :)"; // If this flag is set to 'true', then the first name of the sender will be added to the thank you string. // Note that, for this to work, a %s must be placed in ThankYouMessage where the name should appear. // The first name is obtained as the first token in the poster name. So, if the name is 'My Long Name', // %s will be replaced with My. private static final boolean addName = true; // This is the text that will be searched for in the posts (case insensitive) // You can add as many as you want! private static final String[] HBtext = {"auguri", "compleanno", "auguroni", "birthday", "bday"}; // This is to set your birthday. I will look all posts one day before and one day after your birthday (GMT time) private static final int bYear = 2014; private static final int bMonth = 1; private static final int bDay = 7; // If this flag is set to 'true', the prorgam will execute a dry run, i.e. no actual // reply will be posted on you Facebook wall! private static final boolean dryRun = true; /********************************************/ /** DO NOT TOUCH ANYTHING BELOW THIS LINE! **/ /********************************************/ public static void main (String[] args) throws FacebookException { ResponseList posts = null; // Generate facebook instance. Facebook facebook = new FacebookFactory().getInstance(); // Use default values for oauth app id. facebook.setOAuthAppId("", ""); // Set access token. AccessToken at = new AccessToken(accessTokenString); facebook.setOAuthAccessToken(at); // Get relevant posts System.out.println("Retrieving posts..."); Reading r = new Reading(); r.until(bYear + "-" + bMonth + "-" + (bDay+1) + "T00:00:00+0000"); r.since(bYear + "-" + bMonth + "-" + (bDay-1) + "T00:00:00+0000"); r.limit(1000); // Ok, this is a bit too much maybe! :) posts = facebook.getFeed(r); // Loop on them int i = 0; for(Post t:posts) { if(t.getMessage() == null) continue; if(haveToThank(t.getMessage())) { String thankString; if(addName) { String[] currName = t.getFrom().getName().split(" "); thankString = String.format(ThankYouMessage, currName[0]); } else { thankString = ThankYouMessage; } System.out.println(++i + ". Thanking " + t.getFrom().getName() + " for post \"" + t.getMessage().substring(0, Math.min(t.getMessage().length(), 20)) + "\" with message \"" + thankString + "\""); if(!dryRun) { facebook.commentPost(t.getId(), thankString); try { Thread.sleep(1500); } catch (InterruptedException e) {} } } } System.out.println("Done."); } private static boolean haveToThank(String p){ for(String s:HBtext) { if(p.toLowerCase().indexOf(s.toLowerCase()) != -1) { return true; } } return false; } }