Build Instagram bot with node.js

Search for a command to run...

My first test bot was built with an open-source module InstaPy. Now, I need to test the second bot created with node.js under the guidance of a dev team (mentor - Haider Imtiaz). Instagram bot library - instauto tool to do a programming task and write my thesis
Twitter is one of the most popular social media platforms in the world, with over 330 million active users as of 2021. If you are interested in building a Twitter-like application, this tutorial will guide you through the process of building a FullSt...

Hey everyone, In this article, we will build our own Tic Tac Toe game using pure Javascript. https://youtu.be/lKe57l8sttw Code <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta http-equiv="X-UA-Compatible" content="IE...

Hi everyone, In this blog, we will learn how we can create node.js functions locally and deploy them to AWS Lambda using serverless. So, let's get started. Step 1. Create an AWS Account ☁️ In order to deploy functions to AWS Lambda, you should have a...

Hey there, In this article, we are going to learn how you can upload any file to firebase storage. Uploading files to firebase storage is really easy. I would I recommend you to code along with me for better understanding. So, let's get started. Step...

Hey there, Welcome back. In this article, I would be showing you how I made a collaborative IDE with a video call. The main application of this application is that you can create a room and people can join the room. You have the ability to write mark...

Hey reader, In this article, we would be building an Instagram bot that is capable of doing various things such as automatic uploading, commenting, accepting follow requests, and much more.
Note: The best part of this article is at the last, so, please read this article till the end. 🙂
Building an Instagram bot with node.js is really easy. All you need is an Instagram account and node.js installed on your machine.
So, let's get started.
Firstly, create an empty project and run npm init -y command to create package.json file. After that install instagram-web-api package to get started.
npm install instagram-web-api
Great, now open your fav editor and code along with me.
In this step, we would create an Instagram client and authenticate via username and password.
const Instagram = require('instagram-web-api');
let username = '<YOUR_USERNAME>';
let password = '<YOUR_PASSWORD>';
const client = new Instagram({ username, password });
(async() => {
await client.login();
const profile = await client.getProfile();
console.log('profile');
})()
Now, Let's play around with the API
const uploadPhoto = async (photoUrl) => {
const { media } = await client.uploadPhoto({ photo: photoUrl });
console.log(`Pic uploaded!!`);
}
uploadPhoto('https://example.com/cat.png')
const changeMyDp = async (photoPath) => {
await client.changeProfilePhoto({ photo: photoPath });
console.log('DP changed!!');
}
const updateProfile = async (data) => {
await client.updateProfile(data);
console.log('Profile updated')
}
updateProfile({ name: 'Piyush', username: 'piyushgargdev', bio: 'piyushgarg.hashnode.dev' });
Okay, I hope that now you are pretty much familiar with API, now let's build a bot that runs at a specific time and likes, comments on the posts based on hashtags.
To get started, go and install node-cron. This enables us to schedule tasks on a specific time.
npm install node-cron.
Now, I am going to firstly build a function that fetches posts by hashtags, then I would loop over posts and like them.
const likePostsByHashTag = async (hashtag) => {
const LIMIT = 10;
const feed = await client.getMediaFeedByHashtag({ hashtag });
const posts = feed.edge_hashtag_to_media.edges;
for (let i = 0; i < LIMIT; i++) {
const mediaId = posts[i].node.id
setInterval(() => {
await client.like({ mediaId: mediaId}); // let each like has a gap of 4 seconds
}, 4000);
}
}
Cool, now let's work with node-cron and schedule our posts in such a way that one post is uploaded every day at 12:00am.
const cron = require('node-cron');
const posts = [
'https://expample.com/pic1.png',
'https://expample.com/pic2.png',
'https://expample.com/pic3.png',
'https://expample.com/pic4.png',
'https://expample.com/pic5.png',
]
let current = 0;
const uploadPhoto = async (photoUrl, caption) => {
const { media } = await client.uploadPhoto({ photo: photoUrl, caption });
current++;
console.log(`Pic uploaded!!`);
}
// 0 0 0 * * ? - means everyday at 12:00am
// Read more about these expression at https://www.npmjs.com/package/node-cron
cron.schedule('0 0 0 * * ?', async () => {
await uploadPhoto(posts[current], 'Great! Its posted automatically!');
});
Done 🙂