Overview

Let’s set up a script that sends one of Paul Graham’s top essays to our inbox every Monday, Wednesday, and Friday.

Here’s how we’ll tackle it:

  1. Write the script to fetch a random essay from Paul Graham’s most popular essays
  2. Set up an email to be sent with the essay every Monday/Wednesday/Friday
  3. Configure the schedule so that it “remembers” which essays have already been sent through

Fetching essay data

We’ll use the cheerio library to parse the essay page HTML. Here’s what that function might look like:

Here’s what the output of that might look like:

Triggering an email with the essay

First, let’s define a helper function to pick a random essay:

Now, all we have to do is trigger the email.

Here we handle that in a NextJS API endpoint:

Keeping track of alert history

When you create a job on a schedule, the scheduler will pass in some metadata into the body of each request. Included in the metadata is the schedule’s state, which can be accessed at req.body.$state and set or updated in the response.

Let’s modify the API handler to take advantage of $state:

export default async function handler(req, res) {
  // ...

  const exclude = req.body.$state?.sent || [];
  const essay = req.body.essay || getRandomEssay(exclude);

  // ...

  const email = await resend.sendEmail({...});

  return res.json({ email, $set: { sent: [...exclude, essay] } });
}

We can clean this up a bit by taking advantage of dynamic values in our job schedule configuration. When we set the request body for our job, we can write it like this:

{
  // Pass in `req.body.$state?.sent` as `req.body.exclude`
  "exclude": "$state.sent"
}

If we do this, we can change the line above from this:

const exclude = req.body.$state?.sent || [];

To this:

const exclude = req.body.exclude || [];

Now, assuming you’ve deployed your API endpoint to https://yourdomain.com/api/pg, you can create your scheduled job by running the following script in your terminal with your BOOPER_API_KEY set, and the url modified to the appropriate domain: