[p-i wk 2] Learning Puppeteer & building social media bots

This has been a long (and winding) week. Coming from a mostly nonexistent coding background, getting into Node.js and Puppeteer in just a couple of days was both draining and rewarding. I’d like to appreciate my cohort Kevin for helping me get started with Terminal and VS Code, this Coding Train video series introducing GitHub, as well as my other cohort Philip offering support and figuring out Puppeteer with me.

My initial attempt was to figure out how to manipulate Instagram, automating a workflow that logs on and likes ALL the posts. It took me some days and an office hour to figure out the process of: going to instagram, fully automated log-in, finding and clicking the like button for the most recent post on the feed.

[demo video to come]

I found out that puppeteer is simply mimicking human behaviors, except for the fact that code usually runs faster than your average home internet. I just needed to let it wait for the button / box / div to load before I ask it to click on said button / box / div.

Here’s my code for sending fb message to a friend that sometimes works:

import puppeteer from "puppeteer";

const username = "myusername";
const password = "mypassword";

const browser = await puppeteer.launch({
  headless: false,
  slowMo: 10,
});

const context = await browser.createIncognitoBrowserContext();
const page = await context.newPage(); //context??

await page.goto("https://www.facebook.com");

await page.setViewport({
  width: 854,
  height: 480,
});

console.log("openfacebook");

await page.waitForSelector("#email");

await page.type("#email", username);
await page.type("#pass", password);
await page.keyboard.press("Enter");

// wait for a chat to be opened.
await page.waitForSelector("[aria-label='New Message']");
await page.click("[aria-label='New Message']");

await page.waitFor(5000);
await page.keyboard.type("Kevn", { delay: 10 });
await page.waitFor(1000);
await page.keyboard.press("ArrowDown", { delay: 10 });
await page.keyboard.press("Enter", { delay: 10 });

// send message

await page.waitForSelector("div.orhb3f3m._1mf _1mj");

await page.keyboard.type("._1mf _1mj", "I'm Alive!");
await page.keyboard.press("Enter");

Leave a Reply

Your email address will not be published. Required fields are marked *