A few months ago, a Belgian telecom company created this awesome post on Facebook.
You had to tag a friend in the comments and the page would automatically respond with an image showing the profile picture of that friend, plus a note of ‘Sinterklaas’ (a Santa Claus-like figure roaming Belgium and the Netherlands in early December) judging your behavior of the past year.
My meme-loving sister tagged me and apparently, I’ve been bad in the past year?
Lots of people thought there was someone making these images by hand, but I knew better. At the time of writing, the post has over 8200 comments. No way those 4100 pictures were made by hand.
After looking around I found out that Inti De Ceukelaire created such a lead generation thing before and has written about it here. I’m a sucker for these things and wanted to find out exactly how difficult it was to make it myself.
About 200 lines of Python later, I pulled it off. Behold the Facebook Autoreplier bot in action.
I’ve released the code under the MIT license on Github allowing you to play around with it. You’ll need some knowledge of Python to create different images and change the copy, but overall the process is pretty straightforward. If you’re ever stuck, we at The Growth Revolution with our roots in lead generation services can help you set it up.
So let’s get started generating leads and create your own Facebook autoreplier bot!
What you need
You’ll need to install Python3 and all the requirements that are in the requirements.txt file inside the Github repo.
Facebook-wise you’ll need:
- A Facebook page, duh.
- A Facebook app in the Facebook developer portal.
- A page access token (normally this expires after a few hours, I think? We created an access token that lasts 60 days).
Getting the long-lived Facebook access token
If you don’t have a page yet, start by making a Facebook page here.
Then head over to the Facebook developer portal, click on my apps in the top right and add a new app. You can give it any name, other people won’t see it anyway. Same goes for the contact email.
We need to do two things here: copy the API credentials and make sure our app can post comments on our Facebook page.
To get the API credentials, click on the top-left dashboard. There you’ll see the app ID and the app secret. Copy-paste these into a working environment, a simple text editor will do.
To allow our app to post comments, click on app review in the side menu. Make sure you keep your app in development! Start a submission, then select both the manage pages and publish pages permission.
Next, let’s get you an access token! To do this, go to the Facebook open graph explorer. Make sure you select the application you just created. In my case it’s called “test”.
Then click on get token and make sure you select get page access token. Open it up again, and select the page you want to comment on. In my case I took the page with the name “Quehue”. Then click on the bottom one again and see if “allow to post as page” appeared. If it did, click it, if it didn’t, it ‘s fine. To check if everything works, click submit and you should see the ID and name of the page appear.
Copy the access token and add it to the document where you just pasted your app ID and app secret. Now, this access token is a short-lived one. We are going to get a long-lived one, just because I’m too lazy to constantly refresh it.
Go to this URL, but change the app_ID, app_secret, and existing_access_token in your own ID, secret and token.
You’ll get some code back, including the long-lived access token.
That’s it, we’re now ready to dive into the code!
Monitor the comments
Clone or download the code from Github and open the autoresponder.py file. Here we’ll have to fill in which Facebook post it should monitor as well as the access token.
Head over to Facebook, create a new post with your page and copy both the page ID and the story/post ID into autoresponder.py. To get those ID’s, click on the timestamp of your post. If you just created it, it will be “just now”. In my case, I created it 2 hours ago. Then you will see the story_fbid parameter in the URL. This is the post ID. The id param behind it is the page ID.
Now you’ll be able to run autoresponder.py and it will monitor that particular Facebook post. Every time a new comment is posted (or if there’s an existing comment that is not yet in the comments.sqlite3 database), it will reply to that comment with a custom image.
Making the custom image
In the make_new_profile_pic method, a new image is created and returned. If you want to customize it (change the size of the profile picture, add some text, etc.) you will need some knowledge of Python3 and pillow/PIL to change the code.
If you just want to try it out, you can save your own “overlay picture” in the folder, change the background/foreground image path to yours and it should work. The image is resized to 480×480 pixels, but you can change that. But be aware, the bigger the picture, the slower it is.
In the background version, it uses the image fcdk_overlay.png as the background image and pastes the profile picture on top of this, located 100px from the top and 100px from the left. When you open fcdk_overlay.png you will see it has a white border of exactly 100px wide.
In the foreground version, the tgr_overlay.png picture is pasted on top of the profile picture. This is a great way to generate leads and make custom profile pictures for your Facebook fans!