Instagram Bot using Python Node JS and RabbitMQ

Devanshani Rasanjika
Nerd For Tech
Published in
5 min readMar 9, 2022

I’m working on a app which needs to implement Instagram actions which is able to like/unlike posts, follow/unfollow users and add comments for posts. Main purpose of my implementation is that user should be able to perform actions using a REST API. Since Instagram API’s deprecated for above actions we can not use their end points in my implementation directly. So I have designed an Instagram bot to perform those actions in server side using REST API. Here I’m going to implement my solution using Node JS puppeteer as the automation tool, RabbitMQ as the message broker to communicate between Python & Node JS and for the REST API implementation I have used Python Flask.

Puppeteer is a node library which provides high level API to control chrome or chromium and most of the things we can do manually in the browser can be done using puppeteer.

RabbitMQ is a widely deployed open source message broker or queue manager which gives our application a common platform to send and receive messages.

Below is the diagram of my implementation .

So Let’s get to do it.

Pre-requisites:

  • Basic knowledge of Node JS
  • Basic knowledge of Python

Set up Python message broker :

I have used python pika library to implement the message queue. As AMQP is a two-way RPC protocol where the client can send requests to the server and the server can send requests back to the client, Pika implements or extends IO loops in each of its asynchronous connection adapters.

So I have implemented message publisher from python and instagram_action_publish() is calling inside a REST API to publish messages to the queue. So here web application calls to the REST API written from python Flask and it will publish messages to the queue.

Set up Node JS Project :

I have used npm-init to initialize the Node service and created app.js and executed node app.js command to test the application. Then installed puppeteer using npm i puppeteer npm i puppeteer-extra npm i puppeteer-extra-plugin-stealth for the automation script and installed RabbitMQ using npm i amqp-connection-manager . This is a wrapper around amqplib which provides automatic reconnects.

And after that I have decided to have following folder structure.

  • app.js
  • messageBroker/publishers/publisher.js
  • messageBroker/subscribers/subscriber.js
  • bot/instagramFunctions.js
  • bot/config/instagramConfig.js
  • nativeActions/instagramActions.js

Below is the app.js code implementation which integrated log4js to configure logs and here subscribe to the message queue.

Then as the next step implemented message queue subscriber to subscribe the messages published from python service and call each Instagram action separately. And here I have configured Rabbitmq parameters and subscribed queue id in environment variables.

Then I have edited my instagramConfig.js file by adding the selectors and other configurations for the Instagram UI elements and network calls as below.

Then I have edited my instagramFunctions.js file inside the bot folder by adding the automation script for the actions separately.

Step 01 :

As the first step added initPuppeteer() method which import puppeteer library ,starts the browser and creates a new page as well as sets the browser width and height.

Step 02 :

In this step we’ll add a visitInstagram() which visits Instagram website and wait until network idle and wait until it appears the login UI and perform login action as below.

I have put random timeouts to wait for actions because if the actions are too fast our bot will be easily detected by Instagram which may result in our account being blocked or blacklisted.

Here I have select the UI elements by evaluating the HTML page because selectors are changing frequently in Instagram.

Step 03 :

In this step we’ll add a likepost() which visits Instagram post using post url and wait until network idle and wait until it visible the post and perform like action and undo like action as below.

In this scenario I have used aria-label in HTML content to find the like span > svg[aria-label = “Like”] and unlike span > svg[aria-label = "Unlike"]buttons.

Here I need to verify that Instagram network call got success and returned a valid Json response by intercepting the like action back end API call as below using puppeteer. So there we can intercept and get the API URL , Json response and response status.

As same I implemented for the like action I have done for follow/unfollow users and comment on posts as below. Here I’m going to add the full code implementation for instagramFunctions.js

Here I have added the full code implementation for instagramActions.js

Then publisher use to publish response back to python service like below.

And in the python service it will going to consume the response messages like below and send the response back to the web application.

There are limitations in this solutions as

Instagram change UI element selectors frequently because of that I have evaluate the page to find out the visible text content and perform click actions. But there is a risk of changing those end user visible text contents also. So we need to maintain our solution .

Puppeteer module use chromium browser to perform the bot as an automation script. Assume that user is going to login through Instagram credentials and going to perform multiple actions. But user can not perform those concurrently it going to crash the chromium browser. So user needs to wait until it finish previous action.

If single user needs to perform concurrently we need to handle many back end servers to run the script. So it will cost more.

There are security concerns as if the user tried many actions within a short time Instagram black listed the user.

With Puppeteer, Current response time for a single action is around 4–6 seconds. Assume10 users assigned to a particular instance are parallelly posting actions the response time will increase to an average of 20 seconds. This is due to the high memory usage from the chromium web browser instances that are created to run the actions.

--

--