Tweet daily using Github Actions

Banner image for Tweet daily using Github Actions
Photo by Charles Deluvio on Unsplash

Github Actions are really powerful. I like to see them as personal assistants. You tell your assistants to do some work for you. Similarly, you tell Github actions to do some job for you.

I've recorded a video while building this. If you'd like to watch me build, here is the video 👇

https://www.youtube.com/watch?v=XI_B99Yw4kY

The main usecase for these actions, I feel, is when we use it to publish your library to npm or deploying your app on S3 or Github Pages whenever you push new changes. CI/CD - like the techies call it.

These actions can be triggered in different scenarios. In the above usecase, it is triggered on push. Similarly, you can also do it on a pull-request where we run a few checks or welcome new contributors to the project.

Another way to trigger an action is on a schedule, which is what I used to make the action Tweet everyday. 🗓We can give CRON expressions to schedule the triggers for the action.

Okay, where do we tell all these things to the Action? There's got to be some config, right?

Yeah. We have a yaml file for every action and we specify all the configuration for it there. All the yamls for your actions lie in .github/workflows path in your GitHub repo.

This is the action config I wrote

(Trimmed version. Full code here)

yml
1name: Tweet
2
3on:
4 schedule:
5 - cron: '0 10,16 * * *'
6

Now we know how to schedule things. Next step?

Tweet

We know there should be an API to post tweets. But for that, you need a few API credentials from Twitter. This was a big time taking process for me as the application for a dev account on Twitter had to go through a few reviews from Twitter employees.

Once you get the credentials, tweeting is as easy as the code below.

Full Code

js
1const Twitter = require('twitter');
2
3const client = new Twitter({
4 consumer_key: process.env.TWITTER_API_KEY,
5 consumer_secret: process.env.TWITTER_API_SECRET,
6 access_token_key: process.env.TWITTER_ACCESS_TOKEN_KEY,
7 access_token_secret: process.env.TWITTER_ACCESS_TOKEN_SECRET,
8});
9
10client.post('statuses/update', { status: tweet }, function (error, tweet, response) {
11 if (!error) {
12 console.log(tweet);
13 }
14});
15

I have this in a file scripts/getQuote.js and I run using yarn tweet. We add a script in package.json to be able to do this.

json
1"scripts": {
2 "tweet": "node scripts/getQuote.js",
3}
4

Last step

What's remaining now is that we tell our Action to run yarn tweet and that's it.

yml
1jobs:
2 build:
3 runs-on: ubuntu-latest
4
5 steps:
6 - name: Tweet it yo
7 run: |
8 yarn install
9 yarn tweet
10 env:
11 CI: true
12 TWITTER_API_KEY: ${{ secrets.TWITTER_API_KEY }}
13 TWITTER_API_SECRET: ${{ secrets.TWITTER_API_SECRET }}
14 TWITTER_ACCESS_TOKEN_KEY: ${{ secrets.TWITTER_ACCESS_TOKEN_KEY }}
15 TWITTER_ACCESS_TOKEN_SECRET: ${{ secrets.TWITTER_ACCESS_TOKEN_SECRET }}
16

If you observe, we pass the Twitter API credentials as environment variables. This can be done in the Repo settings in GitHub. You can add a few secrets and they get passed to the Actions in that repo.

Screenshot of Github Repo settings

That takes us to the end of the article. Thanks for reading till end and if you think learnt something from this, please tweet it so your friends learn too.

If you'd like to check out the tweets in action, its here -https://twitter.com/learningcurvpod

https://twitter.com/learningcurvpod/status/1259425283003822080

Take care.

Aravind Balla

By Aravind Balla, a Javascript Developer building things to solve problems faced by him & his friends. You should hit him up on Twitter!

Get letters from me 🙌

Get a behind-the-scenes look on the stuff I build, articles I write and podcast episodes which make you a more effective builder.

Read the archive 📬

One email every Tuesday. No more. Maybe less.