-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathapp.js
69 lines (58 loc) · 1.97 KB
/
app.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
require('dotenv').config();
const express = require('express');
const cors = require('cors');
const CronJob = require('cron').CronJob;
const { TwitterBot } = require('./twitter-bot');
const app = express();
const PORT = process.env.PORT || 3000;
app.use(cors());
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
const bot = new TwitterBot({
consumer_key: process.env.CONSUMER_KEY,
consumer_secret: process.env.CONSUMER_KEY_SECRET,
access_token: process.env.ACCESS_TOKEN,
access_token_secret: process.env.ACCESS_TOKEN_SECRET,
triggerWord: process.env.TRIGGER
});
const job = new CronJob(
'0 */3 * * * *',
doJob,
onComplete,
true
);
async function doJob() {
console.log(`execute @ ${new Date().toTimeString()}`);
let tempMessage = {};
try {
const authenticatedUserId = await bot.getAdminUserInfo();
const message = await bot.getDirectMessage(authenticatedUserId);
if (message.id) {
tempMessage = message;
const { data } = await bot.tweetMessage(message);
const response = await bot.deleteMessage(message);
console.log(`... DM has been successfuly reposted with id: ${data.id} @ ${data.created_at}`);
console.log('------------------------------------');
} else {
console.log('no tweet to post');
console.log('------------------------------------');
};
} catch (error) {
console.log(error, 'ERROR.');
console.log('------------------------------------');
if (tempMessage.id) {
await bot.deleteMessage(tempMessage);
};
};
};
async function onComplete() {
console.log('my job is done!');
};
app.get('/', (req, res, next) => {
res.send('Welcome to twitter bot server!');
});
app.get('/trigger', async (req, res, next) => {
job.fireOnTick();
res.send('job triggered!');
});
app.listen(PORT, () => console.log(`Server is listening to port ${PORT}`));