-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
55 lines (43 loc) · 1.47 KB
/
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
require('dotenv').config();
const express = require('express');
const bodyParser = require('body-parser');
const Twit = require('twit');
const Cron = require('cron').CronJob;
const Twot = new Twit({
consumer_key: process.env.CONSUMER_KEY,
consumer_secret: process.env.CONSUMER_SECRET,
access_token: process.env.ACCESS_TOKEN,
access_token_secret: process.env.ACCESS_TOKEN_SECRET,
});
const app = express();
const port = process.env.PORT;
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));
app.post('/update', async (req, res) => {
let result;
let status;
({ status } = req.body);
try {
result = await Twot.post('statuses/update', { status: `[${new Date}] ${status}` });
} catch (e) {
return res.status(500).json({ message: 'Failed!' });
}
return res.status(200).json({ message: 'Success!', data: result.data });
});
app.listen(port, () => {
console.log(`[${new Date}] App listening on port ${port}`);
});
const job = new Cron('0 */60 * * * *', async () => {
let result;
let status = 'Tweeted from Express.js using Twit library https://www.npmjs.com/package/twit';
try {
result = await Twot.post('statuses/update', { status: `[${new Date}] ${status}` });
} catch (e) {
console.log(`[${new Date}] Failed : ${e.message}`);
}
console.log(`[${new Date}] Success : ${JSON.stringify(result.data, null, 2)}`);
});
job.start();
module.exports = app;