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:
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.
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.
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.
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
TWILIO_PHONE_NUMBER to the function.
This was a relatively simple project, and it has been fun getting the text every morning.