I have a blog (The one you’re reading right now. Welcome!) that uses Hexo to turn Markdown files into a static website. As a static site generator, Hexo also produces an RSS feed that contains basic information about each article (e.g. Title, Date, Author). In this solution I read the RSS feed and randomly select an article. Then I send the tweet. The solution below can be run on an “ad hoc” basis from your local machine OR be automated. I’m currently using a GitHub Action to automate the tweet on a cron schedule.
There are two main components to the solution, the solution itself AND the Twitter Developer credentials that grant access to the Twitter API. I’ll start with the Twitter credentials.
From the Twitter developer console we need to create an app, and from the app we can create a set of tokens and keys. We need to capture those tokens and keys and save them somewhere safe. These are effectively usernames and passwords so they should be treated as such. I prefer to store and pass these values as environment variables, using a .env file for NodeJs. **NEVER EVER commit these values or this file into any type of source control (i.e. GIT).**
NOTE: You may need to regenerate credentials when switching environments or when things just stop working.
API_KEY= < API KEY >
The tweeting solution has few components, the solution below will simply, read the RSS feed, randomly select an article, craft a tweet, send a tweet. Easy peazy.
const dotenv = require("dotenv");
Hopefully this solution has some components that might be helpful for other projects, from parsing an RSS feed, to creating a basic Tweet.