Facebook Birthday Automated Thank You Post


SUBMITTED BY: Guest

DATE: Jan. 7, 2014, 1:25 p.m.

FORMAT: Java

SIZE: 4.6 kB

HITS: 894

  1. /* Author: Alessandro Pellegrini <alessandro.pellegrini@tin.it>
  2. * This class posts a Birthday Thank you on your Facebook wall
  3. * You must donwload Facebook4J for this to work.
  4. * Before running this script, log in to Facebook!
  5. * You must configure static variables in the class for it to work correctly!
  6. * Compile with:
  7. * javac -cp facebook4j-core-2.0.4.jar BirthdayThanks.java
  8. * Run with (on Windows):
  9. * java -cp facebook4j-core-2.0.4.jar;. BirthdayThanks
  10. * Check if the Facebook4J core jar has the correct name!
  11. * There is a dryRun flag which prevents the program from actually posting
  12. * to your Facebook wall. Set it to false after having tried its behaviour!
  13. * ...and Happy Birthday! :)
  14. */
  15. import facebook4j.Facebook;
  16. import facebook4j.FacebookException;
  17. import facebook4j.FacebookFactory;
  18. import facebook4j.ResponseList;
  19. import facebook4j.Post;
  20. import facebook4j.Reading;
  21. import facebook4j.auth.AccessToken;
  22. public class BirthdayThanks {
  23. // Get an access token from https://developers.facebook.com/tools/explorer
  24. // You must specify the following permissions when getting the token:
  25. // read_stream, publish_actions, publish_stream
  26. private static final String accessTokenString = "TOKEN HERE!";
  27. // This is the reply that will be posted to your birthday posts
  28. private static final String ThankYouMessage = "Thank you %s! :)";
  29. // If this flag is set to 'true', then the first name of the sender will be added to the thank you string.
  30. // Note that, for this to work, a %s must be placed in ThankYouMessage where the name should appear.
  31. // The first name is obtained as the first token in the poster name. So, if the name is 'My Long Name',
  32. // %s will be replaced with My.
  33. private static final boolean addName = true;
  34. // This is the text that will be searched for in the posts (case insensitive)
  35. // You can add as many as you want!
  36. private static final String[] HBtext = {"auguri", "compleanno", "auguroni", "birthday", "bday"};
  37. // This is to set your birthday. I will look all posts one day before and one day after your birthday (GMT time)
  38. private static final int bYear = 2014;
  39. private static final int bMonth = 1;
  40. private static final int bDay = 7;
  41. // If this flag is set to 'true', the prorgam will execute a dry run, i.e. no actual
  42. // reply will be posted on you Facebook wall!
  43. private static final boolean dryRun = true;
  44. /********************************************/
  45. /** DO NOT TOUCH ANYTHING BELOW THIS LINE! **/
  46. /********************************************/
  47. public static void main (String[] args) throws FacebookException {
  48. ResponseList<Post> posts = null;
  49. // Generate facebook instance.
  50. Facebook facebook = new FacebookFactory().getInstance();
  51. // Use default values for oauth app id.
  52. facebook.setOAuthAppId("", "");
  53. // Set access token.
  54. AccessToken at = new AccessToken(accessTokenString);
  55. facebook.setOAuthAccessToken(at);
  56. // Get relevant posts
  57. System.out.println("Retrieving posts...");
  58. Reading r = new Reading();
  59. r.until(bYear + "-" + bMonth + "-" + (bDay+1) + "T00:00:00+0000");
  60. r.since(bYear + "-" + bMonth + "-" + (bDay-1) + "T00:00:00+0000");
  61. r.limit(1000); // Ok, this is a bit too much maybe! :)
  62. posts = facebook.getFeed(r);
  63. // Loop on them
  64. int i = 0;
  65. for(Post t:posts) {
  66. if(t.getMessage() == null)
  67. continue;
  68. if(haveToThank(t.getMessage())) {
  69. String thankString;
  70. if(addName) {
  71. String[] currName = t.getFrom().getName().split(" ");
  72. thankString = String.format(ThankYouMessage, currName[0]);
  73. } else {
  74. thankString = ThankYouMessage;
  75. }
  76. System.out.println(++i + ". Thanking " + t.getFrom().getName() + " for post \"" + t.getMessage().substring(0, Math.min(t.getMessage().length(), 20)) + "\" with message \"" + thankString + "\"");
  77. if(!dryRun) {
  78. facebook.commentPost(t.getId(), thankString);
  79. try {
  80. Thread.sleep(1500);
  81. } catch (InterruptedException e) {}
  82. }
  83. }
  84. }
  85. System.out.println("Done.");
  86. }
  87. private static boolean haveToThank(String p){
  88. for(String s:HBtext) {
  89. if(p.toLowerCase().indexOf(s.toLowerCase()) != -1) {
  90. return true;
  91. }
  92. }
  93. return false;
  94. }
  95. }

comments powered by Disqus