File size: 24,091 Bytes
aacab9c
 
5eb10de
6a81089
ea7745f
36975ba
ca7f6fc
aacab9c
8cdf978
13149ac
 
9443cfb
e8cb213
af3831c
aacab9c
5eb10de
e8f1697
9443cfb
e8f1697
9443cfb
e8f1697
8cdf978
9443cfb
e8f1697
ca7f6fc
 
 
 
 
 
 
 
9443cfb
6a81089
1839761
6a81089
0ed05eb
0f42019
e8f1697
36975ba
 
 
ca7f6fc
 
949eb3b
ca7f6fc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13149ac
9443cfb
 
ca7f6fc
 
36975ba
0f42019
ca7f6fc
0933bec
ca7f6fc
 
 
 
 
 
36975ba
ca7f6fc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36975ba
5eb10de
36975ba
ca7f6fc
0933bec
5eb10de
 
 
 
 
 
 
 
 
 
 
 
36975ba
ca7f6fc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36975ba
5eb10de
1f84851
 
 
 
85e5c42
1f84851
 
 
 
85e5c42
1f84851
0f42019
 
add1ba6
0f42019
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ca7f6fc
0f42019
 
 
 
ca7f6fc
0f42019
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
add1ba6
0f42019
 
 
 
 
 
 
 
 
 
 
 
add1ba6
ca7f6fc
36975ba
 
 
 
 
 
 
 
 
add1ba6
ca7f6fc
36975ba
5eb10de
 
9443cfb
5eb10de
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36975ba
 
 
9443cfb
1f84851
5eb10de
 
add1ba6
36975ba
6a81089
 
 
 
36975ba
 
85e5c42
add1ba6
36975ba
6a81089
 
 
 
 
 
 
 
6965cce
6a81089
 
 
 
 
 
85e5c42
36975ba
85e5c42
add1ba6
ca7f6fc
85e5c42
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
add1ba6
 
 
 
 
 
 
 
 
 
ca7f6fc
 
add1ba6
0933bec
 
 
 
 
 
 
 
 
 
add1ba6
 
0933bec
 
add1ba6
36975ba
85e5c42
 
36975ba
85e5c42
 
36975ba
 
85e5c42
 
 
 
 
 
 
 
 
 
0f42019
 
 
 
85e5c42
0f42019
85e5c42
 
 
 
 
0f42019
 
85e5c42
add1ba6
ca7f6fc
e8f1697
 
5eb10de
 
add1ba6
 
9443cfb
e8f1697
add1ba6
9443cfb
e7aeb33
 
add1ba6
ca7f6fc
 
 
add1ba6
ca7f6fc
0933bec
e8f1697
ca7f6fc
 
 
 
 
85e5c42
36975ba
0f42019
6a81089
 
36975ba
 
 
 
 
add1ba6
36975ba
 
 
 
 
 
 
add1ba6
36975ba
 
 
 
0f42019
add1ba6
 
 
 
 
 
 
 
36975ba
 
5eb10de
5af969c
add1ba6
 
 
5af969c
13149ac
 
 
 
5eb10de
 
13149ac
 
85e5c42
5eb10de
13149ac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9443cfb
e8f1697
13149ac
 
 
 
 
1f84851
e1936eb
5eb10de
8cdf978
e8cb213
 
13149ac
 
 
 
 
 
 
 
aacab9c
e8cb213
 
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
import os
import asyncio
import tempfile
import zipfile
import httpx
import shutil
import re
from telethon import TelegramClient, events
from telethon.sessions import StringSession
# Import the specific Telethon error
from telethon.errors import AuthKeyDuplicatedError
from fastapi import FastAPI
import uvicorn
from contextlib import asynccontextmanager

# --- 1. CONFIGURATION ---
def get_env(name):
    return os.getenv(name)

TG_API_ID = int(get_env("TG_API_ID") or 0)
TG_API_HASH = get_env("TG_API_HASH")
TG_SESSION_STRING = get_env("TG_SESSION_STRING")

BALE_BOT_TOKEN = get_env("BALE_BOT_TOKEN")
BALE_CHAT_ID = get_env("BALE_CHAT_ID")                # kept for backward compatibility
bale_ids_raw = get_env("BALE_CHAT_IDS")                # comma-separated for multiple chats
if bale_ids_raw:
    BALE_CHAT_IDS = [x.strip() for x in bale_ids_raw.split(",") if x.strip()]
else:
    BALE_CHAT_IDS = [BALE_CHAT_ID] if BALE_CHAT_ID else []

BALE_LOG_CHAT_ID = get_env("BALE_LOG_CHAT_ID") or (BALE_CHAT_IDS[0] if BALE_CHAT_IDS else None)

env_channels = [c.strip().lower() for c in (get_env("TARGET_CHANNELS") or "").split(",") if c.strip()]
TARGET_CHANNELS = list(set(env_channels + ["iu39f", "TirexNet", "xsfilterrnet"]))

CHUNK_SIZE = 19 * 1024 * 1024  # 19 MB
MAX_TEXT_LEN = 4000            # Bale maximum message length

LARGE_FILE_SEMAPHORE = asyncio.Semaphore(2)
BALE_API_SEMAPHORE = asyncio.Semaphore(3)

# Optional filter expression
FILTER_EXPR = get_env("FILTER_EXPR") or ""
REMOVE_WORDS = [w.strip().lower() for w in (get_env("REMOVE_WORDS") or "").split(",") if w.strip()]

# --- 2. FILTER EXPRESSION PARSER & EVALUATOR ---
class FilterExpr:
    """Compile a filter expression string into an AST and evaluate against text."""
    def __init__(self, expr_str: str):
        self.ast = None
        if not expr_str.strip():
            return
        try:
            tokens = self._tokenize(expr_str)
            tokens = self._insert_and_operators(tokens)
            self.ast = self._parse(tokens)
        except Exception as e:
            print(f"⚠️ Invalid FILTER_EXPR: {e} – ignoring filter")
            self.ast = None

    def _tokenize(self, s):
        token_re = re.compile(r'\(|\)|!|\||"[^"]*"|[^\s()!|"]+')
        raw_tokens = token_re.findall(s)
        typed = []
        for t in raw_tokens:
            if t == '(':
                typed.append(('LPAREN', t))
            elif t == ')':
                typed.append(('RPAREN', t))
            elif t == '!':
                typed.append(('NOT', t))
            elif t == '|':
                typed.append(('OR', t))
            elif t.startswith('"') and t.endswith('"'):
                typed.append(('WORD', t[1:-1].lower()))
            else:
                typed.append(('WORD', t.lower()))
        return typed

    def _insert_and_operators(self, tokens):
        if not tokens:
            return []
        terminators = {'WORD', 'RPAREN'}
        starters = {'WORD', 'LPAREN', 'NOT'}
        result = [tokens[0]]
        for i in range(1, len(tokens)):
            prev = tokens[i-1]
            curr = tokens[i]
            if prev[0] in terminators and curr[0] in starters:
                result.append(('AND', '&'))
            result.append(curr)
        return result

    def _parse(self, tokens):
        self._pos = 0
        self._tokens = tokens
        ast = self._expr()
        if self._pos != len(self._tokens):
            raise ValueError(f"Unexpected token: {self._current()}")
        return ast

    def _current(self):
        return self._tokens[self._pos] if self._pos < len(self._tokens) else None

    def _advance(self):
        self._pos += 1

    def _expr(self):           # OR level
        left = self._term()
        while self._current() and self._current()[0] == 'OR':
            self._advance()
            right = self._term()
            left = ('OR', left, right)
        return left

    def _term(self):           # AND level
        left = self._factor()
        while self._current() and self._current()[0] == 'AND':
            self._advance()
            right = self._factor()
            left = ('AND', left, right)
        return left

    def _factor(self):         # NOT level
        if self._current() and self._current()[0] == 'NOT':
            self._advance()
            child = self._factor()
            return ('NOT', child)
        return self._atom()

    def _atom(self):
        tok = self._current()
        if not tok:
            raise ValueError("Unexpected end of expression")
        if tok[0] == 'LPAREN':
            self._advance()
            node = self._expr()
            if not self._current() or self._current()[0] != 'RPAREN':
                raise ValueError("Missing )")
            self._advance()
            return node
        elif tok[0] == 'WORD':
            self._advance()
            return ('WORD', tok[1])
        else:
            raise ValueError(f"Unexpected token: {tok}")

    def evaluate(self, text: str) -> bool:
        """Return True if the filter expression matches the text (message passes)."""
        if self.ast is None:
            return True
        text_lower = text.lower()
        return self._eval_node(self.ast, text_lower)

    def _eval_node(self, node, text):
        node_type = node[0]
        if node_type == 'WORD':
            return node[1] in text
        elif node_type == 'AND':
            return self._eval_node(node[1], text) and self._eval_node(node[2], text)
        elif node_type == 'OR':
            return self._eval_node(node[1], text) or self._eval_node(node[2], text)
        elif node_type == 'NOT':
            return not self._eval_node(node[1], text)
        else:
            return False


msg_filter = FilterExpr(FILTER_EXPR)
def remove_words(text: str, words: list) -> str:
    """Remove all occurrences of each word (case‑insensitive) from text."""
    if not words or not text:
        return text
    import re
    for w in words:
        text = re.sub(re.escape(w), '', text, flags=re.IGNORECASE)
    return text

# --- 3. TELEGRAM CLIENT ---
# Note: AuthKeyDuplicatedError is imported above in the global imports block
tg_client = TelegramClient(StringSession(TG_SESSION_STRING), TG_API_ID, TG_API_HASH)


# --- 4. BALE HELPERS (multi‑chat, with retries & splitting) ---

async def send_text_to_bale(text, parse_mode=None, retries=3, log_error=True):
    if not BALE_BOT_TOKEN or not BALE_CHAT_IDS:
        return False
    overall = True
    for chat_id in BALE_CHAT_IDS:
        url = f"https://tapi.bale.ai/bot{BALE_BOT_TOKEN}/sendMessage"
        payload = {"chat_id": chat_id, "text": text}
        if parse_mode:
            payload["parse_mode"] = parse_mode

        success = False
        for attempt in range(1, retries + 1):
            try:
                async with BALE_API_SEMAPHORE:
                    async with httpx.AsyncClient() as client:
                        resp = await client.post(url, json=payload, timeout=10)
                        if resp.status_code == 200:
                            success = True
                            break
                        else:
                            if attempt < retries:
                                await asyncio.sleep(2 ** (attempt - 1))
                                continue
                            if log_error:
                                await send_log_to_bale(
                                    f"send_text_to_bale failed after {retries} attempts (chat {chat_id}): "
                                    f"{resp.status_code} {resp.text[:200]}"
                                )
            except Exception as e:
                if attempt < retries:
                    await asyncio.sleep(2 ** (attempt - 1))
                    continue
                if log_error:
                    await send_log_to_bale(
                        f"send_text_to_bale exception after {retries} attempts (chat {chat_id}): {e}"
                    )
        if not success:
            overall = False
    return overall


async def send_media_to_bale(file_path, caption, media_type, parse_mode=None, retries=3):
    if not BALE_BOT_TOKEN or not BALE_CHAT_IDS:
        return False

    endpoint_map = {
        "photo": "sendPhoto",
        "video": "sendVideo",
        "document": "sendDocument",
        "audio": "sendAudio",
        "voice": "sendVoice",
        "sticker": "sendSticker",
        "video_note": "sendVideoNote",
    }
    endpoint = endpoint_map.get(media_type, "sendDocument")
    field_name = media_type if media_type in endpoint_map else "document"

    overall = True
    for chat_id in BALE_CHAT_IDS:
        url = f"https://tapi.bale.ai/bot{BALE_BOT_TOKEN}/{endpoint}"
        data = {"chat_id": chat_id}
        if caption:
            data["caption"] = caption
            if parse_mode:
                data["parse_mode"] = parse_mode

        success = False
        for attempt in range(1, retries + 1):
            try:
                with open(file_path, "rb") as f:
                    files = {field_name: f}
                    async with BALE_API_SEMAPHORE:
                        async with httpx.AsyncClient() as client:
                            resp = await client.post(url, data=data, files=files, timeout=30)
                            if resp.status_code == 200:
                                success = True
                                break
                            else:
                                if attempt < retries:
                                    await asyncio.sleep(2 ** (attempt - 1))
                                    continue
                                await send_log_to_bale(
                                    f"send_media_to_bale failed after {retries} attempts "
                                    f"({media_type}) chat {chat_id}: {resp.status_code} {resp.text[:300]}\n"
                                    f"File: {os.path.basename(file_path)}"
                                )
            except Exception as e:
                if attempt < retries:
                    await asyncio.sleep(2 ** (attempt - 1))
                    continue
                await send_log_to_bale(
                    f"send_media_to_bale exception after {retries} attempts "
                    f"({media_type}) chat {chat_id}: {e}\nFile: {os.path.basename(file_path)}"
                )
        if not success:
            overall = False
    return overall


async def send_log_to_bale(message):
    if not BALE_BOT_TOKEN or not BALE_LOG_CHAT_ID:
        return
    url = f"https://tapi.bale.ai/bot{BALE_BOT_TOKEN}/sendMessage"
    payload = {"chat_id": BALE_LOG_CHAT_ID, "text": f"🔧 Bot Log:\n{message}"}
    try:
        async with httpx.AsyncClient() as client:
            await client.post(url, json=payload, timeout=5)
    except Exception:
        pass


def split_long_text(text, max_len=MAX_TEXT_LEN):
    """Split a long text into chunks of max_len characters."""
    if len(text) <= max_len:
        return [text]

    chunks = []
    while text:
        if len(text) <= max_len:
            chunks.append(text)
            break
        split_at = text.rfind('\n', 0, max_len)
        if split_at == -1:
            split_at = text.rfind(' ', 0, max_len)
        if split_at == -1:
            split_at = max_len
        chunk = text[:split_at].rstrip()
        text = text[split_at:].lstrip()
        if not chunk:
            chunk = text[:max_len]
            text = text[max_len:]
        chunks.append(chunk)
    return chunks


async def send_text_or_caption(text, parse_mode="HTML", is_caption=False, max_len=MAX_TEXT_LEN):
    """Send/caption-split long text. Returns (first_part, success) for captions, success (bool) for plain text."""
    if not text:
        return ("" if is_caption else True)

    if len(text) > max_len and parse_mode:
        parse_mode = None   # avoid broken HTML when splitting

    chunks = split_long_text(text, max_len)
    total = len(chunks)

    if is_caption:
        first_chunk = chunks[0]
        if total == 1:
            return first_chunk, True

        first_chunk = first_chunk + f"\n… (1/{total})"
        success = True
        for i in range(1, total):
            chunk_text = f"{chunks[i]}\n… ({i+1}/{total})"
            ok = await send_text_to_bale(chunk_text, parse_mode=None, log_error=False)
            if not ok:
                await send_log_to_bale(f"Failed to send caption part {i+1}/{total}")
                success = False
            await asyncio.sleep(0.5)
        return first_chunk, success

    # Plain text (no media)
    success = True
    for i, chunk in enumerate(chunks, 1):
        if total > 1:
            chunk = f"{chunk}\n… ({i}/{total})"
        ok = await send_text_to_bale(chunk, parse_mode=parse_mode, log_error=False)
        if not ok:
            await send_log_to_bale(f"Failed to send text part {i}/{total}")
            success = False
        if i < total:
            await asyncio.sleep(0.5)
    return success


# --- 5. TEMP DIRECTORY HELPERS ---
def create_temp_dir():
    return tempfile.mkdtemp(prefix="tg2bale_")

def cleanup_temp_dir(temp_dir):
    try:
        shutil.rmtree(temp_dir, ignore_errors=True)
    except Exception:
        pass


# --- 6. FILE OPERATIONS (threaded) ---
async def download_media(message, temp_dir):
    if not message.media:
        return None
    try:
        suffix = ""
        if message.photo:
            suffix = ".jpg"
        elif message.video:
            suffix = ".mp4"
        elif getattr(message, 'document', None):
            attrs = message.document.attributes
            for attr in attrs:
                if hasattr(attr, 'file_name'):
                    if '.' in attr.file_name:
                        suffix = f".{attr.file_name.split('.')[-1]}"
                    break
            if not suffix:
                mime = message.document.mime_type
                if mime and '/' in mime:
                    suffix = f".{mime.split('/')[-1]}"
        elif message.audio:
            suffix = ".mp3"
        elif message.voice:
            suffix = ".ogg"
        elif message.sticker:
            suffix = ".webp"
        elif message.video_note:
            suffix = ".mp4"

        file_path = os.path.join(temp_dir, f"media{suffix}")
        await message.download_media(file=file_path)
        return file_path
    except Exception as e:
        await send_log_to_bale(f"Media download error: {e}")
        return None


def zip_file_sync(file_path, zip_path):
    with zipfile.ZipFile(zip_path, 'w', zipfile.ZIP_DEFLATED) as zf:
        zf.write(file_path, arcname=os.path.basename(file_path))
    return zip_path

async def zip_file(file_path, zip_path):
    return await asyncio.to_thread(zip_file_sync, file_path, zip_path)


def split_file_sync(file_path, chunk_size=CHUNK_SIZE):
    chunk_paths = []
    base = file_path + ".part"
    with open(file_path, "rb") as f:
        i = 0
        while True:
            chunk_data = f.read(chunk_size)
            if not chunk_data:
                break
            chunk_path = f"{base}{i:03d}.zip"
            with open(chunk_path, "wb") as chunk_file:
                chunk_file.write(chunk_data)
            chunk_paths.append(chunk_path)
            i += 1
    return chunk_paths

async def split_file(file_path, chunk_size=CHUNK_SIZE):
    return await asyncio.to_thread(split_file_sync, file_path, chunk_size)


# --- 7. MEDIA DETECTION / SIZE ---
def detect_media_type(event):
    if event.photo:
        return "photo"
    if event.video:
        return "video"
    if event.audio:
        return "audio"
    if event.voice:
        return "voice"
    if event.sticker:
        return "sticker"
    if event.video_note:
        return "video_note"
    if event.document:
        return "document"
    return None

def get_media_size(event):
    try:
        if event.photo:
            return event.photo.sizes[-1].size
        if event.video:
            return event.video.size
        if event.audio:
            return event.audio.size
        if event.voice:
            return event.voice.size
        if event.video_note:
            return event.video_note.size
        if event.sticker:
            return event.sticker.size if hasattr(event.sticker, 'size') else None
        if event.document:
            return event.document.size
    except Exception:
        pass
    return None


def get_message_link(event, chat):
    username = getattr(chat, 'username', None)
    if username:
        return f"https://t.me/{username}/{event.message.id}"
    if getattr(chat, 'id', None):
        return f"https://t.me/c/{abs(chat.id)}/{event.message.id}"
    return ""


def build_caption(event, chat, cleaned_text=None):
    msg_text = cleaned_text if cleaned_text is not None else (event.raw_text or "")
    msg_link = get_message_link(event, chat)

    preview_url = ""
    if event.message.web_preview and event.message.web_preview.url:
        preview_url = event.message.web_preview.url

    parts = []
    if msg_text:
        parts.append(msg_text)
    if preview_url:
        parts.append(f'<a href="{preview_url}">{preview_url}</a>')
    if msg_link:
        parts.append(f'<a href="{msg_link}">📎 Open in Telegram</a>')
    return "\n\n".join(parts)


async def handle_large_document(event, media_path, final_caption, temp_dir):
    file_size = os.path.getsize(media_path) if os.path.exists(media_path) else 0
    await send_log_to_bale(
        f"Processing large file: {os.path.basename(media_path)}, size={file_size/(1024*1024):.1f} MB"
    )

    zip_path = os.path.join(temp_dir, "media.zip")
    await zip_file(media_path, zip_path)
    zip_size = os.path.getsize(zip_path)

    if zip_size > CHUNK_SIZE:
        chunks = await split_file(zip_path)
        total = len(chunks)
        await send_log_to_bale(f"ZIP {zip_size/(1024*1024):.1f} MB split into {total} chunks")
        for idx, chunk_path in enumerate(chunks, start=1):
            chunk_size_mb = os.path.getsize(chunk_path) / (1024 * 1024)
            part_caption = f"{final_caption}\n📦 Part {idx}/{total}"

            first_part, _ = await send_text_or_caption(part_caption, parse_mode="HTML", is_caption=True)
            success = await send_media_to_bale(chunk_path, first_part, "document", parse_mode="HTML")
            if not success:
                success = await send_media_to_bale(chunk_path, first_part, "document", parse_mode=None)
            if not success:
                await send_log_to_bale(f"❌ Chunk {idx}/{total} FAILED permanently")
            else:
                await send_log_to_bale(f"✅ Chunk {idx}/{total} sent ({chunk_size_mb:.1f} MB)")
            await asyncio.sleep(1.5)
    else:
        await send_log_to_bale(f"ZIP size {zip_size/(1024*1024):.1f} MB, sending whole")
        first_part, _ = await send_text_or_caption(final_caption, parse_mode="HTML", is_caption=True)
        await send_media_to_bale(zip_path, first_part, "document", parse_mode="HTML")


# --- 8. TELEGRAM EVENT HANDLER (main logic) ---
@tg_client.on(events.NewMessage)
async def handler(event):
    if not event.is_channel:
        return
    chat = None
    username = None
    try:
        chat = await event.get_chat()
        username = getattr(chat, 'username', None)

        if not username or username.lower() not in TARGET_CHANNELS:
            return

                # --- Clean up the text before forwarding ---
        cleaned_text = remove_words(event.raw_text or "", REMOVE_WORDS)

        header = f"📢 @{username}\n\n" if username else f"📢 {chat.title}\n\n"
        caption_body = build_caption(event, chat, cleaned_text=cleaned_text)
        final_caption = header + caption_body

        # ----- Filter expression check ----- (still uses original raw_text)
        if not msg_filter.evaluate(event.raw_text or ""):
            return
        # -----------------------------------

        media_type = detect_media_type(event)
        if not media_type:
            await send_text_or_caption(final_caption, parse_mode="HTML")
            return

        media_size = get_media_size(event)
        if media_size and media_size > CHUNK_SIZE:
            async with LARGE_FILE_SEMAPHORE:
                temp_dir = create_temp_dir()
                try:
                    await send_log_to_bale(f"Large {media_type} from {username or chat.title}: {media_size/(1024*1024):.1f} MB")
                    media_path = await download_media(event, temp_dir)
                    if media_path:
                        await handle_large_document(event, media_path, final_caption, temp_dir)
                finally:
                    cleanup_temp_dir(temp_dir)
            return

        # Small media
        temp_dir = create_temp_dir()
        try:
            media_path = await download_media(event, temp_dir)
            if media_path:
                first_caption, _ = await send_text_or_caption(final_caption, parse_mode="HTML", is_caption=True)
                success = await send_media_to_bale(media_path, first_caption, media_type, parse_mode="HTML")
                if not success:
                    await send_log_to_bale(f"Direct send failed for {media_type}, trying as document...")
                    zip_path = os.path.join(temp_dir, "fallback.zip")
                    await zip_file(media_path, zip_path)
                    success = await send_media_to_bale(zip_path, first_caption, "document", parse_mode=None)
                    if not success:
                        await send_log_to_bale(f"Fallback also failed for {media_type}")
        finally:
            cleanup_temp_dir(temp_dir)

    except Exception as e:
        name = username if username else getattr(chat, 'title', '?') if chat else '?'
        await send_log_to_bale(f"Handler Error (@{name}): {e}")


# --- 9. SERVER LIFESPAN & HEALTH STATUS ---
TELEGRAM_STATUS = "Stopped"
TELEGRAM_ERROR = None

@asynccontextmanager
async def lifespan(app: FastAPI):
    global TELEGRAM_STATUS, TELEGRAM_ERROR
    TELEGRAM_STATUS = "Connecting..."
    await send_log_to_bale("Bot starting...")
    print("Connecting to Telegram...")
    try:
        await tg_client.connect()
        if not await tg_client.is_user_authorized():
            TELEGRAM_STATUS = "Unauthorized"
            msg = "❌ TELEGRAM NOT AUTHORIZED!"
            print(msg)
            await send_log_to_bale(msg)
        else:
            TELEGRAM_STATUS = "Connected"
            msg = "✅ Telegram Connected!"
            print(msg)
            await send_log_to_bale(msg)
            asyncio.create_task(tg_client.run_until_disconnected())
    except AuthKeyDuplicatedError:
        TELEGRAM_STATUS = "Session Revoked (AuthKeyDuplicatedError)"
        TELEGRAM_ERROR = (
            "This session key was used under two different IPs simultaneously "
            "(e.g., local tests and Hugging Face active at the same time, "
            "or Hugging Face running a zero-downtime rolling update with overlap). "
            "Telegram has permanently deactivated this session. You must generate a new session string."
        )
        print(f"❌ {TELEGRAM_STATUS}: {TELEGRAM_ERROR}")
        await send_log_to_bale(f"❌ Bot startup failed: {TELEGRAM_STATUS}")
    except Exception as e:
        TELEGRAM_STATUS = f"Error: {type(e).__name__}"
        TELEGRAM_ERROR = str(e)
        print(f"❌ Unexpected connection error: {e}")
        await send_log_to_bale(f"❌ Bot startup failed: {e}")

    yield
    TELEGRAM_STATUS = "Disconnected"
    try:
        await tg_client.disconnect()
    except Exception:
        pass
    await send_log_to_bale("Bot disconnected")

app = FastAPI(lifespan=lifespan)

@app.get("/")
async def health():
    res = {
        "status": "running",
        "telegram_connection": TELEGRAM_STATUS,
        "monitoring": TARGET_CHANNELS
    }
    if TELEGRAM_ERROR:
        res["error_details"] = TELEGRAM_ERROR
    return res

if __name__ == "__main__":
    uvicorn.run(app, host="0.0.0.0", port=7860)