Autohook 🎣
Autohook configures and manages Twitter webhooks for you. Zero configuration. Just run and go!
- 🚀 Spawns a server for you
- ⚙️ Registers a webhook (it removes existing webhooks if you want, and you can add more than one webhook if your Premium subscription supports it)
- ✅ Performs the CRC validation when needed
- 📝 Subscribes to your current user's context (you can always subscribe more users if you need)
- 🎧 Exposes a listener so you can pick up Account Activity events and process the ones you care about
Usage
You can use Autohook as a module or as a command-line tool.
Node.js module
const { Autohook } = require('twitter-autohook');
(async ƛ => {
const webhook = new Autohook();
await webhook.removeWebhooks();
webhook.on('event', event => console.log('Something happened:', event));
await webhook.start();
await webhook.subscribe({oauth_token, oauth_token_secret});
})();
Command line
Starting Autohook from the command line is useful when you need to test your connection and subscriptions.
When started from the command line, Autohook simply provisions a webhook, subscribes your user (unless you specify --do-not-subscribe-me
), and echoes incoming events to stdout
.
$ autohook -rs
$ autohook --help
OAuth
Autohook works only when you pass your OAuth credentials. You won't have to figure out OAuth by yourself – Autohook will work that out for you.
You can pass your OAuth credentials in a bunch of ways.
Create a file named ~/.env.twitter
(sits in your home dir) with the following variables:
TWITTER_CONSUMER_KEY=
TWITTER_CONSUMER_SECRET=
TWITTER_ACCESS_TOKEN=
TWITTER_ACCESS_TOKEN_SECRET=
TWITTER_WEBHOOK_ENV=
Autohook will pick up these details automatically, so you won't have to specify anything in code or via CLI.
Env variables
Useful when you're deploying to remote servers, and can be used in conjunction with your dotenv file.
export TWITTER_CONSUMER_KEY=
export TWITTER_CONSUMER_SECRET=
export TWITTER_ACCESS_TOKEN=
export TWITTER_ACCESS_TOKEN_SECRET=
export TWITTER_WEBHOOK_ENV=
heroku config:set TWITTER_CONSUMER_KEY=value TWITTER_CONSUMER_SECRET=value TWITTER_ACCESS_TOKEN=value TWITTER_ACCESS_TOKEN_SECRET=value TWITTER_WEBHOOK_ENV=value
Directly
Not recommended, because you should always secure your credentials.
Node.js
new Autohook({
token: 'value',
token_secret: 'value',
consumer_key: 'value',
consumer_secret: 'value',
env: 'env',
port: 1337
});
CLI
$ autohook \
--token $TWITTER_ACCESS_TOKEN \
--secret $TWITTER_ACCESS_TOKEN_SECRET \
--consumer-key $TWITTER_CONSUMER_KEY \
--consumer-secret $TWITTER_CONSUMER_SECRET \
--env $TWITTER_WEBHOOK_ENV
Install
$ npm i -g twitter-autohook
$ yarn global add twitter-autohook