-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.js
38 lines (33 loc) · 1.03 KB
/
bot.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
'use strict';
const {generateTweet} = require('./src/generator');
const {postTweet} = require('./src/twitter');
const TWEET_TIME_INTERVAL = 1000 * 60 * 60 * 4; // 4 Hours
const RETRY_ATTEMPTS = 4;
const startBot = () => {
console.log('Starting Bot...');
setImmediate(generateAndPostTweet);
setInterval(() => {
generateAndPostTweet();
}, TWEET_TIME_INTERVAL);
};
const generateAndPostTweet = (attempts = 1) => {
if (attempts >= RETRY_ATTEMPTS) {
return console.log('Max retries attempted...');
}
console.log('Attempting to generate tweet...');
generateTweet((error, tweet) => {
if (error) {
console.log(`Error generating tweet on attempt ${attempts}: ${error}`);
return generateAndPostTweet(++attempts);
}
console.log(`Generated tweet: ${tweet}`);
postTweet(tweet, error => {
if (error) {
console.log(`Error posting tweet on attempt ${attempts}: ${error}`);
return generateAndPostTweet(++attempts);
}
console.log(`Tweeted: ${tweet}`);
});
});
};
startBot();