Build Instagram bot with node.js

Build Instagram bot with node.js

ยท

3 min read

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.

Setup project

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.

Login to instagram

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

Upload photos to your Instagram account

const uploadPhoto = async (photoUrl) => {
    const { media } = await client.uploadPhoto({ photo: photoUrl });
    console.log(`Pic uploaded!!`);
}

uploadPhoto('https://example.com/cat.png')

Change your display picture

const changeMyDp = async (photoPath) => {
    await client.changeProfilePhoto({ photo: photoPath });
    console.log('DP changed!!');
}

Update Instagram profile

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.

Instagram BOT

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.

Import node-cron

const cron = require('node-cron');

Create post array that you want to schedule

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',
]

Define current variable to keep track of current post.

let current = 0;

Define a function that uploads the photo to Instagram

const uploadPhoto = async (photoUrl, caption) => {
    const { media } = await client.uploadPhoto({ photo: photoUrl, caption });
    current++;
    console.log(`Pic uploaded!!`);
}

Finally, let's schedule them with node-cron

// 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 ๐Ÿ™‚

Did you find this article valuable?

Support Piyush Garg by becoming a sponsor. Any amount is appreciated!

ย