Skip to content

Instantly share code, notes, and snippets.

@paulvictor
Created July 10, 2012 16:13
Show Gist options
  • Save paulvictor/3084391 to your computer and use it in GitHub Desktop.
Save paulvictor/3084391 to your computer and use it in GitHub Desktop.
Facebook birthday replier
#!/usr/bin/env ruby
require 'rest-client'
require 'json'
require 'time'
require 'date'
at = ENV['access_token']
if at.nil?
$stderr.write 'go get an access token at https://developers.facebook.com/tools/explorer/ with permissions to read your birthday, feed and post on your behalf'
exit 1
end
fb_base_uri = "https://graph.facebook.com"
profile = RestClient.get(fb_base_uri + "/me?access_token=#{at}")
bday = JSON.parse(profile)['birthday']
the_month, the_day, the_year = bday.split('/') # month,day,year
rsp = RestClient.get(fb_base_uri + "/me/feed?access_token=#{at}&fields=created_time,from,message&limit=120")
wishes = JSON.parse(rsp)['data']
wishes.each{|wish|
begin
#skip if there is no belated or if the post isnt from my b'day
post_date = Time.parse(wish['created_time']).to_date
next unless wish['message']
next if (!wish['message'].match(/belated|b'lated/i)) && (!(post_date.day == the_day.to_i && post_date.month == the_month.to_i))
id = wish['from']['id']
puts id
puts "replying to #{wish['from']['name']}"
resp = RestClient.post(fb_base_uri + "/#{id}/feed", {"message" => "Hey, thanks for the wishes..", "access_token" => at})
puts resp
rescue Exception => xcp
puts xcp.inspect
puts id
end
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment