How I made an instagram bot that posts a quote every day

João Ramiro
6 min readFeb 10, 2021

How to generate an image with an inspirational quote and post it to instagram using python.

Introduction

The other day I got curious on how to make posts automatically bot. It turns out that Zuckerberg doesn’t allow this through the Instagram API, probably to counter the existence of like and follow bots etc. So I ended up using an unofficial API to make am inspiration bot

There are only a few sites (presumably facebook partners) that allow you to post and schedule to Instagram. Some of them are Facebook Creator Studio (Facebook’s own tool), Plann, Later but there are many more.

Only through these platforms can you post and schedule, aside from through Instagram itself, at least officially.

Unofficially however, turns out you can do it too, and plenty people have actually developed unofficial APIs , that essentially mimick a user kinda like a bot. The one we will be using today for Instagram automation will be instaauto a great tool in active development made for python that lets you make posts, stories, comments the whole shebang.

Getting the Quotes

Among the many origins for a quotes I chose to use an preexisting site, https://quotes.rest which will give you a different quote every day via https://quotes.rest/qod :

As you can see it not only bring the quote itself, as well as the author and even somes tags that we will be using as hashtags for our Instagram post!

To get the quote we just use the requests library on python like so:

After getting the result back we extract the part of the payload that matters.

Generating the Image

This is when things start to get juicy. This is a basic mockup of want we want to achieve:

Here are a few requirements:

  1. Quote must be vertically and horizontally centered.
  2. There must be a margin in each side of the quote. Line breaks can be added to meet this requirement.
  3. Every quote line must have around the same width.
  4. The Author Name must have a fixed distance to the bottom right corner.

Lets start simple by creating a black square and adding some text on the center:

With this code we are creating a black square that is 1080x1080 (the best dimensions for square images on instagram). Then we are setting font and size and after that we are getting the size of the quote in pixels and using that to center it on the image. We set the color of the quote to white (255,255,255) and align to center (rather then left aligned which is the default).

In order to perfectly align the quote. We have to take into consideration that the origin / anchor point of text is the top left corner of the quote.

Knowing this, subtracting doing W/2-w/2, will give us the exact x position of this anchor in order for the quote to be align horizontally. The same thinking is applied vertically.

This is a good start! but if a quote is to long, it will infinitely expand in each side. This ain’t it yet chief.

We must come up with a way that can insert line breaks in the quote in order for it to fit, keeping in mind the each line should have more less the same width. I ended up creating a function for this that does the this very thing:

First we determine how many times bigger is the quote width comparing to the image width this number will be the number of lines to divide the quote in.

Then to add the line breaks to the string, we have to find exact locations where they should be added. If the quote has 150 character and we want to divide in 3, line breaks should be added at 50 and at 100 right? Almost, we can’t just add a paragraph willy nilly as adding a break in the middle of a word would not be very aesthetic. As so we look for the space that is closest to 50, and then to the space that is closes to 100 and add the line breaks there.

And voila!

Having done all this adding the author becomes trivial. we just subtract from the total image size W, the author text size w and the offset from the border (100px), the same is done vertically.

With this we get our final product:

All there is left to do is post it onto Instagram.

Posting to Instagram

Using the library instauto, this process is made very straightforward. We just pass in the account username and password and pass in the image and the caption for our post. It is as simple as:

Posting Once a Day — Deploying w/ Heroku + Scheduler

The quote is good and it is being posted to Instagram. Now we just need to make the process automatic. I opted to deploy it to heroku as it is has a free tier where you can host your code.

Before adding the code to heroku I just had to add 2 files to the project folder (see final repo with all this on the end of this story). The file requirements.txt contains all the packages that are being used and the runtime.txt contains the version of python to deploy the code in:

Then by following the instructions in heroku’s official documentation or in this tutorial 🎬tutorial we get the code deployed online. To make it run periodically all that is left to do is add the Heroku Extensions Add-On by going on the Resources tab on Heroku and clicking find more Add-Ons.

After that add it you should see the Heroku Scheduler on the bottom of the Resources tab. Click it and add a Job with the parameters that you wish.

I chose to make my python code once every day at 12:30PM

And that’s it @inspiration_bot you now post a new inspiring quote every day!

Conclusion

As always the code is available on github on the card above. I hope this was helpful and thanks for reading.

— João Ramiro, February 2021

--

--

João Ramiro

Researcher, Engineer, Entrepreneur looking to share some of his insights