-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathapp.js
890 lines (868 loc) · 35.6 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
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
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
import { run as grammyjsRun } from '@grammyjs/runner'
import config from './config.js'
import handlers from './handlers/index.js'
import db from './db.js'
import { update_setting } from './db.js'
const { asyncForEach, handle_illust, handle_ranking, handle_novel, get_pixiv_ids, get_user_illusts, ugoira_to_mp4, download_file, _l, k_os, k_link_setting, mg_create, mg_albumize, mg_filter, mg2telegraph, read_user_setting, honsole, handle_new_configuration, exec, sleep, reescape_strings, get_ugoira_path } = handlers
import { tgBot as bot } from './bot.js'
import axios from 'axios'
import { InputFile } from 'grammy'
import fs from 'fs'
import { fetch_tmp_file } from './handlers/common.js'
import { format } from './handlers/telegram/format.js'
// step 0 initial some necessary variables
bot.use(async (ctx, next) => {
// simple i18n
ctx.l = (!ctx.from || !ctx.from.language_code) ? 'en' : ctx.from.language_code
ctx.text = ''
ctx.default_extra = {
parse_mode: 'MarkdownV2'
}
if (!!ctx.message) {
if (ctx.text = ctx.message.text || ctx.message.caption || '') {
// remove command[@username] : /start@Pixiv_bot -> /start
if (ctx.message.entities && ctx.text.startsWith('/')) {
ctx.command = ctx.message.text.substring(1, ctx.message.entities[0].length)
if(ctx.command.includes(`@${bot.botInfo.username}`)){
ctx.command = ctx.command.replace(`@${bot.botInfo.username}`, '')
ctx.text = ctx.text.replace(`@${bot.botInfo.username}`, '')
}
}
}
ctx.default_extra.reply_to_message_id = ctx.message.message_id
ctx.default_extra.allow_sending_without_reply = true
if (!!ctx.update.channel_post) {
ctx.chat_id = ctx.channelPost.chat.id
// channel post is anonymous
ctx.user_id = 1087968824
} else {
ctx.chat_id = ctx.message.chat.id
ctx.user_id = ctx.from.id
}
} else if (!!ctx.inlineQuery) {
ctx.text = ctx.inlineQuery.query
ctx.chat_id = ctx.inlineQuery.from.id
ctx.user_id = ctx.inlineQuery.from.id
} else if (!!ctx.callbackQuery) {
ctx.chat_id = ctx.callbackQuery.message.chat.id
ctx.user_id = ctx.callbackQuery.from.id
}
return await next()
})
bot.command('start', async (ctx, next) => {
// match = deeplink
// see more https://core.telegram.org/bots#deep-linking
if (!ctx.match.trim() || ctx.match === 's') {
// reply start help command
await bot.api.sendMessage(ctx.chat.id, _l(ctx.l, 'start'), {
...ctx.default_extra
})
} else {
// callback to next bot.on handler
return await next()
}
})
bot.command('help', async (ctx) => {
await bot.api.sendMessage(ctx.chat.id, 'https://pixiv-bot.pages.dev', {
...ctx.default_extra,
parse_mode: ''
})
})
bot.command('privacy', async (ctx) => {
await bot.api.sendMessage(ctx.chat.id, 'https://pixiv-bot.pages.dev/privacy', {
...ctx.default_extra,
parse_mode: ''
})
})
// gift: get id even if channel
bot.command('id', async (ctx) => {
let text = ctx.chat.id < 0 ? `#chatid: \`${ctx.chat.id}\`\n` : ''
// channel post maybe didn't have .from
text += ctx.from ? `#userid: \`${ctx.from.id}\`` : ''
await bot.api.sendMessage(ctx.chat.id, text, {
...ctx.default_extra,
parse_mode: 'Markdown'
})
})
// step1 initial config
bot.use(async (ctx, next) => {
if ((ctx.command === 's' || ctx.text.substring(0, 3) === 'eyJ') ||
(ctx.message && ctx.message.reply_to_message && ctx.message.reply_to_message.from && ctx.message.reply_to_message.from.id === bot.botInfo.id &&
ctx.message.reply_to_message.text && ctx.message.reply_to_message.text.substring(0, 5) === '#link')) {
} else {
ctx.ids = get_pixiv_ids(ctx.text)
if (!ctx.callbackQuery && !ctx.inlineQuery
&& JSON.stringify(ctx.ids).length === 36 // have horrible bug in the feature LOL.
&& !['link'].includes(ctx.command)
&& !ctx.text.includes('fanbox.cc')) {
// bot have nothing to do.
return
}
}
// read configuration
ctx.us = await read_user_setting(bot, ctx)
honsole.dev('input ->', ctx.chat, ctx.text, ctx.us)
if (ctx.us === 'error') {
honsole.warn('Get user setting error', ctx.text)
return
} else {
return await next()
}
})
bot.on('callback_query', async (ctx) => {
let chat_id = ctx.chat_id
let message_id = ctx.callbackQuery.message.message_id
let user_id = ctx.user_id
let stext = ctx.callbackQuery.data.split('|')
let linked_chat_id = parseInt(stext[2])
let apply_flag = false
// let action = stext[0].replace('_','∏').split('∏')
if (stext[0] === 'l') {
if ((chat_id > 0 || await is_chat_admin(chat_id, user_id)) && await is_chat_admin(linked_chat_id, user_id)) {
if (stext[1] === 'link_unlink') {
await update_setting({
del_link_chat: {
chat_id: linked_chat_id
}
}, chat_id)
await bot.api.editMessageText(chat_id, message_id, false, _l(ctx.l, 'link_unlink_done'), {
reply_markup: {}
})
apply_flag = true
} else {
try {
let link_setting = {
chat_id: stext[2],
...ctx.us.setting.link_chat_list[stext[2]]
}
link_setting[stext[1].replace('link_', '')] = stext[4]
await update_setting({
add_link_chat: link_setting
}, chat_id)
await ctx.editMessageReplyMarkup(k_link_setting(ctx.l, link_setting))
apply_flag = true
}
catch (error) {
console.warn(error)
}
}
} else {
await ctx.answerCallbackQuery(reescape_strings(_l(ctx.l, 'error_not_a_gc_administrator')), {
show_alert: true
}).catch(e=>{})
return
}
}
if (apply_flag) {
await ctx.answerCallbackQuery(reescape_strings(_l(ctx.l, 'saved'))).catch(e=>{})
} else {
await ctx.answerCallbackQuery(reescape_strings(_l(ctx.l, 'error'))).catch(e=>{})
}
})
bot.command('link', async (ctx) => {
// link this chat to another chat / channel
let chat_id = ctx.message.chat.id
let user_id = ctx.from.id
if (ctx.from.id === 1087968824) {
await bot.api.sendMessage(chat_id, _l(ctx.l, 'error_anonymous'), ctx.default_extra)
} else {
if (chat_id > 0 || await is_chat_admin(chat_id, user_id)) {
// if (ctx.us.setting.link_chat_list && JSON.stringify(ctx.us.setting.link_chat_list).length > 2) {
let new_flag = true
if (ctx.us.setting.link_chat_list) {
for (const linked_chat_id in ctx.us.setting.link_chat_list) {
// support muilt linked chat
// It's hard think permission
// So only link 1
if (await is_chat_admin(linked_chat_id, user_id)) {
await bot.api.sendMessage(chat_id, _l(ctx.l, 'link_setting'), {
...ctx.default_extra,
...k_link_setting(ctx.l, {
chat_id: linked_chat_id,
...ctx.us.setting.link_chat_list[linked_chat_id]
})
})
} else {
await bot.api.sendMessage(chat_id, _l(ctx.l, 'error_not_a_gc_administrator'), ctx.default_extra)
}
new_flag = false
}
}
if (new_flag) {
await bot.api.sendMessage(chat_id, '\\#link ' + _l(ctx.l, 'link_start'), {
...ctx.default_extra,
reply_markup: {
force_reply: true,
selective: true
}
})
}
} else {
await bot.api.sendMessage(chat_id, _l(ctx.l, 'error_not_a_gc_administrator'), ctx.default_extra)
}
}
})
bot.on([':text', ':caption'], async (ctx) => {
let chat_id = ctx.chat_id
let user_id = ctx.user_id
if (ctx.command === 's' || ctx.text.substring(0, 3) === 'eyJ') {
await handle_new_configuration(bot, ctx, ctx.default_extra)
return
}
// @link
if (ctx.message && ctx.message.reply_to_message && ctx.message.reply_to_message.text && ctx.message.reply_to_message.text.substring(0, 5) === '#link') {
if (ctx.from.id === 1087968824) {
await bot.api.sendMessage(ctx.chat.id, _l(ctx.l, 'error_anonymous'), ctx.default_extra)
}
if ((ctx.chat.id > 0 || await is_chat_admin(ctx.chat.id, ctx.from.id)) && await is_chat_admin(ctx.text, ctx.from.id)) {
let linked_chat = await bot.api.getChat(ctx.text)
let default_linked_setting = {
chat_id: linked_chat.id,
type: linked_chat.type,
sync: 0,
administrator_only: 0,
repeat: 0
}
// if(
await update_setting({
add_link_chat: default_linked_setting
}, ctx.chat.id)
await bot.api.sendMessage(ctx.chat.id, _l(ctx.l, 'link_done', linked_chat.title, linked_chat.id) + _l(ctx.l, 'link_setting'), {
...ctx.default_extra,
...k_link_setting(ctx.l, default_linked_setting)
})
} else {
await bot.api.sendMessage(ctx.chat.id, _l(ctx.l, 'error_not_a_gc_administrator'), ctx.default_extra)
}
return
}
if (chat_id > 0) {
(async ()=>{
await ctx.react('👀').catch(e=>{})
setTimeout(async () => {
await ctx.api.setMessageReaction(chat_id, ctx.message.message_id, []).catch(e=>{})
}, 5000)
})()
}
let direct_flag = (ctx.message.caption && !ctx.us.caption_extraction) ? false : true
for (const linked_chat_id in ctx.us.setting.link_chat_list) {
let link_setting = ctx.us.setting.link_chat_list[linked_chat_id]
if (ctx.message.sender_chat && ctx.message.sender_chat.id === linked_chat_id) {
direct_flag = false
// sync mode
} else if ((ctx.type !== 'channel') && (chat_id > 0 || link_setting.sync === 0 || (link_setting.sync === 1 && ctx.message.text.includes('@' + bot.botInfo.username)))) {
// admin only
if (chat_id > 0 || link_setting.administrator_only === 0 || (link_setting.administrator_only === 1 && await is_chat_admin(chat_id, user_id))) {
let new_ctx = {
...ctx,
chat_id: linked_chat_id,
user_id: user_id,
default_extra: {
parse_mode: 'MarkdownV2'
},
type: link_setting.type
}
delete new_ctx.us
await tg_sender(new_ctx)
if (link_setting.repeat < 2) {
direct_flag = false
if (link_setting.repeat === 1) {
// feature request:
// return message id
await ctx.reply(_l(ctx.l, 'sent'), {
...ctx.default_extra,
reply_to_message_id: ctx.message.message_id
})
}
}
}
}
}
if (direct_flag) {
await tg_sender(ctx)
}
return
})
/**
* build ctx object can send illust / novel manually (subscribe / auto push)
* @param {*} ctx
*/
export async function tg_sender(ctx) {
let chat_id = ctx.chat_id || ctx.message.chat.id
let user_id = ctx.user_id || ctx.from.id
let text = ctx.text || ''
let default_extra = ctx.default_extra
if (!ctx.us) {
ctx.us = await read_user_setting(bot, ctx)
}
default_extra.show_caption_above_media = ctx.us.caption_above
let ids = ctx.ids
let illusts = []
// fetch authors' all illusts
// alpha version (owner only)
if (ids.author.length > 0) {
if (user_id === config.tg.master_id) {
(async ()=>{
await bot.api.sendChatAction(chat_id, 'typing').catch(e=>{})
})();
await asyncForEach(ids.author, async (id) => {
illusts = [...illusts, ...await get_user_illusts(id)]
});
}
}
if (ids.illust.length > 0) {
await asyncForEach(ids.illust, async (id) => {
let d = await handle_illust(id, ctx.us)
if (d) {
if (d === 404) {
if (chat_id > 0) {
await bot.api.sendMessage(chat_id, _l(ctx.l, 'illust_404'), default_extra)
return
}
} else {
illusts.push(d)
}
}
})
}
if (illusts.length > 0) {
let mgs = []
let files = []
await asyncForEach(ctx.us.desc ? illusts.reverse() : illusts, async (illust) => {
// telegraph
ctx.us.q_id += 1
let mg = illust.mediagroup
// send as file
if ((!ctx.us.asfile && !ctx.us.telegraph) &&
(
!ctx.us.album ||
(illusts.length == 1 && mg.length === 1) ||
(!ctx.us.album_one && mg.length === 1)
)) {
// see https://core.telegram.org/bots/api#inlinekeyboardbutton
// Especially useful when combined with switch_pm… actions – in this case the user will be automatically returned to the chat they switched from, skipping the chat selection screen.
// So we need inline share button to switch chat window even if user don't want share button
if (illust.type === 2 && ctx.match) {
ctx.us.share = true
}
let extra = {
...default_extra,
...k_os(illust.id, ctx.us)
}
if (ctx.us.spoiler) {
extra.has_spoiler = ctx.us.spoiler
}
if (ctx.us.caption_above) {
extra.show_caption_above_media = ctx.us.caption_above
}
if (illust.type <= 1) {
let { reply_to_message_id } = extra
await asyncForEach(illust.mediagroup, async (o, i) => {
let photo_urls = [o.media_r, `dl-${o.media_r}`]
let extra_one = {
...extra,
reply_to_message_id,
caption: ctx.us.single_caption ? format(illust, {
...ctx.us,
single_caption: false
}, 'message', i) : o.caption
}
let result = null
if (!ctx.us.asfile){
result = await sendPhotoWithRetry(chat_id, ctx.l, photo_urls, extra_one)
reply_to_message_id = result.message_id
extra_one.reply_to_message_id = result.message_id
}
if (ctx.us.asfile || ctx.us.append_file_immediate) {
delete extra_one.reply_markup
await sendDocumentWithRetry(chat_id, o.media_o, extra_one, ctx.l)
}
if (ctx.us.append_file && !ctx.us.append_file_immediate) {
delete extra_one.reply_markup
files.push([chat_id, o.media_o, extra_one, ctx.l])
}
})
} else if (illust.type === 2) {
(async ()=>{
await bot.api.sendChatAction(chat_id, 'upload_video').catch(e=>{})
})();
let media = mg[0].media_t
if (!media) {
if (mg[0].media_o) {
media = mg[0].media_o
} else {
media = await ugoira_to_mp4(illust)
}
}
if (media.includes('tmp/')) {
media = new InputFile(media)
}
let result = null
if (!ctx.us.asfile){
result = await bot.api.sendAnimation(chat_id, media, extra).catch(async (e) => {
if (await catchily(e, chat_id, ctx.l)) {
bot.api.sendMessage(chat_id, _l(ctx.l, 'error'), default_extra)
}
})
// save ugoira file_id and next time bot can reply without send file
if (!illust.tg_file_id && result.document) {
let col = db.collection.illust
await col.updateOne({
id: illust.id
}, {
$set: {
tg_file_id: result.document.file_id
}
})
}
if (result) {
extra.reply_to_message_id = result.message_id
}
}
if (ctx.us.asfile || ctx.us.append_file_immediate) {
delete extra.reply_markup
await sendDocumentWithRetry(chat_id, media, extra, ctx.l)
}
if (ctx.us.append_file && !ctx.us.append_file_immediate) {
delete extra.reply_markup
files.push([chat_id, media, extra, ctx.l])
}
}
} else {
// handle mediagroup
if (ctx.us.telegraph || ctx.us.album_one) {
if (mgs.length === 0) {
mgs.push([])
}
mgs[0] = [...mgs[0], ...mg]
} else {
mgs = [...mgs, ...mg]
}
}
})
let mg_extra = { }
if (mgs.length > 0) {
if (ctx.us.telegraph) {
// when not have title provided and 1 illust only
if(!ctx.us.telegraph_title && illusts.length === 1) {
ctx.us.telegraph_title = illusts[0].title
if(!ctx.us.telegraph_author_name){
ctx.us.telegraph_author_name = illusts[0].author_name
ctx.us.telegraph_author_url = `https://www.pixiv.net/artworks/${illusts[0].id}`
}
}
try {
(async ()=>{
await bot.api.sendChatAction(chat_id, 'typing').catch(e=>{})
})();
let res_data = await mg2telegraph(mgs[0], ctx.us.telegraph_title, user_id, ctx.us.telegraph_author_name, ctx.us.telegraph_author_url)
if (res_data) {
await asyncForEach(res_data, async (d) => {
await bot.api.sendMessage(chat_id, d.ids.join('\n') + '\n' + d.telegraph_url)
})
await bot.api.sendMessage(chat_id, _l(ctx.l, 'telegraph_iv'), default_extra)
}
} catch (error) {
console.warn(error)
}
} else {
if(ctx.us.album_one){
mgs = mgs[0]
}
// Bad Request: document can't be mixed with other media types
// if (ctx.us.append_file_immediate) {
// const mgs_f = mgs.map(mg => {
// return {
// ...mg,
// type: 'document'
// }
// })
// mgs = mgs.flatMap((value, index) => [value, mgs_f[index]])
// }
await asyncForEach(mg_albumize(mgs, ctx.us), async (mg, i) => {
let result = await sendMediaGroupWithRetry(chat_id, ctx.l, mg, mg_extra, ['r', 'o', 'dlr', 'dlo'])
if (result) {
if (result[0] && result[0].message_id) {
mg_extra.reply_to_message_id = result[0].message_id
} else {
delete mg_extra.reply_to_message_id
}
} else {
honsole.warn('error send mg', result)
// await bot.api.sendMessage(chat_id, _l(ctx.l, 'error'), default_extra)
}
// Too Many Requests: retry after 10
if (i > 4) {
await sleep(3500)
} else {
await sleep(1500)
}
if (ctx.us.append_file_immediate) {
let result = await sendMediaGroupWithRetry(chat_id, ctx.l, mg.map(mg=>{
return {
...mg,
type: 'document'
}
}), mg_extra, ['o', 'dlo'])
if (result) {
if (result[0] && result[0].message_id) {
mg_extra.reply_to_message_id = result[0].message_id
} else {
delete mg_extra.reply_to_message_id
}
} else {
honsole.warn('error send mg', result)
// await bot.api.sendMessage(chat_id, _l(ctx.l, 'error'), default_extra)
}
// Too Many Requests: retry after 10
if (i > 4) {
await sleep(3500)
} else {
await sleep(1500)
}
}
})
if(ctx.us.asfile || (ctx.us.append_file && !ctx.us.append_file_immediate)) {
await asyncForEach(mg_albumize(mgs, ctx.us), async (mg, i) => {
let result = await sendMediaGroupWithRetry(chat_id, ctx.l, mg.map(mg=>{
return {
...mg,
type: 'document'
}
}), mg_extra, ['o', 'dlo'])
if (result) {
if (result[0] && result[0].message_id) {
mg_extra.reply_to_message_id = result[0].message_id
} else {
delete mg_extra.reply_to_message_id
}
} else {
honsole.warn('error send mg', result)
// await bot.api.sendMessage(chat_id, _l(ctx.l, 'error'), default_extra)
}
// Too Many Requests: retry after 10
if (i > 4) {
await sleep(3500)
} else {
await sleep(1500)
}
})
}
}
}
await asyncForEach(files, async (f, i)=>{
await sendDocumentWithRetry(...f)
})
}
if (ids.novel.length > 0) {
await asyncForEach(ids.novel, async (id) => {
(async ()=>{
await bot.api.sendChatAction(chat_id, 'typing').catch(e=>{})
})();
let d = await handle_novel(id)
if (d) {
await bot.api.sendMessage(chat_id, `${d.telegraph_url}`)
} else {
await bot.api.sendMessage(chat_id, _l(ctx.l, 'illust_404'), default_extra)
}
})
}
if (text.includes('fanbox.cc/') && chat_id > 0) {
await bot.api.sendMessage(chat_id, _l(ctx.l, 'fanbox_not_support'), default_extra)
}
return true
}
bot.on('inline_query', async (ctx) => {
let res = []
let offset = ctx.inlineQuery.offset
if (!offset) {
offset = 0; // offset == empty -> offset = 0
}
let query = ctx.text
// offset = page
offset = parseInt(offset)
let res_options = {
cache_time: 20,
is_personal: ctx.us.setting.dbless ? false : true // personal result
}
let ids = ctx.ids
if (ids.illust.length > 0) {
ids.illust = [...new Set(ids.illust)]
await asyncForEach([...ids.illust.reverse()], async (id) => {
let d = await handle_illust(id, ctx.us)
if (!d || d === 404) {
return
}
// There is no enough time to convert ugoira, so need switch_pm to bot's chat window convert
if (d.type === 2 && d.inline.length === 0) {
// pre convert (without await)
ugoira_to_mp4(d.id)
await ctx.answerInlineQuery([], {
switch_pm_text: _l(ctx.l, 'pm_to_generate_ugoira'),
switch_pm_parameter: ids.illust.join('-_-').toString(),
cache_time: 0
}).catch(async (e) => {
await catchily(e, chat_id, ctx.l)
})
return true
}
res = d.inline.concat(res)
})
if (res.splice((offset + 1) * 20 - 1, 20)) {
res_options.next_offset = offset + 1
}
res = res.splice(offset * 20, 20)
// ids.illust > 8 -> .length > 64 = cant send /start with deeplink
// lazy... I would like to store ids in database or redis
if (res.length > 1 && ids.illust.length < 8) {
res_options.switch_pm_text = _l(ctx.l, 'pm_to_get_all_illusts')
res_options.switch_pm_parameter = ids.illust.join('-')
}
} else if (query.replaceAll(' ', '') === '') { // why not use .trim() ? LOL
let data = await handle_ranking([offset], ctx.us)
res = data.data
if (data.next_offset) {
res_options.next_offset = data.next_offset
}
}
await ctx.answerInlineQuery(res, res_options).catch(async (e) => {
await catchily(e, config.tg.master_id, ctx.l)
})
})
bot.catch(async (e) => {
honsole.warn('gg', e)
bot.api.sendMessage(config.tg.master_id, e, {
disable_web_page_preview: true
})
})
db.db_initial().then(async () => {
if (!process.env.DEPENDIONLESS && !process.env.dev) {
try {
await exec('which ffmpeg')
await exec('which mp4fpsmod')
await exec('which unzip')
}
catch (error) {
console.error('You must install ffmpeg, mp4fpsmod and unzip to active ugoira to mp4 function', error)
console.error('If you want to run bot but won\'t install these above, please exec following command:')
console.error('DEPENDIONLESS=1 node app.js')
console.log('bye')
process.exit()
}
}
if(process.argv[1].includes('cron')) {
return
}
bot.init().then(async () => {
grammyjsRun(bot)
console.log(new Date(), `bot @${bot.botInfo.username} started!`)
bot.api.sendMessage(config.tg.master_id, `${new Date().toString()} bot started!`)
}).catch((e) => {
console.error('You are offline or bad bot token', e)
process.exit()
})
if (config.web.enabled && !process.env.WEBLESS) {
import('./web.js')
}
})
/**
* catch error report && reply
* @param {*} e error
* @param {*} ctx ctx
*/
async function catchily(e, chat_id, language_code = 'en') {
let default_extra = {
parse_mode: 'MarkdownV2'
}
honsole.warn(e)
try {
bot.api.sendMessage(config.tg.master_id, JSON.stringify(e).substring(0, 1000), {
disable_web_page_preview: true
}).catch(e=>{})
if (!e.ok) {
const description = e.description.toLowerCase()
if (description.includes('media_caption_too_long')) {
await bot.api.sendMessage(chat_id, _l(language_code, 'error_text_too_long'), default_extra).catch(e=>{})
return false
} else if (description.includes('can\'t parse entities: character')) {
await bot.api.sendMessage(chat_id, _l(language_code, 'error_format', e.description)).catch(e=>{})
return false
// banned by user
} else if (description.includes('forbidden:')) {
return false
// not have permission
} else if (description.includes('not enough rights to send')) {
await bot.api.sendMessage(chat_id, _l(language_code, 'error_not_enough_rights'), default_extra).catch(e=>{})
return false
// just a moment
} else if (description.includes('too many requests')) {
console.log(chat_id, 'sleep', e.description.parameters.retry_after, 's')
// await sleep(e.description.parameters.retry_after * 1000)
return 'redo'
} else if (description.includes('failed to get http url content') || description.includes('wrong file identifier/http url specified') || description.includes('wrong type of the web page content') || description.includes('group send failed')) {
let photo_urls = []
if (e.method === 'sendPhoto') {
photo_urls[0] = e.payload.photo
} else if (e.method === 'sendMediaGroup' && e.payload.media) {
photo_urls = e.payload.media.filter(m => {
return m.media && typeof m.media === 'string' && m.media.includes('https://')
}).map(m => {
return m.media
})
} else if(e.method === 'sendDocument') {
photo_urls[0] = e.payload.document
}
honsole.dev(photo_urls)
if (config.tg.refetch_api && photo_urls) {
(async ()=>{
try {
await axios.post(config.tg.refetch_api, {
url: photo_urls.join('\n')
})
honsole.log('[ok] fetch new url(s)', photo_urls)
} catch (error) {
honsole.warn('[err] fetch new url(s)', error)
}
})()
}
}
}
} catch (error) {
console.warn(error)
return false
}
return true
}
/**
* send mediagroup with retry
* @param {*} chat_id
* @param {*} mg
* @param {*} extra
* @param {*} mg_type
* @returns
*/
async function sendMediaGroupWithRetry(chat_id, language_code, mg, extra, mg_type = []) {
if (mg_type.length === 0) {
honsole.warn('empty mg', chat_id, mg)
return false
}
let current_mg_type = mg_type.shift();
(async ()=>{
await bot.api.sendChatAction(chat_id, mg[0].type === 'document' ? 'upload_document' : 'upload_photo').catch(e=>{})
})();
try {
return await bot.api.sendMediaGroup(chat_id, await mg_filter([...mg], current_mg_type), extra)
} catch (e) {
let status = await catchily(e, chat_id, language_code)
// Bad Request: failed to send message #1 with the error message "WEBPAGE_MEDIA_EMPTY"
// Bad Request: failed to send message #2 with the error message "WEBPAGE_CURL_FAILED"
// have a few bugs
// if (e.description && e.description.includes('failed to send message') && e.description.includes('#')) {
// status = 'redo'
// let mg_index = e.description.split(' ').find(x=>{
// return x.startsWith('#')
// })
// if (mg_index) {
// mg_index = parseInt(mg_index.substring(1)) - 1
// if (!mg[mg_index].invaild) {
// mg[mg_index].invaild = [current_mg_type]
// }else {
// mg[mg_index].invaild.push(current_mg_type)
// }
// }
// }
if (status) {
if (status === 'redo') {
mg_type.unshift(current_mg_type)
}
return await sendMediaGroupWithRetry(chat_id, language_code, mg, extra, mg_type)
} else {
honsole.warn('error send mg', chat_id, mg)
return false
}
}
}
/**
* send photo with retry
* @param {*} chat_id
* @param {*} mg
* @param {*} extra
* @param {*} mg_type
* @returns
*/
async function sendPhotoWithRetry(chat_id, language_code, photo_urls = [], extra) {
if (photo_urls.length === 0) {
honsole.warn('error send photo', chat_id, photo_urls)
return false
}
(async ()=>{
await bot.api.sendChatAction(chat_id, 'upload_photo').catch(e=>{})
})();
let raw_photo_url = photo_urls.shift()
let photo_url = raw_photo_url
try {
if (photo_url.substring(0, 3) === 'dl-') {
photo_url = new InputFile(await fetch_tmp_file(photo_url.substring(3)))
}
return await bot.api.sendPhoto(chat_id, photo_url, extra)
} catch (e) {
const status = await catchily(e, chat_id, language_code)
if (status) {
if (status === 'redo') {
photo_urls.unshift(raw_photo_url)
}
return await sendPhotoWithRetry(chat_id, language_code, photo_urls, extra)
} else {
honsole.warn('error send photo', chat_id, photo_urls)
return false
}
}
}
/**
* sendDocumentWithRetry
* @param {*} chat_id
* @param {*} media_o
* @param {*} extra
* @param {*} l
*/
async function sendDocumentWithRetry(chat_id, media_o, extra, l) {
(async ()=>{
await bot.api.sendChatAction(chat_id, 'upload_document').catch(e=>{})
})();
let reply_to_message_id = null
extra = {
...extra,
disable_content_type_detection: true
}
await bot.api.sendDocument(chat_id, media_o, extra).then(x => {
reply_to_message_id = x.message_id
}).catch(async (e) => {
if (await catchily(e, chat_id, l)) {
await bot.api.sendDocument(chat_id, new InputFile(await fetch_tmp_file(media_o)), extra).then(x => {
reply_to_message_id = x.message_id
}).catch(async (e) => {
await bot.api.sendMessage(chat_id, _l(l, 'file_too_large', media_o.replace('i.pximg.net', config.pixiv.pximgproxy)), extra).then(x => {
reply_to_message_id = x.message_id
})
})
}
})
return reply_to_message_id
}
/**
* when user is chat's administrator / creator, return true
* @param {*} chat_id
* @param {*} user_id
* @returns Boolean
*/
async function is_chat_admin(chat_id, user_id) {
try {
let { status } = await bot.api.getChatMember(chat_id, user_id)
if (status === 'administrator' || status === 'creator') {
return true
}
}
catch (e) {
}
return false
}