Last active
October 27, 2023 15:59
-
-
Save vireshas/6723476 to your computer and use it in GitHub Desktop.
Facebook birthday replier
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# Usage: | |
# replier.rb <pass access token here> | |
# access_token="your access token goes here" replier.rb | |
# | |
# If your access_token is expired, you can get a 400 bad request exception. Go get a fresh access_token in that case. | |
require 'rest-client' | |
require 'json' | |
require 'time' | |
require 'date' | |
at = ENV['access_token'] || ARGV[0] | |
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("/") | |
rsp = RestClient.get(fb_base_uri + URI.encode("/me?fields=id,name,feed.limit(200).fields(created_time,id,message,comments,name,from)&access_token=#{at}")) | |
wishes = JSON.parse(rsp)['feed']['data'] | |
responses = ["Hey, thank you ", "Hey, thanks ", "Thank you ", "Thanks "] | |
smileys = [" :)", " :-)", " :):)", " :^)"] | |
#not using this pool at the moment | |
special_pool = {"rushikesh" => "rushya, goundya", "srinidhi" => "nidhi.py ;)"} #customized reponses | |
wishes.each {|wish| | |
begin | |
next unless wish['message'] | |
#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 if (!wish['message'].match(/belated|b'lated/i)) && (!(post_date.day == the_day.to_i && post_date.month == the_month.to_i)) | |
#return if you have already replied | |
return if wish['comments'] | |
from = wish['from']['name'].split(/ /).first | |
special_response = wish["message"].match(/(le)|(macha)/) | |
north_karnataka_maryade = "le " if special_response && special_response[1] | |
south_karnataka_maryade = "macha " if special_response && special_response[2] | |
from = (north_karnataka_maryade || south_karnataka_maryade || "" ) + from | |
pick_a_response = responses[rand(responses.count)] | |
spice_it_up = pick_a_response + from + smileys[rand(smileys.count)] | |
puts "replying to #{wish['from']['name']} \n message: wish['message']\n response: #{spice_it_up}" | |
post_id = wish['id'] | |
resp = RestClient.post(fb_base_uri + "/#{post_id}/comments", {"message" => spice_it_up, "access_token" => at}) | |
rescue Exception => xcp | |
puts post_id | |
end | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment