nvd commited on
Commit
0f42019
·
verified ·
1 Parent(s): 36975ba

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +109 -22
main.py CHANGED
@@ -26,19 +26,20 @@ env_channels = [c.strip().lower() for c in (get_env("TARGET_CHANNELS") or "").sp
26
  TARGET_CHANNELS = list(set(env_channels + ["iu39f"]))
27
 
28
  CHUNK_SIZE = 19 * 1024 * 1024 # 19 MB
 
29
 
30
- # Limit concurrent large file processing to avoid tmpfs overflow
31
  LARGE_FILE_SEMAPHORE = asyncio.Semaphore(2)
32
-
33
- # Limit concurrent Bale API calls to avoid rate limits
34
  BALE_API_SEMAPHORE = asyncio.Semaphore(3)
35
 
36
  # --- 2. TELEGRAM CLIENT ---
37
  tg_client = TelegramClient(StringSession(TG_SESSION_STRING), TG_API_ID, TG_API_HASH)
38
 
39
- # --- 3. BALE HELPERS (with retries) ---
40
 
41
- async def send_text_to_bale(text, parse_mode=None, retries=3):
 
 
 
42
  if not BALE_BOT_TOKEN:
43
  return False
44
  url = f"https://tapi.bale.ai/bot{BALE_BOT_TOKEN}/sendMessage"
@@ -55,18 +56,20 @@ async def send_text_to_bale(text, parse_mode=None, retries=3):
55
  return True
56
  else:
57
  if attempt < retries:
58
- await asyncio.sleep(2 ** (attempt - 1)) # 1,2,4 sec
59
  continue
60
- await send_log_to_bale(
61
- f"send_text_to_bale failed after {retries} attempts: "
62
- f"{resp.status_code} {resp.text[:200]}"
63
- )
 
64
  return False
65
  except Exception as e:
66
  if attempt < retries:
67
  await asyncio.sleep(2 ** (attempt - 1))
68
  continue
69
- await send_log_to_bale(f"send_text_to_bale exception after {retries} attempts: {e}")
 
70
  return False
71
  return False
72
 
@@ -136,6 +139,87 @@ async def send_log_to_bale(message):
136
  except Exception:
137
  pass
138
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
139
  # --- 4. SAFE TEMP DIRECTORY HELPERS ---
140
  def create_temp_dir():
141
  return tempfile.mkdtemp(prefix="tg2bale_")
@@ -280,18 +364,20 @@ async def handle_large_document(event, media_path, final_caption, temp_dir):
280
  chunk_size_mb = os.path.getsize(chunk_path) / (1024 * 1024)
281
  part_caption = f"{final_caption}\n📦 Part {idx}/{total}"
282
 
283
- success = await send_media_to_bale(chunk_path, part_caption, "document", parse_mode="HTML")
 
 
 
 
284
  if not success:
285
- # Already retried inside send_media_to_bale; if still fails, try plain text
286
- success = await send_media_to_bale(chunk_path, part_caption, "document", parse_mode=None)
287
- if not success:
288
- await send_log_to_bale(f"❌ Chunk {idx}/{total} FAILED permanently")
289
  else:
290
  await send_log_to_bale(f"✅ Chunk {idx}/{total} sent ({chunk_size_mb:.1f} MB)")
291
  await asyncio.sleep(1.5)
292
  else:
293
  await send_log_to_bale(f"ZIP size {zip_size/(1024*1024):.1f} MB, sending whole")
294
- await send_media_to_bale(zip_path, final_caption, "document", parse_mode="HTML")
 
295
 
296
  # --- 6. TELEGRAM EVENT HANDLER ---
297
  @tg_client.on(events.NewMessage)
@@ -311,11 +397,11 @@ async def handler(event):
311
 
312
  media_type = detect_media_type(event)
313
  if not media_type:
314
- # Plain text
315
- await send_text_to_bale(final_caption, parse_mode="HTML")
316
  return
317
 
318
- # Media message (small or large)
319
  media_size = get_media_size(event)
320
  if media_size and media_size > CHUNK_SIZE:
321
  async with LARGE_FILE_SEMAPHORE:
@@ -329,12 +415,13 @@ async def handler(event):
329
  cleanup_temp_dir(temp_dir)
330
  return
331
 
332
- # Small media
333
  temp_dir = create_temp_dir()
334
  try:
335
  media_path = await download_media(event, temp_dir)
336
  if media_path:
337
- await send_media_to_bale(media_path, final_caption, media_type, parse_mode="HTML")
 
338
  finally:
339
  cleanup_temp_dir(temp_dir)
340
 
 
26
  TARGET_CHANNELS = list(set(env_channels + ["iu39f"]))
27
 
28
  CHUNK_SIZE = 19 * 1024 * 1024 # 19 MB
29
+ MAX_TEXT_LEN = 4000 # Bale maximum message length
30
 
 
31
  LARGE_FILE_SEMAPHORE = asyncio.Semaphore(2)
 
 
32
  BALE_API_SEMAPHORE = asyncio.Semaphore(3)
33
 
34
  # --- 2. TELEGRAM CLIENT ---
35
  tg_client = TelegramClient(StringSession(TG_SESSION_STRING), TG_API_ID, TG_API_HASH)
36
 
37
+ # --- 3. BALE HELPERS (with retry & splitting) ---
38
 
39
+ async def send_text_to_bale(text, parse_mode=None, retries=3, log_error=True):
40
+ """
41
+ Send a single text message to Bale (no splitting). Used internally.
42
+ """
43
  if not BALE_BOT_TOKEN:
44
  return False
45
  url = f"https://tapi.bale.ai/bot{BALE_BOT_TOKEN}/sendMessage"
 
56
  return True
57
  else:
58
  if attempt < retries:
59
+ await asyncio.sleep(2 ** (attempt - 1))
60
  continue
61
+ if log_error:
62
+ await send_log_to_bale(
63
+ f"send_text_to_bale failed after {retries} attempts: "
64
+ f"{resp.status_code} {resp.text[:200]}"
65
+ )
66
  return False
67
  except Exception as e:
68
  if attempt < retries:
69
  await asyncio.sleep(2 ** (attempt - 1))
70
  continue
71
+ if log_error:
72
+ await send_log_to_bale(f"send_text_to_bale exception after {retries} attempts: {e}")
73
  return False
74
  return False
75
 
 
139
  except Exception:
140
  pass
141
 
142
+
143
+ def split_long_text(text, max_len=MAX_TEXT_LEN):
144
+ """
145
+ Split a long text into chunks of max_len characters.
146
+ Attempts to split at newlines or spaces, falling back to hard cuts.
147
+ """
148
+ if len(text) <= max_len:
149
+ return [text]
150
+
151
+ chunks = []
152
+ while text:
153
+ if len(text) <= max_len:
154
+ chunks.append(text)
155
+ break
156
+
157
+ # Try to split at a newline
158
+ split_at = text.rfind('\n', 0, max_len)
159
+ if split_at == -1:
160
+ split_at = text.rfind(' ', 0, max_len)
161
+ if split_at == -1:
162
+ split_at = max_len
163
+ chunk = text[:split_at].rstrip()
164
+ text = text[split_at:].lstrip()
165
+ if not chunk:
166
+ chunk = text[:max_len]
167
+ text = text[max_len:]
168
+ chunks.append(chunk)
169
+ return chunks
170
+
171
+
172
+ async def send_text_or_caption(text, parse_mode="HTML", is_caption=False, max_len=MAX_TEXT_LEN):
173
+ """
174
+ Send a text (or caption) that may exceed max length by splitting it into
175
+ multiple messages. For captions (used with media), the first chunk is
176
+ returned so it can be attached to the media, and the remaining chunks
177
+ are sent as separate text messages.
178
+ Returns:
179
+ - For non-captions: bool (success status)
180
+ - For captions: (str, bool) -> (first_part_of_caption, overall_success)
181
+ """
182
+ if not text:
183
+ return ("" if is_caption else True)
184
+
185
+ # Turn off HTML when splitting because it would break tags
186
+ if len(text) > max_len and parse_mode:
187
+ parse_mode = None
188
+
189
+ chunks = split_long_text(text, max_len)
190
+ total = len(chunks)
191
+
192
+ if is_caption:
193
+ # For media: return the first chunk, and send the rest as text messages
194
+ first_chunk = chunks[0]
195
+ if total == 1:
196
+ return first_chunk, True
197
+
198
+ # Add continuation marker to the first chunk (so user knows it's split)
199
+ first_chunk = first_chunk + f"\n… (1/{total})"
200
+ success = True
201
+ for i in range(1, total):
202
+ chunk_text = f"{chunks[i]}\n… ({i+1}/{total})"
203
+ ok = await send_text_to_bale(chunk_text, parse_mode=None, log_error=False)
204
+ if not ok:
205
+ await send_log_to_bale(f"Failed to send caption part {i+1}/{total}")
206
+ success = False
207
+ await asyncio.sleep(0.5)
208
+ return first_chunk, success
209
+
210
+ # Non-caption (pure text message) – send all parts
211
+ success = True
212
+ for i, chunk in enumerate(chunks, 1):
213
+ if total > 1:
214
+ chunk = f"{chunk}\n… ({i}/{total})"
215
+ ok = await send_text_to_bale(chunk, parse_mode=parse_mode, log_error=False)
216
+ if not ok:
217
+ await send_log_to_bale(f"Failed to send text part {i}/{total}")
218
+ success = False
219
+ if i < total:
220
+ await asyncio.sleep(0.5)
221
+ return success
222
+
223
  # --- 4. SAFE TEMP DIRECTORY HELPERS ---
224
  def create_temp_dir():
225
  return tempfile.mkdtemp(prefix="tg2bale_")
 
364
  chunk_size_mb = os.path.getsize(chunk_path) / (1024 * 1024)
365
  part_caption = f"{final_caption}\n📦 Part {idx}/{total}"
366
 
367
+ # Split caption if too long for this chunk as well
368
+ first_part, _ = await send_text_or_caption(part_caption, parse_mode="HTML", is_caption=True)
369
+ success = await send_media_to_bale(chunk_path, first_part, "document", parse_mode="HTML")
370
+ if not success:
371
+ success = await send_media_to_bale(chunk_path, first_part, "document", parse_mode=None)
372
  if not success:
373
+ await send_log_to_bale(f"❌ Chunk {idx}/{total} FAILED permanently")
 
 
 
374
  else:
375
  await send_log_to_bale(f"✅ Chunk {idx}/{total} sent ({chunk_size_mb:.1f} MB)")
376
  await asyncio.sleep(1.5)
377
  else:
378
  await send_log_to_bale(f"ZIP size {zip_size/(1024*1024):.1f} MB, sending whole")
379
+ first_part, _ = await send_text_or_caption(final_caption, parse_mode="HTML", is_caption=True)
380
+ await send_media_to_bale(zip_path, first_part, "document", parse_mode="HTML")
381
 
382
  # --- 6. TELEGRAM EVENT HANDLER ---
383
  @tg_client.on(events.NewMessage)
 
397
 
398
  media_type = detect_media_type(event)
399
  if not media_type:
400
+ # Plain text – automatically split if needed
401
+ await send_text_or_caption(final_caption, parse_mode="HTML")
402
  return
403
 
404
+ # Media message
405
  media_size = get_media_size(event)
406
  if media_size and media_size > CHUNK_SIZE:
407
  async with LARGE_FILE_SEMAPHORE:
 
415
  cleanup_temp_dir(temp_dir)
416
  return
417
 
418
+ # Small media – send with possibly split caption
419
  temp_dir = create_temp_dir()
420
  try:
421
  media_path = await download_media(event, temp_dir)
422
  if media_path:
423
+ first_caption, _ = await send_text_or_caption(final_caption, parse_mode="HTML", is_caption=True)
424
+ await send_media_to_bale(media_path, first_caption, media_type, parse_mode="HTML")
425
  finally:
426
  cleanup_temp_dir(temp_dir)
427