-
Notifications
You must be signed in to change notification settings - Fork 32
/
cli.js
executable file
·89 lines (75 loc) · 3.45 KB
/
cli.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#!/usr/bin/env node
const {
Autohook,
TooManyWebhooksError,
UserSubscriptionError,
} = require('.');
const path = require('path');
const os = require('os');
require('dotenv').config({path: path.resolve(os.homedir(), '.env.twitter')});
const argv = require('commander')
.description('Zero configuration setup Twitter Account Activity API webhooks (Premium).\n\nAll parameters are optional if the corresponding env variable is defined in your env file or in ~/.env.twitter.')
.option('--token <token>', 'your OAuth access token. (Env var: TWITTER_ACCESS_TOKEN)')
.option('--secret <secret>', 'your OAuth access token secret. (Env var: TWITTER_ACCESS_TOKEN_SECRET)')
.option('--consumer-key <consumerKey>', 'your OAuth consumer key. (Env var: TWITTER_CONSUMER_KEY)')
.option('--consumer-secret <consumerSecret>', 'your OAuth consumer secret. (Env var: TWITTER_CONSUMER_SECRET)')
.option('--ngrok-secret <authToken>', 'your ngrok authtoken. (Env var: NGROK_AUTH_TOKEN)')
.option('--env <env>', 'your Premium environment label as defined in https://developer.twitter.com/en/account/environments. (Env var: TWITTER_WEBHOOK_ENV)')
.option('--port <port>', 'port where the local HTTP server should run. Default: 1337. (Env var: PORT)')
.option('--url <url>', 'URL to an existing webhook configured to respond to Twitter')
.option('-s, --subscribe-me', 'subscribes the app to listen to activities from your user context specified by the current access token credentials')
.option('--subscribe <accessToken:accessTokenSecret>', 'subscribes to activities of the Twitter user idenfified by the specified OAuth credentials', (val, prev) => {
const [oauth_token, oauth_secret] = val.split(':');
const oauth = {oauth_token, oauth_secret};
prev.push(oauth);
return prev;
}, [])
.option('-r, --reset', 'remove existing webhooks from the specified environment before starting a new instance')
.parse(process.argv);
const webhook = new Autohook({
token: argv.token || process.env.TWITTER_ACCESS_TOKEN,
token_secret: argv.secret || process.env.TWITTER_ACCESS_TOKEN_SECRET,
consumer_key: argv.consumerKey || process.env.TWITTER_CONSUMER_KEY,
consumer_secret: argv.consumerSecret || process.env.TWITTER_CONSUMER_SECRET,
env: argv.env || process.env.TWITTER_WEBHOOK_ENV,
port: argv.port || process.env.PORT,
});
webhook.on('event', event => console.log(JSON.stringify(event, null, 2)));
const subscribe = async (auth) => {
try {
webhook.subscribe(auth);
} catch(e) {
console.error(e.message);
process.exit(-1);
}
}
(async () => {
if (!!argv.reset) {
await webhook.removeWebhooks();
}
try {
await webhook.start(argv.url || null);
} catch(e) {
switch (e.constructor) {
case TooManyWebhooksError:
console.error('Cannot add webhook: you have exceeded the number of webhooks available',
`to you for the '${argv.env || process.env.TWITTER_WEBHOOK_ENV}' environment.`,
`Use 'autohook -r' to remove your existing webhooks or remove callbacks manually`,
'using the Twitter API.');
break;
default:
console.error('Error:', e.message);
break;
}
process.exit(-1);
}
if (!!argv.subscribeMe) {
await subscribe({
oauth_token: argv.token || process.env.TWITTER_ACCESS_TOKEN,
oauth_token_secret: argv.secret || process.env.TWITTER_ACCESS_TOKEN_SECRET,
});
}
for (oauth in argv.subscribe) {
await subscribe(oauth);
}
})();