-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathstandalone-server.js
85 lines (71 loc) · 2.19 KB
/
standalone-server.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
const {Autohook, validateWebhook, validateSignature} = require('..');
const url = require('url');
const ngrok = require('ngrok');
const http = require('http');
const PORT = process.env.PORT || 4242;
const startServer = (port, auth) => http.createServer((req, res) => {
const route = url.parse(req.url, true);
if (!route.pathname) {
return;
}
if (route.query.crc_token) {
try {
if (!validateSignature(req.headers, auth, url.parse(req.url).query)) {
console.error('Cannot validate webhook signature');
return;
};
} catch (e) {
console.error(e);
}
const crc = validateWebhook(route.query.crc_token, auth, res);
res.writeHead(200, {'content-type': 'application/json'});
res.end(JSON.stringify(crc));
}
if (req.method === 'POST' && req.headers['content-type'] === 'application/json') {
let body = '';
req.on('data', chunk => {
body += chunk.toString();
});
req.on('end', () => {
try {
if (!validateSignature(req.headers, auth, body)) {
console.error('Cannot validate webhook signature');
return;
};
} catch (e) {
console.error(e);
}
console.log('Event received:', body);
res.writeHead(200);
res.end();
});
}
}).listen(port);
(async () => {
try {
const NGROK_AUTH_TOKEN = process.env.NGROK_AUTH_TOKEN;
if (NGROK_AUTH_TOKEN) {
await ngrok.authtoken(process.env.NGROK_AUTH_TOKEN);
}
const url = await ngrok.connect(PORT);
const webhookURL = `${url}/standalone-server/webhook`;
const config = {
token: process.env.TWITTER_ACCESS_TOKEN,
token_secret: process.env.TWITTER_ACCESS_TOKEN_SECRET,
consumer_key: process.env.TWITTER_CONSUMER_KEY,
consumer_secret: process.env.TWITTER_CONSUMER_SECRET,
env: process.env.TWITTER_WEBHOOK_ENV,
};
const server = startServer(PORT, config);
const webhook = new Autohook(config);
await webhook.removeWebhooks();
await webhook.start(webhookURL);
await webhook.subscribe({
oauth_token: config.token,
oauth_token_secret: config.token_secret,
});
} catch(e) {
console.error(e);
process.exit(-1);
}
})();