Ziro2Mach dream it... build it!


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: {% embed https://www.youtube.com/watch?v=Wr-6jx4DILA %}

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

{% embed https://youtu.be/Q9vDaU8piKc %}

  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;

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;
}

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");
}

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


untill next time !ļø āœŒ

or you could spot me in the wild šŸ¤­ i mean instagram, twitter, linkedin and maybe even youtube where i excalidraw those diagrams