Danny Guo | 郭亚东

Morning Joy

 ·  472 words  ·  ~3 minutes to read

This past April, DEV hosted a hackathon in conjunction with Twilio (here’s a referral link for a $10 credit). This seemed like a good chance to play around with the Twilio API, so I created a small project called Morning Joy. It lets you start each day on a good note by texting you an uplifting news story and a picture of something cute.

Following the news can be stressful or even depressing. This is one small way to remember that good things are happening too.

Here’s an example from the first day:

Morning Joy example

How I Built It

I signed up for Twilio and followed their quickstart guide for Node.js. I bought a phone number and tested sending a text to myself. This was painless. I probably spent more time picking the phone number than I did writing the code.

const twilio = require("twilio")(
    process.env.TWILIO_ACCOUNT_SID,
    process.env.TWILIO_AUTH_TOKEN
);

async function sendText(body, mediaUrl) {
    const text = await twilio.messages.create({
        body,
        mediaUrl: mediaUrl ? [mediaUrl] : [],
        from: process.env.TWILIO_PHONE_NUMBER,
        to: process.env.DESTINATION_PHONE_NUMBER
    });

    return text;
}

Reddit JSON

Next, I needed to find a content source that I can count on to be renewed on a daily basis and to be of high quality. I decided to use Reddit’s r/aww and r/UpliftingNews subreddits. They are two of the largest subreddits, so I should never get duplicate content. I also wanted to grab the first post when sorting by top for today, which means the community has heavily upvoted the post.

I looked into the best way to fetch posts from Reddit. I initially planned to use their API, but it turns out that Reddit has a convenient feature where you can simply append .json to the end of a subreddit URL to receive the data as JSON: https://www.reddit.com/r/aww.json.

This worked well, but I didn’t know how to sort the results by the top posts for today rather than hot (trending posts right now). I discovered that a query parameter does the trick: https://www.reddit.com/r/aww/top.json?t=day.

const got = require("got");

async function getAwwPost() {
    const response = await got(
        "https://www.reddit.com/r/aww/top.json?t=day"
    ).json();

    for (const post of response.data.children) {
        if (post.data.post_hint !== "image") {
            continue;
        }

        const {title, url} = post.data;
        return {title, url};
    }

    return null;
}

I wrote a script to grab the top post for r/UpliftingNews and the top image post for r/Aww and then text them to myself using Twilio.

Hosting

Lastly, I needed a way to schedule the script to run every morning. To keep it simple, I converted my script into an AWS Lambda function and set up a CloudWatch Events rule that triggers on a schedule to fire the function every day at 10 a.m. UTC. The schedule expression is: cron(0 10 * * ? *). I also added environment variables to pass DESTINATION_PHONE_NUMBER, TWILIO_ACCOUNT_SID, TWILIO_AUTH_TOKEN, and TWILIO_PHONE_NUMBER to the function.

This was a relatively simple project, and it has been fun getting the text every morning.


← Fixing MacBook Pro Thermal Performance Issues BEBEN X8 Review →

Follow me on Twitter or Mastodon or subscribe to my newsletter or RSS feed for future posts.

Found an error or typo? Feel free to open a pull request on GitHub.