DEV Community

Cover image for How I Built a Meme Bot for Instagram
Nukala Suraj
Nukala Suraj

Posted on

How I Built a Meme Bot for Instagram

build your own instagram bot from scratch (or) set one up in like 10s for free

a brief overview of this blog post:

Introduction

Imagine you have a cool meme page on instagram with about a 1M+ followers and the best part - you never really need to interfere with it...
for real

not kidding... but well there sure is a catch here cuz otherwise 1M+ followers would not longer be a feat anymore:
maji ka yo

any page on instagram/social media in general has got 2 aspects to it:

  1. content creation 🎨
  2. marketing πŸ“’

this bot system has got your back for content creation and you've gotta take care of the marketing and making sure the page grows...

here's an example of a successfully automated meme page w/ poor marketing: @taewonsu

and I've really gotta mention I absolutely suck at marketing and hence...
treasure
and yea obviously @taewonsu is my automated instagram meme page


So What Exactly Does The Bot Do

doesn't matter if you're a human or a bot(AI), creating memes people ❀️ is tough

but... there exists reddit, the front page of the internet
reddit
this is where memes first hit the internet and then they trickle down to instagram/facebook/twitter/discord

and most people shamelessly just steal memes from reddit (i.e. they do not credit the original creator of the meme)

that however that doesn't apply to a bot so... it'll will credit the original u/creator of the meme and then just post the meme to our page
big brain time


Creating Your Own Bot

Red Pill Blue Pill

This is your last chance, after this there's no turning back.

  1. you take the blue pill... I give you the easy way... setting up a meme bot from my source code.
  2. you take the red pill... I show you how the bot works and how you can build your bot from scratch.

Blue Pill

  1. clone this repository off github
  2. create an instance on heroku (or) whatever
  3. configure the environment variables for your bot
  4. GG

Red Pill

the bot does 3 thing:

  1. scrape for top memes of the day in the specified r/subreddit
  2. check if a meme has been previously posted
  3. post the unique memes to instagram

1. Scraping

working with reddit's API has been an absolute pleasure
mua
let's say the URL for top content in a subreddit say r/animemes is https://www.reddit.com/r/Animemes/top/

then the endpoint for the API would be https://www.reddit.com/r/Animemes/top.json

so fetching the top 3 memes of the day would be:

// fetching new memes from reddit
const res = await axios.get(`https://www.reddit.com/r/${subreddit}/top.json?limit=${top_x ? top_x : 3}`);
animemes = res.data.data.children;
Enter fullscreen mode Exit fullscreen mode

2. Unique

when we're posting a bunch of meme's per day it'll be pretty likely that we may re-post something

so the bot maintains an hashmap, where the key is the id of the meme in reddit's database and the value is the meme data

when ever it gets a new meme, it quickly checks if the id exists in the hashmap, if it doesn't the bot posts the meme and then adds the meme to the hashmap

// fetching past memes
const raw_pa = fs.readFileSync(posted_animemes_location);
const posted_animemes: animeme_in_json = JSON.parse(raw_pa.toString());

// cross-referencing a new meme
if (posted_animemes[animeme.data.id]) {
  console.log("seen this!");
  return null;
}
Enter fullscreen mode Exit fullscreen mode

3. Posting

while building the bot, I was pleasantly surprised at how less bug I was running into and I'm not gonna lie it did feal really good... and then instagram API happened
regret
interacting with instagram's API was an absolute pain in the ass... spent like 3 days and I still couldn't properly set up the access tokens required

luckily I found a god send instagram-private-api

instagram-private-api is basically a typescript 🀩 wrapper over instagram's private API [the endpoints you'd find if you inspected the network panel of your browser's dev tools]
network
instagram-private-api greatly sped up and made the developer experience just way more greater and I was able to implement the auto post to insta reletively quickly

// logging into instagram
try {
  const auth = await ig.account.login(ig_uname, ig_pass);
  if (!auth.pk) return "LOGIN FAILED";
} catch (error) {
  console.log(error);
  return "LOGIN BLOCKED";
}

// uploading an meme
try {
  const publishResult = await ig.publish.photo({
    file: imageBuffer,
    caption: `credits: u/${animeme_to_post.author}\n${animeme_to_post.title} Β―\\_(ツ)_/Β―\n.\n.\n.\n.\n.\n#${hashtags}`,
  });
} catch (error) {
  console.log("work on fixing that image size thing you sucker");
}
Enter fullscreen mode Exit fullscreen mode

the best thing here is since we're basically using instagram-private-api, instagram can't really recognize us a bot
mission accomplished


and that's how I built an automated instagram meme page from scratch

If You ❀️ This Blog Post
Be Sure To Drop a DM on Twitter

also here're all the links throughout the blog post
og automated bot @taewonsu: https://www.instagram.com/taewonsu/
source code on github: https://github.com/LucidMach/taewonsu
my twitter: https://twitter.com/lucidmach

✌️,
LucidMach

Top comments (12)

Collapse
 
vulcanwm profile image
Medea

This is interesting!

Collapse
 
lucidmach profile image
Nukala Suraj

🀩 glad you enjoy it

Collapse
 
lucidmach profile image
Nukala Suraj

❀️ glad you like it

Collapse
 
dhravya profile image
Dhravya

I love your style! And i learnt a lot with this blog. Keep it up
🀩

Collapse
 
lucidmach profile image
Nukala Suraj

this really means a lot to me...
glad you ❀️ it

Collapse
 
kithminiii profile image
Bhagya

An interesting article. Keep it up.

Collapse
 
andrewbaisden profile image
Andrew Baisden

What an interesting and cool idea thanks for the article.

Collapse
 
lucidmach profile image
Nukala Suraj

Β―_(ツ)_/Β―
if it can be automated... it will be automated lmAo

Collapse
 
leslieheit profile image
LeslieHeit • Edited

But how can I grow the meme bot in instagram ? Brains On Rent

Collapse
 
lucidmach profile image
Nukala Suraj

Umm if i knew my bot would have been the most popular page on insta πŸ‘€πŸ˜‚

No clu... I think it involves collaboration with other pages..., Shout-outs, etc

not sure... If you do grow the bot... Lemme know too 😁

Collapse
 
boly38 profile image
Brice

Hi
Did you stop this bot one year ago ?
I ask this question because I'm trying the nodejs lib and encounter login Issues, like some users..
cf. github.com/dilame/instagram-privat...

I'm curious to know if this library is always working well today.

Btw, Thanks for this post πŸ‘πŸ»

Collapse
 
lucidmach profile image
Nukala Suraj

Hi
sorry for the late reply 😬

but yea, i think instagram now has implemented ways to stop the misuse of their private API (essentially what the nodejs lib is doing)

i think my bot was shadow banned about an year ago πŸ˜”