A Twitter bot that posts a random quote from a text file during peak hours, using Python and GitHub Actions.
- Create a Twitter/X account for your bot.
- Go to Settings and privacy > Account information > Automation, and follow the instructions.
- Go to https://developer.twitter.com/ and fill out the form to access the Twitter API.
- In the Developer Portal, find your project app under Default project and click App settings.
- In the User authentication settings area, click on Set up. Set App permissions to Write and Read, Type of App to Web App and for App info insert your bot’s Twitter profile as the callback URL and website link.
- Copy your project's API Key, API Key Secret, Access Token, and Access Token Secret, and store them somewhere safe.
- Fork this repository, click on Settings, and on the sidebar go to Secrets and variables > Actions.
- Add four repository secrets and paste their respective values:
CONSUMER_KEY
: Your API KeyCONSUMER_SECRET
: Your API Key SecretACCESS_TOKEN
: Your Access TokenACCESS_TOKEN_SECRET
: Your Access Token Secret
- In your GitHub repository, create a
.github/workflows
directory. - Inside your workflows directory, create a file called
action.yml
with the following:
name: Schedule Tweet
on:
schedule:
- cron: '0 14,2 * * *' # Runs at 9:00 AM and 9:00 PM EST
workflow_dispatch:
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Check Out Repository
uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: 3.12
- name: Install Dependencies
run: pip install -r requirements.txt
- name: Run Python Script
env:
ACCESS_TOKEN: ${{ secrets.ACCESS_TOKEN }}
ACCESS_TOKEN_SECRET: ${{ secrets.ACCESS_TOKEN_SECRET }}
CONSUMER_KEY: ${{ secrets.CONSUMER_KEY }}
CONSUMER_SECRET: ${{ secrets.CONSUMER_SECRET }}
run: python main.py
Finally, create a file in your repository called tweets.txt
with a list of tweets you'd like to randomly schedule, separated by lines breaks. You can test your bot by going to Actions > Schedule Tweet > Run workflow.