-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
251 lines (217 loc) · 9.69 KB
/
index.js
File metadata and controls
251 lines (217 loc) · 9.69 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/* eslint-disable no-unused-vars */
import { Telegraf } from 'telegraf';
import dotenv from 'dotenv';
import { tpl } from './text.js';
import { DB } from './utils/db.js';
import { getStats } from './utils/stats.js';
import moment from 'moment';
import { CUT_OFF_NUMBERS } from './cut_off_numbers.js';
import { remove_application, finalizeSelfCheck, initSelfCheck, refresh_once, visaBulletenTracker } from './tracker.js';
import { log } from './utils/logger.js';
dotenv.config();
const CASE_REGEXP = /^2023(EU|AF|AS|OC|SA|NA)\d{1,7}$/i; // Valid case regexp: 2023 + (EU|AF|AS|OC|SA|NA) + 1...7 digits
const CAPTCHA_REGEXP = /^[A-Z0-9]{4,6}$/i; // Captcha: 4-6 digits, A-Z, 0-9
if (!process.env.BOT_TOKEN || process.env.BOT_TOKEN === '') {
console.error('BOT_TOKEN not set in .env file');
process.exit(1);
}
const bot = new Telegraf(process.env.BOT_TOKEN);
const selfCheckMemory = {};
bot.start(async ctx => {
const stats = await getStats();
ctx.replyWithHTML(tpl('start', ctx.from.language_code, stats));
});
bot.help(async ctx => {
const { id } = ctx.from;
const status = await DB.queryOne('SELECT * FROM application WHERE (notification_tg_id = $1 OR additional_tg_id = $1)', [id]);
let currentStatus;
let subscribed = false;
const sub = await DB.queryOne('SELECT * FROM bul_sub WHERE tg_id = $1', [id]);
if (sub)
subscribed = true;
if (status)
currentStatus = tpl('statuses.tracking', ctx.from.language_code, {
caseNumber: status.application_id,
});
else
currentStatus = tpl('statuses.nottracking', ctx.from.language_code);
const current_cut_off = tpl('current_cut_off', ctx.from.language_code, CUT_OFF_NUMBERS);
ctx.replyWithHTML(tpl('help', ctx.from.language_code, {
status: currentStatus + '\n' + current_cut_off,
subscribed: tpl(subscribed ? 'bul.status_ok' : 'bul.status_not_ok', ctx.from.language_code),
id: ctx.from.id,
}));
});
bot.command('subscribe', async ctx => {
const { id } = ctx.from;
const status = await DB.queryOne('SELECT * FROM bul_sub WHERE tg_id = $1', [id]);
if (status) {
await DB.query('DELETE FROM bul_sub WHERE tg_id = $1', [id]);
return ctx.reply(tpl('bul.unsbscribed', ctx.from.language_code));
}
await DB.query('INSERT INTO bul_sub (tg_id, lang) VALUES ($1, $2)', [id, ctx.from.language_code]);
ctx.replyWithHTML(tpl('bul.subscribed', ctx.from.language_code));
});
bot.command('donate', ctx => ctx.replyWithMarkdown(tpl('donate', ctx.from.language_code)));
bot.command('stats', async ctx => {
const stats = await getStats();
ctx.replyWithHTML(tpl('stats', ctx.from.language_code, stats));
});
bot.command('remove', async ctx => ctx.replyWithHTML(tpl('caseRemoveSure', ctx.from.language_code)));
bot.command('removeSure', async ctx => {
const { id } = ctx.from;
const status = await DB.queryOne('SELECT * FROM application WHERE (notification_tg_id = $1 OR additional_tg_id = $1)', [id]);
if (!status)
return ctx.replyWithHTML(tpl('errors.caseStatusesEmpty', ctx.from.language_code));
const result = await remove_application(status.application_id, id);
if (result)
ctx.replyWithHTML(tpl('caseRemoved', ctx.from.language_code));
log(`[REMOVE] ${ctx.from.username} (${ctx.from.id}) removed case from tracking`);
});
bot.command('status', async ctx => {
const { id } = ctx.from;
const status = await DB.queryOne('SELECT * FROM application WHERE (notification_tg_id = $1 OR additional_tg_id = $1)', [id]);
if (!status)
return ctx.reply(tpl('errors.caseStatusesEmpty', ctx.from.language_code));
const { application_id } = status;
const statuses = await DB.query('SELECT * FROM history WHERE application_id = $1 ORDER BY record_id DESC', [application_id]);
if (!statuses.length)
return ctx.reply(tpl('errors.caseNotTracked', ctx.from.language_code));
// build log: status (date changed)
const latestStatus = statuses[0];
const applicationRegion = application_id.substring(4, 6);
const applicationNumber = parseInt(application_id.substring(6));
let cutOffString;
if (CUT_OFF_NUMBERS[applicationRegion] < applicationNumber)
cutOffString = tpl('cutoff', ctx.from.language_code, {
region: applicationRegion,
cutOffNumbers: CUT_OFF_NUMBERS[applicationRegion],
}) + '\n';
else
cutOffString = '\n';
ctx.replyWithHTML(tpl('caseStatus', ctx.from.language_code, {
num: application_id,
status: latestStatus.status,
statusUpdated: new Date(latestStatus.created_at).toLocaleString(ctx.from.language_code),
statusSince: moment(latestStatus.created_at).locale(ctx.from.language_code).fromNow(),
cutOffString,
checked: status.last_checked ? new Date(status.last_checked).toLocaleString(ctx.from.language_code) : tpl('statuses.inprocess', ctx.from.language_code),
checkedSince: status.last_checked ? moment(status.last_checked).locale(ctx.from.language_code).fromNow() : '🔄',
}));
});
bot.command('self', async ctx => {
// Trigger self update, and ask user to solve captcha
const { id } = ctx.from;
const status = await DB.queryOne('SELECT * FROM application WHERE (notification_tg_id = $1 OR additional_tg_id = $1)', [id]);
if (!status)
return ctx.replyWithHTML(tpl('errors.caseStatusesEmpty', ctx.from.language_code));
const { application_id } = status;
// Minimum recheck time is 10 minutes
if (status.last_checked && (Date.now() - status.last_checked) < 10 * 60 * 1000)
return ctx.replyWithHTML(tpl('errors.tooFast', ctx.from.language_code));
try {
const result = await initSelfCheck(application_id);
const imgbase = Buffer.from(result.captcha, 'base64');
selfCheckMemory[ctx.from.id] = {
cached: Date.now(),
application_id,
...result,
};
ctx.replyWithPhoto({
source: imgbase,
}, {
caption: tpl('selfCheck', ctx.from.language_code, {
num: application_id,
}),
text: tpl('selfCheck', ctx.from.language_code, {
num: application_id,
}),
});
log(`[${application_id}] Self check triggered by ${ctx.from.id}`);
} catch (e) {
ctx.replyWithHTML(tpl('errors.captcha', ctx.from.language_code));
}
});
bot.command('force', async ctx => {
// Admin tool to force check
const { id } = ctx.from;
if (id !== +process.env.ADMIN_ID)
return ctx.replyWithHTML(tpl('errors.noAccess', ctx.from.language_code));
// Reset check time
await DB.query('UPDATE application SET last_checked = NULL WHERE notification_tg_id = $1', [id]);
ctx.replyWithHTML(tpl('force', ctx.from.language_code));
});
bot.command('access', async ctx => {
// add access to case (for additional tg id from argument)
const { id } = ctx.from;
const { text } = ctx.message;
if (!text)
return ctx.replyWithHTML(tpl('errors.noAccessParam', ctx.from.language_code));
const status = await DB.queryOne('SELECT * FROM application WHERE notification_tg_id = $1', [id]);
if (!status)
return ctx.replyWithHTML(tpl('errors.caseStatusesEmpty', ctx.from.language_code));
const { application_id } = status;
const [_, additional_tg_id] = text.match(/\/access\s+(\d+)/) || [];
if (!additional_tg_id)
return ctx.replyWithHTML(tpl('errors.noAccessParam', ctx.from.language_code));
await DB.query('UPDATE application SET additional_tg_id = $1 WHERE application_id = $2', [additional_tg_id, application_id]);
ctx.replyWithHTML(tpl('access', ctx.from.language_code, {
num: application_id,
additional_tg_id,
}));
});
bot.on('text', async ctx => {
const { id } = ctx.from;
const { text } = ctx.message;
if (text.length > 3 && text.length <= 6 && selfCheckMemory[id] && CAPTCHA_REGEXP.test(text)) {
const cache = selfCheckMemory[id];
const [isSuccess, status] = await finalizeSelfCheck(cache.application_id, text.toUpperCase(), cache);
delete selfCheckMemory[id];
if (isSuccess) {
await DB.query('UPDATE application SET last_checked = NOW() WHERE (notification_tg_id = $1 OR additional_tg_id = $1)', [id]);
await DB.query('INSERT INTO history (application_id, status) VALUES ($1, $2) ON CONFLICT DO NOTHING', [cache.application_id, status]);
return ctx.replyWithHTML(tpl('selfCheckSuccess', ctx.from.language_code, {
status,
num: cache.application_id,
}));
} else {
await DB.query('UPDATE application SET last_checked = NOW(), last_error = $1 WHERE notification_tg_id = $2', [status, id]);
return ctx.replyWithHTML(tpl('errors.selfCheckFail', ctx.from.language_code));
}
}
if (!CASE_REGEXP.test(text))
return ctx.replyWithHTML(tpl('errors.invalidCaseNumber', ctx.from.language_code));
const caseId = text.toUpperCase();
log(`[INFO] User ${id} added case ${caseId}`);
const isCaseExists = await DB.queryOne('SELECT * FROM application WHERE (application_id = $1 OR additional_tg_id = $1)', [caseId]);
if (isCaseExists)
return ctx.replyWithHTML(tpl('errors.caseAlreadyTracked', ctx.from.language_code));
// Case limit
const casesCount = await DB.queryOne('SELECT COUNT(*) FROM application WHERE notification_tg_id = $1', [id]);
if (casesCount.count >= 1)
return ctx.replyWithHTML(tpl('errors.caseLimitPerUser', ctx.from.language_code));
const result = await DB.queryOne('INSERT INTO application (application_id, notification_tg_id, lang) VALUES ($1, $2, $3) RETURNING *', [caseId, id, ctx.from.language_code]);
if (result)
ctx.replyWithHTML(tpl('caseAdded', ctx.from.language_code));
else
ctx.replyWithHTML(tpl('errors.caseAlreadyAdded', ctx.from.language_code));
});
bot.launch();
log('[INFO] Bot started');
if (process.env.ADMIN_ID)
bot.telegram.sendMessage(+process.env.ADMIN_ID, 'Bot started');
if (!process.env.DEBUG) {
log('[INFO] Starting tracker');
// Track (aka cron)
visaBulletenTracker();
refresh_once();
setInterval(refresh_once, 1000 * 60 * 5); // 30 minutes
setInterval(visaBulletenTracker, 1000 * 60 * 60); // 1 hour
}
// Clean selfCheckMemory every 5 minutes (to prevent memory leak)
setInterval(() => {
const now = Date.now();
for (const id in selfCheckMemory)
if (now - selfCheckMemory[id].cached > 1000 * 60 * 5)
delete selfCheckMemory[id];
}, 1000 * 60 * 5);