So, let’s say it’s your birthday. You’re super-excited and spend the day having your cake and eating it too. The next morning you log in to Facebook, only to find your timeline completely flooded with birthday posts from all your well-wishers. You want to reply to all of them, but it would take you all day! How do you automate this whole process of liking and commenting on each birthday post without toiling away on Facebook for hours? We turn to our trusty little friend: Python!
Here are the steps to automatically reply to Facebook birthday wishes with Python:
Step 1: Create a Facebook App ID
1. Go to https://developers.facebook.com/
2. Click on “Add a New App” inside “My Apps”
3. From the window that pops up, select Website.
4. Type any name for your app. I just put in “Ashish”.
5. Click on “Create New Facebook App ID”.
6. On the new pop up box, choose any category. I selected “Education”.
7. Now click “Create App ID”.
8. Scroll to the bottom of the new page and type in any URL.
9. Hit “Next”.
Step 2: Get an Access Token
1. Scroll all the way to the top of the page and under “Tools and Support” click “Graph API Explorer”. Alternatively, you can just go to https://developers.facebook.com/tools/explorer/
2. Under “Application”, select the application you created in step 1.
3. On the dropdown menu to the right select “Get Access Token”.
4. Click on “Extended Permissions” and tick “publish_actions” and “read_stream”.
5. Now click Okay on these screens.
6. Your unique access token has appeared in the box.
WARNING: Do not give this token to anybody. It allows people to post to Facebook on your behalf.
Step 3: Set Up the Python Code
1. Download this code and save it as birthday.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
""" A simple script to like and comment on all posts on a user's timeline for a given day. Useful for replying to birthday posts. By: Ashish Acharya """ import facebook import requests import datetime # Configuration ################################ # Insert your access token here. You can get a temporary one # here: https://developers.facebook.com/tools/explorer/ access_token = 'paste your access token within these quotation marks' # Fetch contents of your own wall by setting user to me user = 'me' # Set your birthday here in the same format: birthday = '2015-06-21' ################################ def parse_date(post): """ Parse date from Facebook's format to Python's datetime format. """ post_date = datetime.datetime.strptime( post['created_time'], '%Y-%m-%dT%H:%M:%S+0000') return post_date.date() def like_and_comment(post): """ This function likes and comments on posts sent to it. """ message = "Thank You! :)" graph.put_like(post['id']) graph.put_comment(post['id'], message) # convert birthday to datetime bday = datetime.datetime.strptime(birthday, '%Y-%m-%d') # Fetch graph, own profile and posts. graph = facebook.GraphAPI(access_token) profile = graph.get_object(user) posts = graph.get_connections(profile['id'], 'feed') # Get all posts made on your birthday from your wall. # This code assumes they're all 'happy birthday' type posts. birthday_posts = [ post for post in posts['data'] if parse_date(post) == bday.date()] # Now like and comment on all birthday posts for idx, post in enumerate(birthday_posts): like_and_comment(post) print('Post %d successfully processed.' % idx) |
2. In the configuration section, add in your access code and birthday in the same format as shown.
3. Install Facebook-SDK and ‘requests’. On linux it’s as simple as pip install facebook-sdk requests
(If you don’t have pip, you need to install python-setuptools and install it through easy_install. Just Google “install python pip”)
Step 4: Run the Code
1. Open up a terminal, navigate to the folder where you have birthday.py and run it with: python2 birthday.py
2. Relax and watch Python do some magic!
If you run into problems in any step, feel free to comment below and I’ll try my best to help you out.
What if we need to post the username in the reply too ?
You’d need to find the correct edge in Facebook’s Graph API and extract information about a post’s creator.
You can find more information here: https://developers.facebook.com/docs/graph-api
If I feel up to it, I’ll update the script with this feature.
i wanted to post the user name in reply ? how it can be done
hi,
i guess this no longer works as the permissions has been removed 🙁