Twitter tweet bot using GitHub Actions

Twitter tweet bot using GitHub Actions

Update Twitter status automatically using GitHub Actions

Asrin Dayananda
·Sep 19, 2021·

2 min read

Subscribe to my newsletter and never miss my upcoming articles

Listen to this article

Log into your twitter account

Register as twitter dev

Once approved create a new app

  • developer.twitter.com/en/portal/apps/new
  • Name your twitter bot
  • You should have keys now

  • Create new repo in github

  • Save your twitter keys to your GitHub repository secrets
  • Click on settings > secrets in your repository
  • Add the keys from your twitter app you created
  • Name them as close as twitter state so you can recall them later easily
  • eg.
    TWITTER_API_KEY
    API_KEY_SECRET
    TIH_BEARER_TOKEN
    
  • Also create more keys by clicking new access token and secret and save all these values

  • Create new file in your GitHub repo like this path

    twitterBot/.github/workflows/twitterBot.yml
    

Add below code to file

# This is a basic workflow to help you get started with Actions

name: TwitterBot

# Controls when the action will run. 
on:
  # Triggers the workflow on push or pull request events but only for the main branch
  push:
    branches: [ main ]
  schedule:
    - cron: '0 16 * * *' 
  # Allows you to run this workflow manually from the Actions tab
  workflow_dispatch:

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
  # This workflow contains a single job called "build"
  build:
    # The type of runner that the job will run on
    runs-on: ubuntu-latest
    steps:
    #Tweet to twitter https://github.com/marketplace/actions/tweet-to-twitter
      - name: TweetToTwitter
        uses: InfraWay/tweet-action@v1.0.1
        with:
          status: Test Tweet
          api_key: ${{ secrets.TWITTER_API_KEY }}
          api_key_secret: ${{ secrets.API_KEY_SECRET }}
          access_token: ${{ secrets.BEARER_TOKEN }}
          access_token_secret: ${{ secrets.BEARER_TOKEN }}
  • Commit your changes

  • Click actions and see your GitHub action build

  • If you get this error image.png

  • Your keys arnt set correctly
  • Re generate your keys in twitter and reset them in your github repository secrets

image.png

  • Re run job or add empty space commit to your yml file and push commit. The action is set to build on push at the moment.

  • If you get this error

    Error: {"request":"\/1.1\/statuses\/update.json","error":"Read-only application cannot POST."}
    
  • Set your twitter app to be able to write posts
  • Twitter App Settings > App permissions > Edit

image.png

  • Set to read and write

image.png

  • You will have to regenerate all your keys and save them to your GitHub Secrets

  • If you see success your github action will succeed

image.png

  • And you will have a successful tweet !😵

image.png

Hope this works for you. Let me know if you get stuck.

Happy Coding, Asrin

If this helped you consider buying me a coffee :)

References

Did you find this article valuable?

Support Asrin Dayananda by becoming a sponsor. Any amount is appreciated!

See recent sponsors Learn more about Hashnode Sponsors
 
Share this