If you find this useful, please consider supporting my work with a donation.
A simple CLI for sending tweets. This is intended for use in CI systems such as GitHub actions in order to enable to Twitter notifications of important events.
You must have Node.js to use this package.
To start, you must have a registered Twitter application.
Next, define four environment variables:
TWITTER_ACCESS_TOKEN_KEY
- your access tokenTWITTER_ACCESS_TOKEN_SECRET
- your access token secretTWITTER_CONSUMER_KEY
- your consumer API keyTWITTER_CONSUMER_SECRET
- your consumer API secret
The CLI will not work without these environment variables. All of these values come from your Twitter application.
Then, you can run the CLI and pass a message on the command line using npx
:
$ npx @humanwhocodes/tweet "Hello from the command line!"
If successful, the CLI will output the response from Twitter.
By default, Tweet uses v2 of the Twitter API. If you'd like to use v1.1 of the Twitter API instead, set the TWITTER_API_VERSION
environment variable to v1
.
If you'd like to test with dotenv
, define an additional environment variable TWEET_DOTENV=1
before executing the CLI. This will cause a local .env
file to be read before executing.
Be sure to set up GitHub secrets for each environment variable. Then, you can configure a job like this:
jobs:
tweet:
name: Tweet Something
runs-on: ubuntu-latest
steps:
- uses: actions/setup-node@v3
with:
node-version: 18
- run: 'npx @humanwhocodes/tweet "Your tweet text"'
env:
TWITTER_CONSUMER_KEY: ${{ secrets.TWITTER_CONSUMER_KEY }}
TWITTER_CONSUMER_SECRET: ${{ secrets.TWITTER_CONSUMER_SECRET }}
TWITTER_ACCESS_TOKEN_KEY: ${{ secrets.TWITTER_ACCESS_TOKEN_KEY }}
TWITTER_ACCESS_TOKEN_SECRET: ${{ secrets.TWITTER_ACCESS_TOKEN_SECRET }}
- Ensure you have Node.js 12+ installed
- Fork and clone this repository
- Run
npm install
- Run
npm test
to run tests
Console says "Required environment variable 'TWITTER_ACCESS_TOKEN_KEY' is an empty string."
You haven't setup the correct environment variables for Tweet CLI. Double check that you don't have any misspellings in your environment variable settings.
GitHub Actions console says "Required environment variable 'TWITTER_ACCESS_TOKEN_KEY' is an empty string."
You're probably trying to use the Tweet CLI during the pull_request
event. This won't work because secrets are not available when a pull request is from a fork. Try using the pull_request_target
event instead. (More information)
This code is licensed under the Apache 2.0 License (see LICENSE for details).
Copyright Human Who Codes LLC. All rights reserved.