-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
97 lines (84 loc) · 3.8 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import fs from 'fs';
import axios from 'axios';
import { Telegram, Telegraf } from 'telegraf';
import './config/config.js';
import { log } from './util/logger.js';
import mongoose from './db/mongoose.js';
import Constants from './Constants.js';
import entryHelper from './entryHelper.js';
import commandController from './commandController.js';
import subscriptionManager from "./subscriptionManager.js";
const dataObj = JSON.parse(fs.readFileSync(Constants.DATA_FILE_PATH, 'utf8'));
// -----------------------------------------------------------------------------------------------
// === INITIALIZATIONS ===
// Telegram object for sending texts on cron runs.
const tg = new Telegram(process.env.BOT_TOKEN);
// Bot object for handling commands.
const bot = new Telegraf(process.env.BOT_TOKEN);
const iterationUpdatesFound = {};
// -----------------------------------------------------------------------------------------------
// === MAIN FUNCTIONS ===
// Function to fetch latest entries and update the dataObj.
const updateEntries = async (entryType) => {
try {
log(`Updating ${entryType} entries.`);
const htmlResponse = await axios.get(dataObj.urls[entryType], {
signal: AbortSignal.timeout(Constants.AXIOS_RESPONSE_TIMEOUT),
timeout: Constants.AXIOS_RESPONSE_TIMEOUT
});
const message = entryHelper.extractEntriesFromPage(entryType, htmlResponse.data);
if (message !== dataObj['savedMessages'][entryType]) {
log(`=> Found new ${entryType} entries.\n`);
dataObj['savedMessages'][entryType] = message;
iterationUpdatesFound[entryType] = true;
} else {
log(`=> No ${entryType} updates.\n`);
}
} catch (err) {
log(`=> Error in fetching ${entryType} entries:`);
log(err);
if (Constants.MONITOR_FAILURE_ENTRY_TYPES.includes(entryType)) {
await tg.sendMessage(dataObj['masterChatID'], `-ERROR-\nError in fetching ${entryType} entries.\n`, Constants.MARKDOWN);
}
}
};
const setupBotCommands = () => {
Constants.COMMAND_ENTRY_LIST.forEach(([command, entryType]) => {
bot.command(command, (ctx) => commandController.handleFetchCommand(dataObj, entryType, ctx));
bot.command(`${Constants.COMMAND_SUBSCRIBE}${command}`, (ctx) => commandController.handleSubscribeCommand(entryType, ctx));
bot.command(`${Constants.COMMAND_UNSUBSCRIBE}${command}`, (ctx) => commandController.handleUnsubscribeCommand(entryType, ctx));
});
bot.command('start', ((ctx) => ctx.replyWithMarkdown(dataObj['startMessage'])));
bot.command('help', ((ctx) => ctx.replyWithMarkdown(dataObj['startMessage'])));
bot.command('test', ((ctx) => ctx.replyWithMarkdown('Test Successful.')));
};
const afterMain = () => {
log('=> Main executed. Starting bot.');
setupBotCommands();
bot.launch();
setTimeout(() => {
log('=> Stopping bot. Exiting process.');
process.exit(0);
}, Constants.BOT_UPTIME);
}
// -----------------------------------------------------------------------------------------------
// === EXECUTION ===
const main = async () => {
await Promise.all(Constants.ALL_ENTRY_TYPES.map(updateEntries));
if (Object.values(iterationUpdatesFound).includes(true)) {
log('=> Found updates. Writing to file.');
entryHelper.writeDataFile(dataObj);
}
await subscriptionManager.notifyAllSubscribers(tg, dataObj, iterationUpdatesFound);
};
log(`Script started | ENV : ${process.env.ENVIRONMENT}`);
main()
.then(afterMain);
// -----------------------------------------------------------------------------------------------
// === GRACEFUL STOP ===
Constants.GRACEFUL_STOP_SIGNALS.forEach(signal => {
process.on(signal, () => {
log(`Received signal: ${signal}. Stopping bot`);
bot.stop(signal);
});
});