File size: 27,991 Bytes
29b5c59
1a1e2eb
 
7ff1595
9a92ff5
 
 
1fa270a
2fe1440
 
 
 
 
 
 
 
 
 
9a92ff5
1a1e2eb
8359204
 
 
 
 
 
 
 
 
 
 
1a1e2eb
9a92ff5
8359204
 
 
 
 
1a1e2eb
3a67739
1a1e2eb
1fa270a
 
 
 
 
9a92ff5
3a67739
 
1fa270a
 
 
 
 
d3ac0b2
3a67739
 
8359204
3a67739
 
 
 
 
1a1e2eb
8359204
 
 
 
 
 
 
 
 
 
2fe1440
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8359204
 
 
 
1fa270a
 
 
 
 
8359204
 
 
 
 
 
 
 
 
 
1fa270a
 
 
 
 
8359204
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1fa270a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8359204
 
 
 
d3ac0b2
1fa270a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d3ac0b2
8359204
1a1e2eb
3a67739
8359204
 
 
1a1e2eb
8359204
 
1fa270a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8359204
 
 
 
 
 
 
 
 
 
 
1a1e2eb
8359204
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1fa270a
 
 
 
 
8359204
 
 
 
 
 
 
 
1a1e2eb
 
3a67739
 
 
 
1a1e2eb
 
3a67739
8359204
 
 
 
 
 
 
 
 
 
1fa270a
 
 
 
 
8359204
 
 
 
1a1e2eb
 
3a67739
 
 
 
1a1e2eb
 
3a67739
8359204
 
3a67739
8359204
 
 
 
 
 
 
1fa270a
 
 
 
 
8359204
 
 
 
 
 
 
 
 
2fe1440
 
89e33ed
 
 
 
2fe1440
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
89e33ed
 
 
2fe1440
 
89e33ed
1fa270a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2fe1440
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8359204
 
1fa270a
 
 
 
 
8359204
 
 
 
 
 
7ff1595
8359204
3a67739
8359204
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
89e33ed
2fe1440
 
89e33ed
 
 
 
2fe1440
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1b3eebd
 
 
8359204
 
 
 
 
 
 
746a920
8359204
 
1b3eebd
 
 
d3ac0b2
7f1478e
8359204
3a67739
 
 
 
 
 
 
 
2fe1440
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3a67739
 
7ff1595
3a67739
8359204
7ff1595
 
 
 
 
3a67739
 
8359204
 
 
 
 
 
 
 
 
 
3a67739
 
 
8359204
 
3a67739
29b5c59
1fa270a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3a67739
 
 
 
1fa270a
 
 
 
 
9a92ff5
 
29b5c59
d3ac0b2
3a67739
 
 
 
 
 
 
 
 
 
 
8359204
3a67739
1fa270a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3a67739
 
 
8359204
 
e7261d3
1fa270a
 
 
 
 
e7261d3
1fa270a
 
 
 
 
 
 
e7261d3
1fa270a
3a67739
 
1fa270a
 
 
 
 
e7261d3
1fa270a
 
 
 
 
 
 
e7261d3
1fa270a
3a67739
d3ac0b2
3a67739
8359204
 
1fa270a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3a67739
8359204
 
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
# Thunder/bot/plugins/stream.py

import asyncio
import secrets
from typing import Any, Dict, Optional

from pyrogram import Client, enums, filters
from pyrogram.errors import FloodWait, MessageNotModified, MessageDeleteForbidden, MessageIdInvalid
from pyrogram.types import (InlineKeyboardButton, InlineKeyboardMarkup,
                            Message)

from Thunder.bot import StreamBot
from Thunder.utils.bot_utils import (gen_canonical_links, gen_links, is_admin,
                                     log_newusr, notify_own, reply_user_err)
from Thunder.utils.canonical_files import get_or_create_canonical_file
from Thunder.utils.database import db
from Thunder.utils.decorators import (check_banned, get_shortener_status,
                                      require_token)
from Thunder.utils.force_channel import force_channel_check
from Thunder.utils.logger import logger
from Thunder.utils.messages import (
    MSG_BATCH_LINKS_READY, MSG_BUTTON_DOWNLOAD, MSG_BUTTON_START_CHAT,
    MSG_BUTTON_STREAM_NOW, MSG_CRITICAL_ERROR, MSG_DM_BATCH_PREFIX,
    MSG_DM_SINGLE_PREFIX, MSG_ERROR_DM_FAILED, MSG_ERROR_INVALID_NUMBER,
    MSG_ERROR_NO_FILE, MSG_ERROR_NOT_ADMIN, MSG_ERROR_NUMBER_RANGE,
    MSG_ERROR_PROCESSING_MEDIA, MSG_ERROR_REPLY_FILE, MSG_ERROR_START_BOT,
    MSG_LINKS, MSG_NEW_FILE_REQUEST, MSG_PROCESSING_BATCH,
    MSG_PROCESSING_FILE, MSG_PROCESSING_REQUEST, MSG_PROCESSING_RESULT,
    MSG_PROCESSING_STATUS
)
from Thunder.utils.rate_limiter import handle_rate_limited_request
from Thunder.vars import Var

BATCH_SIZE = 10
LINK_CHUNK_SIZE = 20
BATCH_UPDATE_INTERVAL = 5
MESSAGE_DELAY = 0.5


async def fwd_media(m_msg: Message) -> Optional[Message]:
    try:
        try:
            return await m_msg.copy(chat_id=Var.BIN_CHANNEL)
        except FloodWait as e:
            await asyncio.sleep(e.value)
            return await m_msg.copy(chat_id=Var.BIN_CHANNEL)
    except Exception as e:
        if "MEDIA_CAPTION_TOO_LONG" in str(e):
            logger.debug(f"MEDIA_CAPTION_TOO_LONG error, retrying without caption: {e}")
            try:
                return await m_msg.copy(chat_id=Var.BIN_CHANNEL, caption=None)
            except FloodWait as e:
                await asyncio.sleep(e.value)
                return await m_msg.copy(chat_id=Var.BIN_CHANNEL, caption=None)
        logger.error(f"Error fwd_media copy: {e}", exc_info=True)
        return None


def get_link_buttons(links):
    return InlineKeyboardMarkup([[
        InlineKeyboardButton(MSG_BUTTON_STREAM_NOW, url=links['stream_link']),
        InlineKeyboardButton(MSG_BUTTON_DOWNLOAD, url=links['online_link'])
    ]])

async def validate_request_common(client: Client, message: Message) -> Optional[bool]:
    if not await check_banned(client, message):
        return None
    if not await require_token(client, message):
        return None
    if not await force_channel_check(client, message):
        return None
    return await get_shortener_status(client, message)


async def send_channel_links(
    links: Dict[str, Any],
    source_info: str,
    source_id: int,
    *,
    target_msg: Optional[Message] = None,
    reply_to_message_id: Optional[int] = None
):
    try:
        text = MSG_NEW_FILE_REQUEST.format(
            source_info=source_info,
            id_=source_id,
            online_link=links['online_link'],
            stream_link=links['stream_link']
        )
        if target_msg:
            await target_msg.reply_text(
                text,
                disable_web_page_preview=True,
                quote=True
            )
        else:
            await StreamBot.send_message(
                chat_id=Var.BIN_CHANNEL,
                text=text,
                disable_web_page_preview=True,
                reply_to_message_id=reply_to_message_id
            )
    except FloodWait as e:
        await asyncio.sleep(e.value)
        text = MSG_NEW_FILE_REQUEST.format(
            source_info=source_info,
            id_=source_id,
            online_link=links['online_link'],
            stream_link=links['stream_link']
        )
        if target_msg:
            await target_msg.reply_text(
                text,
                disable_web_page_preview=True,
                quote=True
            )
        else:
            await StreamBot.send_message(
                chat_id=Var.BIN_CHANNEL,
                text=text,
                disable_web_page_preview=True,
                reply_to_message_id=reply_to_message_id
            )


async def safe_edit_message(message: Message, text: str, **kwargs):
    try:
        try:
            return await message.edit_text(text, **kwargs)
        except FloodWait as e:
            await asyncio.sleep(e.value)
            return await message.edit_text(text, **kwargs)
    except MessageNotModified:
        pass
    except MessageDeleteForbidden:
        logger.debug(f"Failed to edit message {message.id} due to permissions.")
    except Exception as e:
        logger.error(f"Error editing message {message.id}: {e}", exc_info=True)


async def safe_delete_message(message: Message):
    try:
        try:
            await message.delete()
        except FloodWait as e:
            await asyncio.sleep(e.value)
            await message.delete()
    except MessageDeleteForbidden:
        logger.debug(f"Failed to delete message {message.id} due to permissions.")
    except Exception as e:
        logger.error(f"Error deleting message {message.id}: {e}", exc_info=True)


async def send_dm_links(bot: Client, user_id: int, links: Dict[str, Any], chat_title: str):
    try:
        dm_text = MSG_DM_SINGLE_PREFIX.format(chat_title=chat_title) + "\n" + \
                  MSG_LINKS.format(
                      file_name=links['media_name'],
                      file_size=links['media_size'],
                      download_link=links['online_link'],
                      stream_link=links['stream_link']
                  )
        try:
            await bot.send_message(
                chat_id=user_id,
                text=dm_text,
                disable_web_page_preview=True,
                parse_mode=enums.ParseMode.MARKDOWN,
                reply_markup=get_link_buttons(links)
            )
        except FloodWait as e:
            await asyncio.sleep(e.value)
            await bot.send_message(
                chat_id=user_id,
                text=dm_text,
                disable_web_page_preview=True,
                parse_mode=enums.ParseMode.MARKDOWN,
                reply_markup=get_link_buttons(links)
            )
    except Exception as e:
        logger.error(f"Error sending DM to user {user_id}: {e}", exc_info=True)


async def send_link(msg: Message, links: Dict[str, Any]):
    try:
        await msg.reply_text(
            MSG_LINKS.format(
                file_name=links['media_name'],
                file_size=links['media_size'],
                download_link=links['online_link'],
                stream_link=links['stream_link']
            ),
            quote=True,
            parse_mode=enums.ParseMode.MARKDOWN,
            disable_web_page_preview=True,
            reply_markup=get_link_buttons(links)
        )
    except FloodWait as e:
        await asyncio.sleep(e.value)
        await msg.reply_text(
            MSG_LINKS.format(
                file_name=links['media_name'],
                file_size=links['media_size'],
                download_link=links['online_link'],
                stream_link=links['stream_link']
            ),
            quote=True,
            parse_mode=enums.ParseMode.MARKDOWN,
            disable_web_page_preview=True,
            reply_markup=get_link_buttons(links)
        )


@StreamBot.on_message(filters.command("link") & ~filters.private)
async def link_handler(bot: Client, msg: Message, **kwargs):
    async def _actual_link_handler(client: Client, message: Message, **handler_kwargs):
        shortener_val = await validate_request_common(client, message)
        if shortener_val is None:
            return
        if message.from_user and not await db.is_user_exist(message.from_user.id):
            invite_link = f"https://t.me/{client.me.username}?start=start"
            try:
                await message.reply_text(
                    MSG_ERROR_START_BOT.format(invite_link=invite_link),
                    disable_web_page_preview=True,
                    parse_mode=enums.ParseMode.MARKDOWN,
                    reply_markup=InlineKeyboardMarkup([[InlineKeyboardButton(MSG_BUTTON_START_CHAT, url=invite_link)]]),
                    quote=True
                )
            except FloodWait as e:
                await asyncio.sleep(e.value)
                await message.reply_text(
                    MSG_ERROR_START_BOT.format(invite_link=invite_link),
                    disable_web_page_preview=True,
                    parse_mode=enums.ParseMode.MARKDOWN,
                    reply_markup=InlineKeyboardMarkup([[InlineKeyboardButton(MSG_BUTTON_START_CHAT, url=invite_link)]]),
                    quote=True
                )
            return

        if (message.chat.type in [enums.ChatType.GROUP, enums.ChatType.SUPERGROUP]
                and not await is_admin(client, message.chat.id)):
            await reply_user_err(message, MSG_ERROR_NOT_ADMIN)
            return

        if not message.reply_to_message or not message.reply_to_message.media:
            await reply_user_err(
                message,
                MSG_ERROR_REPLY_FILE if not message.reply_to_message else MSG_ERROR_NO_FILE)
            return

        notification_msg = handler_kwargs.get('notification_msg')

        parts = message.text.split()
        num_files = 1
        if len(parts) > 1:
            try:
                num_files = int(parts[1])
                if not 1 <= num_files <= Var.MAX_BATCH_FILES:
                    await reply_user_err(
                        message,
                        MSG_ERROR_NUMBER_RANGE.format(max_files=Var.MAX_BATCH_FILES))
                    return
            except ValueError:
                await reply_user_err(message, MSG_ERROR_INVALID_NUMBER)
                return

        try:
            status_msg = await message.reply_text(MSG_PROCESSING_REQUEST, quote=True)
        except FloodWait as e:
            await asyncio.sleep(e.value)
            status_msg = await message.reply_text(MSG_PROCESSING_REQUEST, quote=True)
        shortener_val = handler_kwargs.get('shortener', shortener_val)
        if num_files == 1:
            await process_single(client, message, message.reply_to_message, status_msg, shortener_val, notification_msg=notification_msg)
        else:
            await process_batch(client, message, message.reply_to_message.id, num_files, status_msg, shortener_val, notification_msg=notification_msg)

    await handle_rate_limited_request(bot, msg, _actual_link_handler, **kwargs)


@StreamBot.on_message(

    filters.private &

    filters.incoming &

    (filters.document | filters.video | filters.photo | filters.audio |

     filters.voice | filters.animation | filters.video_note),

    group=4

)
async def private_receive_handler(bot: Client, msg: Message, **kwargs):
    async def _actual_private_receive_handler(client: Client, message: Message, **handler_kwargs):
        shortener_val = await validate_request_common(client, message)
        if shortener_val is None:
            return
        if not message.from_user:
            return

        notification_msg = handler_kwargs.get('notification_msg')

        await log_newusr(client, message.from_user.id, message.from_user.first_name or "")
        try:
            status_msg = await message.reply_text(MSG_PROCESSING_FILE, quote=True)
        except FloodWait as e:
            await asyncio.sleep(e.value)
            status_msg = await message.reply_text(MSG_PROCESSING_FILE, quote=True)
        await process_single(client, message, message, status_msg, shortener_val, notification_msg=notification_msg)

    await handle_rate_limited_request(bot, msg, _actual_private_receive_handler, **kwargs)


@StreamBot.on_message(

    filters.channel &

    filters.incoming &

    (filters.document | filters.video | filters.audio) &

    ~filters.chat(Var.BIN_CHANNEL),

    group=-1

)
async def channel_receive_handler(bot: Client, msg: Message):
    async def _actual_channel_receive_handler(client: Client, message: Message, **handler_kwargs):
        if not Var.CHANNEL:
            return
        notification_msg = handler_kwargs.get('notification_msg')

        is_banned_statically = hasattr(Var, 'BANNED_CHANNELS') and message.chat.id in Var.BANNED_CHANNELS
        is_banned_dynamically = await db.is_channel_banned(message.chat.id) is not None

        if is_banned_statically or is_banned_dynamically:
            try:
                try:
                    await client.leave_chat(message.chat.id)
                except FloodWait as e:
                    await asyncio.sleep(e.value)
                    await client.leave_chat(message.chat.id)
            except Exception as e:
                logger.error(f"Error leaving banned channel {message.chat.id}: {e}")
            return
        if not await is_admin(client, message.chat.id):
            logger.debug(
                f"Bot is not admin in channel {message.chat.id} "
                f"({message.chat.title or 'Unknown'}). Ignoring message.")
            return

        try:
            shortener_val = await get_shortener_status(client, message)
            canonical_record, stored_msg, reused_existing = await get_or_create_canonical_file(message, fwd_media)
            if reused_existing and stored_msg:
                await safe_delete_message(stored_msg)
                stored_msg = None
            if canonical_record:
                links = await gen_canonical_links(
                    file_name=canonical_record["file_name"],
                    file_size=int(canonical_record.get("file_size", 0) or 0),
                    public_hash=canonical_record["public_hash"],
                    shortener=shortener_val
                )
                reply_to_message_id = int(canonical_record["canonical_message_id"])
            else:
                if not stored_msg:
                    stored_msg = await fwd_media(message)
                    if not stored_msg:
                        logger.error(
                            f"Failed to forward media from channel {message.chat.id}. Ignoring.")
                        return
                links = await gen_links(stored_msg, shortener=shortener_val)
                reply_to_message_id = stored_msg.id
            source_info = message.chat.title or "Unknown Channel"
            # When we reused an existing canonical BIN copy, stored_msg is intentionally
            # None so send_channel_links falls back to StreamBot.send_message(...,
            # reply_to_message_id=...) and keeps the log threaded to the canonical message.

            if notification_msg:
                try:
                    try:
                        await notification_msg.edit_text(
                            MSG_NEW_FILE_REQUEST.format(
                                source_info=source_info,
                                id_=message.chat.id,
                                online_link=links['online_link'],
                                stream_link=links['stream_link']
                            ),
                            disable_web_page_preview=True
                        )
                    except FloodWait as e:
                        await asyncio.sleep(e.value)
                        await notification_msg.edit_text(
                            MSG_NEW_FILE_REQUEST.format(
                                source_info=source_info,
                                id_=message.chat.id,
                                online_link=links['online_link'],
                                stream_link=links['stream_link']
                            ),
                            disable_web_page_preview=True
                        )
                except Exception as e:
                    logger.error(f"Error editing notification message with links: {e}", exc_info=True)
                    await send_channel_links(
                        links,
                        source_info,
                        message.chat.id,
                        target_msg=stored_msg,
                        reply_to_message_id=reply_to_message_id
                    )
            else:
                await send_channel_links(
                    links,
                    source_info,
                    message.chat.id,
                    target_msg=stored_msg,
                    reply_to_message_id=reply_to_message_id
                )

            try:
                try:
                    await message.edit_reply_markup(reply_markup=get_link_buttons(links))
                except FloodWait as e:
                    await asyncio.sleep(e.value)
                    await message.edit_reply_markup(reply_markup=get_link_buttons(links))
            except (MessageNotModified, MessageDeleteForbidden, MessageIdInvalid):
                logger.debug(f"Failed to edit reply markup for message {message.id} due to not modified, permissions or invalid ID. Sending new link instead.")
                await send_link(message, links)
            except Exception as e:
                logger.error(f"Error editing reply markup for message {message.id}: {e}", exc_info=True)
                await send_link(message, links)
        except Exception as e:
            logger.error(f"Error in _actual_channel_receive_handler for message {message.id}: {e}", exc_info=True)

    rl_user_id = None
    if msg.sender_chat and msg.sender_chat.id:
        rl_user_id = msg.sender_chat.id
    elif msg.from_user:
        rl_user_id = msg.from_user.id
    
    if rl_user_id is None:
        logger.debug(f"No identifiable user/channel for rate limiting for message {msg.id}. Skipping rate limit check and processing directly.")
        await _actual_channel_receive_handler(bot, msg)
        return

    await handle_rate_limited_request(bot, msg, _actual_channel_receive_handler, rl_user_id=rl_user_id)


async def process_single(

    bot: Client,

    msg: Message,

    file_msg: Message,

    status_msg: Message,

    shortener_val: bool,

    original_request_msg: Optional[Message] = None,

    notification_msg: Optional[Message] = None
):
    try:
        canonical_record, stored_msg, reused_existing = await get_or_create_canonical_file(file_msg, fwd_media)
        if reused_existing and stored_msg:
            await safe_delete_message(stored_msg)
            stored_msg = None
        if canonical_record:
            links = await gen_canonical_links(
                file_name=canonical_record["file_name"],
                file_size=int(canonical_record.get("file_size", 0) or 0),
                public_hash=canonical_record["public_hash"],
                shortener=shortener_val
            )
            canonical_reply_id = int(canonical_record["canonical_message_id"])
        else:
            if not stored_msg:
                stored_msg = await fwd_media(file_msg)
                if not stored_msg:
                    logger.error(f"Failed to forward media for message {file_msg.id}. Skipping.")
                    return None
            links = await gen_links(stored_msg, shortener=shortener_val)
            canonical_reply_id = stored_msg.id
        if notification_msg:
            result = await safe_edit_message(
                notification_msg,
                MSG_LINKS.format(
                    file_name=links['media_name'],
                    file_size=links['media_size'],
                    download_link=links['online_link'],
                    stream_link=links['stream_link']
                ),
                parse_mode=enums.ParseMode.MARKDOWN,
                disable_web_page_preview=True,
                reply_markup=get_link_buttons(links)
            )
            if not result:
                await send_link(msg, links)
        elif not original_request_msg:
            await send_link(msg, links)
        if msg.chat.type != enums.ChatType.PRIVATE and msg.from_user and not original_request_msg:
            await send_dm_links(bot, msg.from_user.id, links, msg.chat.title or "the chat")
        source_msg = original_request_msg if original_request_msg else msg
        source_info = ""
        source_id = 0
        if source_msg.from_user:
            source_info = source_msg.from_user.full_name
            if not source_info:
                source_info = f"@{source_msg.from_user.username}" if source_msg.from_user.username else "Unknown User"
            source_id = source_msg.from_user.id
        elif source_msg.chat.type == enums.ChatType.CHANNEL:
            source_info = source_msg.chat.title or "Unknown Channel"
            source_id = source_msg.chat.id
        if source_info and source_id:
            try:
                await send_channel_links(
                    links,
                    source_info,
                    source_id,
                    target_msg=stored_msg,
                    reply_to_message_id=canonical_reply_id
                )
            except FloodWait as e:
                await asyncio.sleep(e.value)
                await send_channel_links(
                    links,
                    source_info,
                    source_id,
                    target_msg=stored_msg,
                    reply_to_message_id=canonical_reply_id
                )
        if status_msg:
            await safe_delete_message(status_msg)
        return links
    except Exception as e:
        logger.error(f"Error processing single file for message {file_msg.id}: {e}", exc_info=True)
        if status_msg:
            await safe_edit_message(status_msg, MSG_ERROR_PROCESSING_MEDIA)
        
        await notify_own(bot, MSG_CRITICAL_ERROR.format(
            error=str(e),
            error_id=secrets.token_hex(6)
        ))
        return None


async def process_batch(

    bot: Client,

    msg: Message,

    start_id: int,

    count: int,

    status_msg: Message,

    shortener_val: bool,

    notification_msg: Optional[Message] = None

):
    processed = 0
    failed = 0
    links_list = []
    for batch_start in range(0, count, BATCH_SIZE):
        batch_size = min(BATCH_SIZE, count - batch_start)
        batch_ids = list(range(start_id + batch_start, start_id + batch_start + batch_size))
        try:
            try:
                await status_msg.edit_text(
                    MSG_PROCESSING_BATCH.format(
                        batch_number=(batch_start // BATCH_SIZE) + 1,
                        total_batches=(count + BATCH_SIZE - 1) // BATCH_SIZE,
                        file_count=batch_size
                    )
                )
            except FloodWait as e:
                await asyncio.sleep(e.value)
                await status_msg.edit_text(
                    MSG_PROCESSING_BATCH.format(
                        batch_number=(batch_start // BATCH_SIZE) + 1,
                        total_batches=(count + BATCH_SIZE - 1) // BATCH_SIZE,
                        file_count=batch_size
                    )
                )
        except MessageNotModified:
            pass
        try:
            try:
                messages = await bot.get_messages(msg.chat.id, batch_ids)
            except FloodWait as e:
                await asyncio.sleep(e.value)
                messages = await bot.get_messages(msg.chat.id, batch_ids)
            if messages is None:
                messages = []
        except Exception as e:
            logger.error(f"Error getting messages in batch: {e}", exc_info=True)
            messages = []
        for m in messages:
            if m and m.media:
                links = await process_single(bot, msg, m, None, shortener_val, original_request_msg=msg)
                if links:
                    links_list.append(links['online_link'])
                    processed += 1
                else:
                    failed += 1
            else:
                failed += 1
        if (processed + failed) % BATCH_UPDATE_INTERVAL == 0 or (processed + failed) == count:
            try:
                try:
                    await status_msg.edit_text(
                        MSG_PROCESSING_STATUS.format(
                            processed=processed,
                            total=count,
                            failed=failed
                        )
                    )
                except FloodWait as e:
                    await asyncio.sleep(e.value)
                    await status_msg.edit_text(
                        MSG_PROCESSING_STATUS.format(
                            processed=processed,
                            total=count,
                            failed=failed
                        )
                    )
            except MessageNotModified:
                pass
    for i in range(0, len(links_list), LINK_CHUNK_SIZE):
        chunk = links_list[i:i+LINK_CHUNK_SIZE]
        chunk_text = MSG_BATCH_LINKS_READY.format(count=len(chunk)) + f"\n\n<code>{chr(10).join(chunk)}</code>"
        try:
            await msg.reply_text(
                chunk_text,
                quote=True,
                disable_web_page_preview=True,
                parse_mode=enums.ParseMode.HTML
            )
        except FloodWait as e:
            await asyncio.sleep(e.value)
            await msg.reply_text(
                chunk_text,
                quote=True,
                disable_web_page_preview=True,
                parse_mode=enums.ParseMode.HTML
            )
        if msg.chat.type != enums.ChatType.PRIVATE and msg.from_user:
            try:
                try:
                    await bot.send_message(
                        chat_id=msg.from_user.id,
                        text=MSG_DM_BATCH_PREFIX.format(chat_title=msg.chat.title or "the chat") + "\n" + chunk_text,
                        disable_web_page_preview=True,
                        parse_mode=enums.ParseMode.HTML
                    )
                except FloodWait as e:
                    await asyncio.sleep(e.value)
                    await bot.send_message(
                        chat_id=msg.from_user.id,
                        text=MSG_DM_BATCH_PREFIX.format(chat_title=msg.chat.title or "the chat") + "\n" + chunk_text,
                        disable_web_page_preview=True,
                        parse_mode=enums.ParseMode.HTML
                    )
            except Exception as e:
                logger.error(f"Error sending DM in batch: {e}", exc_info=True)
                await reply_user_err(msg, MSG_ERROR_DM_FAILED)
        if i + LINK_CHUNK_SIZE < len(links_list):
            await asyncio.sleep(MESSAGE_DELAY)
    try:
        await status_msg.edit_text(
            MSG_PROCESSING_RESULT.format(
                processed=processed,
                total=count,
                failed=failed
            )
        )
    except FloodWait as e:
        await asyncio.sleep(e.value)
        await status_msg.edit_text(
            MSG_PROCESSING_RESULT.format(
                processed=processed,
                total=count,
                failed=failed
            )
        )
    if notification_msg:
        await safe_delete_message(notification_msg)