-
Notifications
You must be signed in to change notification settings - Fork 52
/
bot.js
554 lines (491 loc) · 18.1 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
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
#!/usr/bin/env node
'use strict';
const config = require('./config.js');
const fs = require('fs-extra');
const path = require('path');
const { Telegraf } = require('telegraf');
const sharp = require('sharp');
const JSZip = require('jszip');
const axios = require('axios');
const ffmpeg = require('fluent-ffmpeg');
const bot = new Telegraf(config.token);
let messages = {};
loadLang();
let ramdb = {};
let langSession = {};
bot.use((ctx, next) => {
if (ctx.message && !langSession[ctx.message.chat.id]) {
langSession[ctx.message.chat.id] = config.default_lang;
}
return next();
});
// Check storage path
let fspath = path.resolve(config.file_storage);
if (!fs.existsSync(fspath)) {
logger('INTERNAL', 'info', messages[config.default_lang].app.storagepathnotexist);
fs.mkdirpSync(fspath);
}
bot.catch((err) => {
logger('INTERNAL', 'error', err);
});
bot.command('lang', (ctx) => {
i18nHandler(ctx);
});
bot.command('newpack', (ctx) => {
let chatId = ctx.message.chat.id;
if (ramdb[chatId] && ramdb[chatId].islocked) {
return ctx.reply(messages[langSession[chatId]].msg.tasklocked);
}
if (ramdb[chatId] && ramdb[chatId].files.length > 0) {
return ctx.reply(messages[langSession[chatId]].msg.taskexist);
}
newPackHandler(ctx, () => {
return ctx.reply(messages[langSession[chatId]].msg.newpack.replace('%max%', config.maximages));
});
});
bot.command('finish', async (ctx) => {
let chatId = ctx.message.chat.id;
if (ramdb[chatId] && ramdb[chatId].islocked) {
return ctx.reply(messages[langSession[chatId]].msg.tasklocked);
}
let args = ctx.message.text.split(' ').slice(1).join(' ').trim();
let format = args === 'png' ? 'png' : 'jpg'; // Default to 'jpg' if no 'png' parameter is provided
if (ramdb[chatId] && ramdb[chatId].files.length > 0) {
await finishHandler(ctx, format);
} else {
ctx.reply(messages[langSession[chatId]].msg.nosticker);
}
});
bot.command('sources', (ctx) => {
sourcesHandler(ctx);
});
bot.command('cancel', (ctx) => {
cancellationHandler(ctx);
});
bot.on('message', (ctx) => {
generalMsgHandler(ctx);
});
bot.launch();
async function errMsgHandler(ctx, err) {
let chatId = ctx.message.chat.id;
if (err) {
await ctx.reply(messages[langSession[chatId]].msg.errmsg
.replace('%errcode%', err.code || '')
.replace('%errbody%', err.response?.body || err.message));
} else {
await ctx.reply(messages[langSession[chatId]].msg.error);
}
return cleanup(chatId);
}
function newPackHandler(ctx, callback) {
let chatId = ctx.message.chat.id;
logger(chatId, 'info', 'Started a new pack task.');
ramdb[chatId] = {
start: ctx.message.date,
files: [],
srcimg: [],
destimg: [],
islocked: false
};
callback();
}
async function finishHandler(ctx, format) {
try {
let chatId = ctx.message.chat.id;
logger(chatId, 'info', 'Starting pack task...');
ramdb[chatId].islocked = true;
let fpath = {
packpath: path.join(config.file_storage, chatId.toString())
};
fpath['srcpath'] = path.join(fpath.packpath, 'src');
fpath['imgpath'] = path.join(fpath.packpath, 'img');
fs.mkdirpSync(fpath.packpath);
fs.mkdirpSync(fpath.srcpath);
fs.mkdirpSync(fpath.imgpath);
await ctx.reply(messages[langSession[chatId]].msg.downloading);
await downloadHandler(ctx, fpath);
await ctx.reply(messages[langSession[chatId]].msg.converting);
await convertHandler(ctx, fpath, format);
await ctx.reply(messages[langSession[chatId]].msg.packaging);
// Batch images and send ZIP files
await batchAndSendZipFiles(ctx, fpath);
cleanup(chatId);
logger(chatId, 'info', 'Task finished.');
} catch (err) {
errMsgHandler(ctx, err);
}
}
async function batchAndSendZipFiles(ctx, fpath) {
let chatId = ctx.message.chat.id;
let files = ramdb[chatId].destimg;
let batches = [];
let currentBatch = [];
let currentBatchSize = 0;
// Batch images based on file sizes
for (let file of files) {
let fileSize = fs.statSync(file).size;
if (currentBatchSize + fileSize > config.maxfilebytes && currentBatch.length > 0) {
batches.push(currentBatch);
currentBatch = [];
currentBatchSize = 0;
}
currentBatch.push(file);
currentBatchSize += fileSize;
}
// Add the last batch if it has any files
if (currentBatch.length > 0) {
batches.push(currentBatch);
}
logger(chatId, 'info', `Total batches to send: ${batches.length}`);
// Send each batch as a ZIP file
for (let i = 0; i < batches.length; i++) {
let batch = batches[i];
let zipFilePath = await createZipForBatch(ctx, fpath, batch, i + 1);
await ctx.telegram.sendDocument(ctx.from.id, {
source: fs.createReadStream(zipFilePath),
filename: `stickers_${chatId}_${i + 1}.zip`
});
// Clean up the sent files and ZIP
fs.unlinkSync(zipFilePath);
for (let file of batch) {
fs.unlinkSync(file);
}
logger(chatId, 'info', `Sent batch ${i + 1} and cleaned up.`);
}
}
async function createZipForBatch(ctx, fpath, batchFiles, batchNumber) {
let chatId = ctx.message.chat.id;
let zip = new JSZip();
for (let file of batchFiles) {
let fname = path.basename(file);
logger(chatId, 'info', `Adding file ${fname} to batch ${batchNumber}`);
zip.file(fname, fs.readFileSync(file));
}
let zipContent = await zip.generateAsync({
compression: 'DEFLATE',
type: 'nodebuffer',
comment: 'Created by telegram-stickerimage-bot',
platform: process.platform
});
let zipFilePath = path.join(fpath.packpath, `stickers_${chatId}_${batchNumber}.zip`);
fs.writeFileSync(zipFilePath, zipContent);
return zipFilePath;
}
function cancellationHandler(ctx) {
let chatId = ctx.message.chat.id;
if (!ramdb[chatId]) {
return ctx.reply(messages[langSession[chatId]].msg.notask);
}
delete ramdb[chatId];
cleanup(chatId);
logger(chatId, 'info', 'Task Cancelled.');
return ctx.reply(messages[langSession[chatId]].msg.taskcancelled);
}
function sourcesHandler(ctx) {
let chatId = ctx.message.chat.id;
return ctx.reply(messages[langSession[chatId]].msg.supported_sticker_sources.replace('%sources%', config.sticker_sources.reduce((x, c) => `${x}\n${c}`)));
}
function generalMsgHandler(ctx) {
let chatId = ctx.message.chat.id;
if (ctx.chat.type !== 'private') return;
if (ramdb[chatId] && !ramdb[chatId].islocked) {
if (ctx.message.sticker) {
addSticker(ctx);
}
if (ctx.message.entities) {
ctx.message.entities.forEach((e) => {
if (e.type === 'url') {
let url = ctx.message.text.slice(e.offset, e.offset + e.length);
if (config.sticker_sources.find((x) => url.startsWith(x)) &&
url.length > 25) {
stickerSetHandler(ctx, path.basename(url));
} else {
ctx.reply(messages[langSession[chatId]].msg.unsupported_sticker_source.replace('%source%', url));
}
}
});
}
} else if (!ramdb[chatId] && ctx.message.sticker) {
directHandler(ctx);
} else {
ctx.reply((ramdb[chatId] && ramdb[chatId].islocked) ?
messages[langSession[chatId]].msg.tasklocked :
messages[langSession[chatId]].msg.start);
}
}
function i18nHandler(ctx) {
let chatId = ctx.message.chat.id;
let chosen_lang = ctx.message.text.split(' ').slice(1).join('').trim();
if (config.available_lang.hasOwnProperty(chosen_lang)) {
langSession[chatId] = chosen_lang;
logger(chatId, 'info', 'Changing language to: ' + chosen_lang);
return ctx.reply(messages[langSession[chatId]].msg.language_change);
}
let message = messages[langSession[chatId]].msg.language_available;
let languages_names = '';
for (let k in config.available_lang) {
if (config.available_lang.hasOwnProperty(k)) {
languages_names += '\n' + '[' + k + '] ' + config.available_lang[k].join(' / ');
}
}
return ctx.reply(message.replace('%languages%', languages_names));
}
async function downloadHandler(ctx, fpath) {
let chatId = ctx.message.chat.id;
logger(chatId, 'info', 'Downloading files...');
for (let fileId of ramdb[chatId].files) {
try {
let { url, ext } = await resolveFile(ctx, fileId);
let destFile = path.join(fpath.srcpath, fileId + ext);
await download(url, destFile);
logger(chatId, 'info', 'File ' + fileId + ' saved to disk.');
ramdb[chatId].srcimg.push(destFile);
} catch (err) {
logger(chatId, 'error', 'Error downloading file ' + fileId + ': ' + err);
}
}
}
async function convertHandler(ctx, fpath, format) {
let chatId = ctx.message.chat.id;
logger(chatId, 'info', 'Converting images...');
for (let src of ramdb[chatId].srcimg) {
try {
let dest = await convert(ctx, src, fpath, format);
ramdb[chatId].destimg.push(dest);
} catch (err) {
logger(chatId, 'error', 'Error converting file ' + src + ': ' + err);
}
}
}
async function zipHandler(ctx) {
let chatId = ctx.message.chat.id;
logger(chatId, 'info', 'Adding files to ZIP file...');
let zip = new JSZip();
for (let src of ramdb[chatId].srcimg) {
let fname = path.join(chatId.toString(), 'src', path.basename(src));
logger(chatId, 'info', 'Adding file ' + fname);
zip.file(fname, fs.readFileSync(src));
}
for (let dest of ramdb[chatId].destimg) {
let fname = path.join(chatId.toString(), 'img', path.basename(dest));
logger(chatId, 'info', 'Adding file ' + fname);
zip.file(fname, fs.readFileSync(dest));
}
logger(chatId, 'info', 'Packaging files...');
let content = await zip.generateAsync({
compression: 'DEFLATE',
type: 'nodebuffer',
comment: 'Created by telegram-stickerimage-bot',
platform: process.platform
});
return content;
}
function stickerSetHandler(ctx, setName) {
let chatId = ctx.message.chat.id;
ctx.reply(messages[langSession[chatId]].msg.get_set_info);
bot.telegram.getStickerSet(setName)
.then((set) => {
if (ramdb[chatId].files.length + set.stickers.length >= config.maximages) {
return ctx.reply(messages[langSession[chatId]].msg.taskfull);
}
logger(chatId, 'info', 'Adding Sticker Set: ' + setName);
addSet(ctx, set);
})
.catch((err) => {
logger(chatId, 'error', 'Error Adding Sticker Set: ' + setName + ': ' + err);
ctx.reply(messages[langSession[chatId]].msg.invalid_set.replace('%setName%', setName));
});
}
async function directHandler(ctx) {
let chatId = ctx.message.chat.id;
let messageId = ctx.message.message_id;
let format = 'jpg'; // Default format
newPackHandler(ctx, () => { });
ramdb[chatId].islocked = true;
let fpath = {
packpath: path.join(config.file_storage, chatId.toString())
};
fpath['srcpath'] = path.join(fpath.packpath, 'src');
fpath['imgpath'] = path.join(fpath.packpath, 'img');
fs.mkdirpSync(fpath.packpath);
fs.mkdirpSync(fpath.srcpath);
fs.mkdirpSync(fpath.imgpath);
logger(chatId, 'info', 'Started direct image task.');
let pendingMsg = await ctx.reply(messages[langSession[chatId]].msg.direct_task_started);
try {
let { url, ext } = await resolveFile(ctx, ctx.message.sticker.file_id);
let destFile = path.join(fpath.srcpath, ctx.message.sticker.file_id + ext);
await download(url, destFile);
// Determine format based on sticker type
if (ctx.message.sticker.is_animated || ctx.message.sticker.is_video) {
format = 'gif';
}
let outputFile = await convert(ctx, destFile, fpath, format);
// Send the file as a document with disable_content_type_detection, though, doesn't seem to work
await ctx.replyWithDocument({
source: fs.createReadStream(outputFile),
filename: path.basename(outputFile)
}, {
reply_to_message_id: messageId,
disable_content_type_detection: true
});
await ctx.deleteMessage(pendingMsg.message_id);
cleanup(chatId);
} catch (err) {
cleanup(chatId);
logger(chatId, 'error', 'Error direct image task:' + err);
await ctx.reply(
messages[langSession[chatId]].msg.error,
{ reply_to_message_id: messageId }
);
}
}
function addSticker(ctx) {
let chatId = ctx.message.chat.id;
if (ramdb[chatId].files.includes(ctx.message.sticker.file_id)) {
return ctx.reply(messages[langSession[chatId]].msg.duplicated_sticker, { reply_to_message_id: ctx.message.message_id });
}
if (ramdb[chatId].files.length >= config.maximages) {
return ctx.reply(messages[langSession[chatId]].msg.taskfull);
}
ramdb[chatId].files.push(ctx.message.sticker.file_id);
let remain = config.maximages - ramdb[chatId].files.length;
ctx.reply(remain === 0 ?
messages[langSession[chatId]].msg.taskfull :
messages[langSession[chatId]].msg.saved.replace('%remain%', remain));
}
function addSet(ctx, set) {
let chatId = ctx.message.chat.id;
let originCount = ramdb[chatId].files.length;
set.stickers.forEach((s) => {
if (!ramdb[chatId].files.includes(s.file_id)) {
ramdb[chatId].files.push(s.file_id);
}
});
ctx.reply(messages[langSession[chatId]].msg.set_added_count
.replace('%sticker_count%', ramdb[chatId].files.length - originCount));
}
async function resolveFile(ctx, fileId) {
let chatId = ctx.message.chat.id;
try {
let file = await ctx.telegram.getFile(fileId);
let url = `https://api.telegram.org/file/bot${config.token}/${file.file_path}`;
let ext = path.extname(file.file_path) || '';
return { url, ext };
} catch (err) {
await ctx.reply(
messages[langSession[chatId]].msg.err_get_filelink.replace('%fileId%', fileId)
);
logger(chatId, 'error', 'Get File Link for ' + fileId + ': ' + err);
throw err;
}
}
async function download(url, dest) {
const response = await axios({
method: 'get',
url: url,
responseType: 'stream',
});
const writer = fs.createWriteStream(dest);
response.data.pipe(writer);
return new Promise((resolve, reject) => {
writer.on('finish', resolve);
writer.on('error', (err) => {
fs.unlink(dest, () => { });
reject(err);
});
});
}
async function convert(ctx, src, fpath, format) {
let chatId = ctx.message.chat.id;
let destimg;
let width = 512; // Default width
let ext = path.extname(src).toLowerCase();
if (ext === '.tgs' || ext === '.webm') {
// Animated sticker, convert to GIF regardless of the format parameter
destimg = path.join(fpath.imgpath, path.basename(src, ext) + '.gif');
if (ext === '.tgs') {
await convertTgsToGif(src, destimg, width);
} else {
await convertWebmToGif(src, destimg, width);
}
} else {
// Static image, convert based on the format parameter (jpg or png)
destimg = path.join(fpath.imgpath, path.basename(src, ext) + '.' + format);
await convertImage(src, destimg, width, format);
}
return destimg;
}
async function convertWebmToGif(src, dest, width) {
return new Promise((resolve, reject) => {
ffmpeg(src)
.outputOptions([
'-vf', `scale=${width}:-1:flags=lanczos`,
'-y'
])
.toFormat('gif')
.save(dest)
.on('end', resolve)
.on('error', reject);
});
}
async function convertTgsToGif(src, dest, width) {
const fs = require('fs-extra');
const path = require('path');
const { exec } = require('child_process');
// Define paths
const jsonPath = src + '.json';
// Decompress the .tgs file to JSON
await new Promise((resolve, reject) => {
const command = `gzip -dc "${src}" > "${jsonPath}"`;
exec(command, (error, stdout, stderr) => {
if (error) {
console.error(`Error decompressing .tgs file: ${stderr}`);
reject(error);
} else {
resolve();
}
});
});
// Use lottie2gif to convert JSON to GIF
await new Promise((resolve, reject) => {
const command = `${config.lottie2gif} -o "${dest}" "${jsonPath}"`;
exec(command, (error, stdout, stderr) => {
if (error) {
console.error(`Error converting JSON to GIF: ${stderr}`);
reject(error);
} else {
resolve();
}
});
});
// Clean up the temporary JSON file
fs.unlinkSync(jsonPath);
}
async function convertImage(src, dest, width, format) {
let image = sharp(src);
if (width && width < 512) {
image = image.resize(width, width, { fit: 'inside' });
}
if (format === 'png') {
await image.toFile(dest);
} else {
await image.flatten({ background: '#FFFFFF' }).jpeg().toFile(dest);
}
}
function cleanup(id) {
logger(id, 'info', 'Cleaning up...');
delete ramdb[id];
fs.removeSync(path.resolve(config.file_storage, id.toString()));
}
function loadLang() {
for (let k in config.available_lang) {
if (config.available_lang.hasOwnProperty(k)) {
messages[k] = JSON.parse(fs.readFileSync(path.resolve('./lang/' + k + '.json'), 'utf8'));
logger('INTERNAL', 'info', 'Loaded language strings: ' + k);
}
}
}
function logger(chatId, type, msg) {
console.log('[' + chatId + ']', '[' + type.toUpperCase() + ']', msg);
}