-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
79 lines (67 loc) · 3.35 KB
/
server.js
File metadata and controls
79 lines (67 loc) · 3.35 KB
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
const express = require('express');
const bodyParser = require('body-parser');
const cors = require('cors');
const Twitter = require('twitter');
process.on('uncaughtException', (error) => {
console.log('[❗⚠️🛑💢 Something happened with the bot!] ' + error);
process.exit(1);
});
if (!process.env.NODE_ENV) {
require('./config');
} else {
CONSUMER_KEY = process.env.CONSUMER_KEY;
CONSUMER_SECRET = process.env.CONSUMER_SECRET;
ACCESS_TOKEN_KEY = process.env.ACCESS_TOKEN_KEY;
ACCESS_TOKEN_SECRET = process.env.ACCESS_TOKEN_SECRET;
}
const app = express();
app.use(cors()); // for setting Access-Control-Allow-Origin to the * any origin wildcard
app.use(bodyParser.json()); // for using req.body
const twitterClient = new Twitter({
consumer_key: CONSUMER_KEY,
consumer_secret: CONSUMER_SECRET,
access_token_key: ACCESS_TOKEN_KEY,
access_token_secret: ACCESS_TOKEN_SECRET
});
// For sending server or feedback notifications
app.post('/notification', (req, res) => {
const originalJSONString = JSON.stringify(req.body, null, ' ');
const URIEncodedString = encodeURIComponent(originalJSONString);
const shortenedURIEncodedString = URIEncodedString.substr(0, 280);
const decodedShortenedURIEncodedString = decodeURIComponent(shortenedURIEncodedString);
twitterClient.post('statuses/update', { status: decodedShortenedURIEncodedString }, (error, tweet, response) => {
if (error) {
// a free heroku account will put up to about 100 console messages into a logging mechanism that you can still access
console.log('[❌ Failed to post tweet] ' + JSON.stringify({ 'Twitter error': error, notification: req.body }, null, ' '));
return res.send({ success: false });
} else {
console.log('[✉️ Server notification sent] ' + JSON.stringify({ notification: req.body }, null, ' '));
return res.send({ success: true });
}
});
});
// uncaught API error handling route
app.use(function (error, req, res, next) {
// post the error to Twitter
const adHocID = Math.random().toString(36).substr(2, 5); // to prevent duplicate tweet errors
const twitterPost = '[❌ API error] ' + error + '\n\n' + adHocID + '\n\nSee logs for more.';
const URIEncodedString = encodeURIComponent(twitterPost);
const shortenedURIEncodedString = URIEncodedString.substr(0, 280);
const decodedShortenedURIEncodedString = decodeURIComponent(shortenedURIEncodedString);
twitterClient.post('statuses/update', { status: decodedShortenedURIEncodedString }, (twitterError, tweet, response) => {
if (twitterError) {
console.log('[❌❌ Failed to post API error tweet] ' + error + ' ' + JSON.stringify({ 'Twitter error': twitterError, 'API error details': error, 'req.body': req.body }, null, ' '));
} else {
console.log('[❌✉️ API error server notification sent] ' + error + ' ' + JSON.stringify({ 'API error details': error, 'req.body': req.body }, null, ' '));
}
});
return res.send({ success: false });
});
const PORT = process.env.PORT || '3000';
app.set('port', PORT);
app.listen(PORT, (error) => {
if (error) {
return console.log('[❗ Something unexpected happened when starting up] ', error);
}
console.log('[🤖 Twitter Server and Feedback Notification Bot] has been started up 🆙');
});