Automate Twitter via github Actions.

because why not!

Featured image

We are used to the way we work, but we are also used to the way we work.Hahahah, Sounds confusing, right? I know, but it is not.Lemme explain.

We are used to tweet via either twitter webApps or Native apps.But hold on, in the era of Devops where every things reliable on CI/CD, then why not automate our twitter too.

We gonna call this Twitter-Together

basically twitter-together is a Github Action that utilizes text files to publish tweets from a GitHub repository. Rather than tweeting directly, GitHub’s pull request review process encourages more collaboration, Twitter activity and editorial contributions by enabling everyone to submit tweet drafts to a project.

Setups

  1. Create a twitter app with your shared twitter account and store the credentials as TWITTER_API_KEY, TWITTER_API_SECRET_KEY, TWITTER_ACCESS_TOKEN and TWITTER_ACCESS_TOKEN_SECRET in your repository’s secrets settings.

  2. Create a github workflow .yml file with the following contents:

on: [push, pull_request]
name: Twitter, together!
jobs:
  preview:
    name: Preview
    runs~|-|on: ubuntu~|-|latest
    if: github.event_name ~|=|~|=| 'pull_request'
    steps:
      ~|-| uses: gr2m/twitter~|-|together@v1.x
        env:
          GITHUB_TOKEN: $
  tweet:
    name: Tweet
    runs~|-|on: ubuntu~|-|latest
    if: github.event_name ~|=|~|=| 'push' && github.ref ~|=|~|=| 'refs/heads/main'
    steps:
      ~|-| name: checkout main
        uses: actions/checkout@v2
      ~|-| name: Tweet
        uses: gr2m/twitter~|-|together@v1.x
        env:
          GITHUB_TOKEN: $
          TWITTER_ACCESS_TOKEN: $
          TWITTER_ACCESS_TOKEN_SECRET: $
          TWITTER_API_KEY: $
          TWITTER_API_SECRET_KEY: $

  1. After creating or updating .github/workflows/twitter-together.yml in your repository’s default branch, a pull request will be created with further instructions.

Now see the magic!

No wait, what about logic ?

chillax!

Here comes it:-

How It Works

Twitter-together is using two workflows

  1. push event to publish new tweets
  2. pull_request event to validate and preview new tweets

The Push Events

When triggered by the push event, the script looks for added *.tweet files in the tweets/ folder or subfolders. If there are any, a tweet for each added tweet file is published.

If there is no tweets/ subfolder, the script opens a pull request creating the folder with further instructions.

The Pull Request Events

For the pull_request event, the script handles only opened and synchronize actions. It looks for new *.tweet files in the tweets/ folder or subfolders. If there are any, the length of each tweet is validated. If one is too long, a failed check run with an explanation is created. If all tweets are valid, a check run with a preview of all tweets is created.

What else?

Whatelse

Nothing else.

Nothing