pmrony commited on
Commit
5bfca4b
Β·
verified Β·
1 Parent(s): 48f1d9a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +62 -40
app.py CHANGED
@@ -363,75 +363,69 @@ async def save_progress(source_id, dest_id, msg_id):
363
  except Exception as e: print(f"Error saving progress: {e}")
364
 
365
 
366
- # ================= AUTO SHARE LOGIC (5 videos / 5 mins) =================
367
  async def auto_share_loop(client, target_chat_id, delay=300):
368
  global auto_share_running
369
 
370
  while auto_share_running:
371
  try:
372
- # Check maximum message ID to avoid infinite blank search
373
- latest_msg_id = 0
374
- async for m in client.get_chat_history(STORAGE_CHANNEL_ID, limit=1):
375
- latest_msg_id = m.id
376
- break
377
-
378
  progress_res = await db_query(lambda: supabase.table('clone_progress').select('last_copied_id').eq('source_id', STORAGE_CHANNEL_ID).eq('dest_id', target_chat_id).execute())
379
  last_checked_id = 0
380
  if progress_res.data:
381
  last_checked_id = progress_res.data[0]['last_copied_id']
382
-
383
- if latest_msg_id > 0 and last_checked_id >= latest_msg_id:
384
- await asyncio.sleep(60)
385
- continue
386
 
387
  batch_to_send = []
388
- while len(batch_to_send) < 5 and auto_share_running:
389
- if latest_msg_id > 0 and last_checked_id >= latest_msg_id:
390
- break
391
-
392
- chunk_ids = list(range(last_checked_id + 1, min(last_checked_id + 51, latest_msg_id + 1)))
393
- if not chunk_ids:
394
- break
395
-
396
- try:
397
- msgs = await client.get_messages(STORAGE_CHANNEL_ID, chunk_ids)
398
- for idx, msg in enumerate(msgs):
399
- msg_expected_id = chunk_ids[idx]
400
- if msg is None or getattr(msg, "empty", False):
401
- last_checked_id = max(last_checked_id, msg_expected_id)
402
- continue
403
-
404
  if msg.video or (msg.document and msg.document.mime_type and "video" in msg.document.mime_type) or msg.photo:
405
  batch_to_send.append(msg)
406
- last_checked_id = max(last_checked_id, msg.id)
407
  if len(batch_to_send) == 5:
408
  break
409
- else:
410
- last_checked_id = max(last_checked_id, msg.id)
411
- except FloodWait as e:
412
- await asyncio.sleep(e.value + 1)
413
- except Exception as e:
414
- print(f"Fetch error in auto share: {e}")
415
- await asyncio.sleep(5)
416
-
417
  if not batch_to_send:
418
- await save_progress(STORAGE_CHANNEL_ID, target_chat_id, last_checked_id)
419
- await asyncio.sleep(10)
 
420
  continue
421
 
 
422
  sent_message_ids = []
 
423
  for msg in batch_to_send:
424
  if not auto_share_running: break
425
  try:
426
  sent = await msg.copy(target_chat_id)
427
  sent_message_ids.append(sent.id)
 
428
  await asyncio.sleep(1.5)
429
  except FloodWait as e:
430
  await asyncio.sleep(e.value + 1)
431
  except Exception as e:
432
  print(f"Error copying msg {msg.id}: {e}")
433
 
434
- await save_progress(STORAGE_CHANNEL_ID, target_chat_id, last_checked_id)
 
435
 
436
  for _ in range(delay):
437
  if not auto_share_running: break
@@ -476,6 +470,34 @@ async def stop_sharing_cmd(client, message):
476
  await message.reply("πŸ›‘ <b>Auto-sharing stopped successfully!</b>", parse_mode=enums.ParseMode.HTML)
477
 
478
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
479
  # ================= TELEGRAM BOT COMMANDS =================
480
  @bot.on_message(filters.command("start"))
481
  async def start(client, message):
@@ -1174,7 +1196,7 @@ async def auto_clean_channel_loop():
1174
  print(f"Auto clean error: {e}")
1175
  await asyncio.sleep(4 * 3600)
1176
 
1177
- @bot.on_message(filters.private & filters.user(ADMIN_IDS) & ~filters.command(["start", "stats", "users", "broadcast", "png", "addvideo", "blur", "clean", "sendto", "clone", "upload", "startshare", "stopshare"]))
1178
  async def catch_admin_steps(client, message):
1179
  state = admin_states.get(message.chat.id, {})
1180
  if state.get("step") == 1:
 
363
  except Exception as e: print(f"Error saving progress: {e}")
364
 
365
 
366
+ # ================= FIXED AUTO SHARE LOGIC (Without get_chat_history) =================
367
  async def auto_share_loop(client, target_chat_id, delay=300):
368
  global auto_share_running
369
 
370
  while auto_share_running:
371
  try:
 
 
 
 
 
 
372
  progress_res = await db_query(lambda: supabase.table('clone_progress').select('last_copied_id').eq('source_id', STORAGE_CHANNEL_ID).eq('dest_id', target_chat_id).execute())
373
  last_checked_id = 0
374
  if progress_res.data:
375
  last_checked_id = progress_res.data[0]['last_copied_id']
 
 
 
 
376
 
377
  batch_to_send = []
378
+ current_id = last_checked_id + 1
379
+ search_limit = 50
380
+ highest_valid_id = last_checked_id
381
+
382
+ chunk_ids = list(range(current_id, current_id + search_limit))
383
+
384
+ try:
385
+ msgs = await client.get_messages(STORAGE_CHANNEL_ID, chunk_ids)
386
+ valid_messages_found = False
387
+
388
+ for msg in msgs:
389
+ if not auto_share_running: break
390
+ if msg and not getattr(msg, "empty", False):
391
+ valid_messages_found = True
392
+ highest_valid_id = msg.id
393
+
394
  if msg.video or (msg.document and msg.document.mime_type and "video" in msg.document.mime_type) or msg.photo:
395
  batch_to_send.append(msg)
 
396
  if len(batch_to_send) == 5:
397
  break
398
+ except FloodWait as e:
399
+ await asyncio.sleep(e.value + 1)
400
+ continue
401
+ except Exception as e:
402
+ print(f"Fetch error in auto share: {e}")
403
+ await asyncio.sleep(5)
404
+ continue
405
+
406
  if not batch_to_send:
407
+ if valid_messages_found and highest_valid_id > last_checked_id:
408
+ await save_progress(STORAGE_CHANNEL_ID, target_chat_id, highest_valid_id)
409
+ await asyncio.sleep(30)
410
  continue
411
 
412
+ last_sent_id = highest_valid_id
413
  sent_message_ids = []
414
+
415
  for msg in batch_to_send:
416
  if not auto_share_running: break
417
  try:
418
  sent = await msg.copy(target_chat_id)
419
  sent_message_ids.append(sent.id)
420
+ last_sent_id = msg.id
421
  await asyncio.sleep(1.5)
422
  except FloodWait as e:
423
  await asyncio.sleep(e.value + 1)
424
  except Exception as e:
425
  print(f"Error copying msg {msg.id}: {e}")
426
 
427
+ if last_sent_id > last_checked_id:
428
+ await save_progress(STORAGE_CHANNEL_ID, target_chat_id, last_sent_id)
429
 
430
  for _ in range(delay):
431
  if not auto_share_running: break
 
470
  await message.reply("πŸ›‘ <b>Auto-sharing stopped successfully!</b>", parse_mode=enums.ParseMode.HTML)
471
 
472
 
473
+ # ================= HELP COMMAND =================
474
+ @bot.on_message(filters.command("help"))
475
+ async def help_command(client, message):
476
+ user_id = message.from_user.id
477
+ is_admin = user_id in ADMIN_IDS
478
+
479
+ help_text = "πŸ›  **Bot Commands Help Menu**\n\n"
480
+
481
+ if is_admin:
482
+ help_text += "πŸ‘‘ **Admin Commands:**\n"
483
+ help_text += "πŸ‘‰ `/stats` - Check total users, videos, and groups.\n"
484
+ help_text += "πŸ‘‰ `/broadcast` - Send a message to all bot users.\n"
485
+ help_text += "πŸ‘‰ `/clone <source_id> <dest_id>` - Clone videos from one group to another.\n"
486
+ help_text += "πŸ‘‰ `/sendto <group_id>` - Reply to a media to forward it directly to a specific group.\n"
487
+ help_text += "πŸ‘‰ `/upload <telegram/byse>` - Change video upload server (Local or byse.sx).\n"
488
+ help_text += "πŸ‘‰ `/blur <percentage>` - Enable video/photo blur (e.g., `/blur 60`). Send `/blur 0` to disable.\n"
489
+ help_text += "πŸ‘‰ `/clean` - Scan and kick dead/deleted users from the Premium channel.\n"
490
+ help_text += "πŸ‘‰ `/startshare` - Start auto-forwarding 5 videos to the target group every 5 minutes.\n"
491
+ help_text += "πŸ‘‰ `/startshare reset` - Reset auto-forward progress to the first video.\n"
492
+ help_text += "πŸ‘‰ `/stopshare` - Stop the auto-forwarding process.\n\n"
493
+
494
+ help_text += "πŸ‘€ **User Commands:**\n"
495
+ help_text += "πŸ‘‰ `/start` - Start the bot and get the WebApp link.\n"
496
+
497
+ await message.reply(help_text, parse_mode=enums.ParseMode.MARKDOWN)
498
+ # ================================================
499
+
500
+
501
  # ================= TELEGRAM BOT COMMANDS =================
502
  @bot.on_message(filters.command("start"))
503
  async def start(client, message):
 
1196
  print(f"Auto clean error: {e}")
1197
  await asyncio.sleep(4 * 3600)
1198
 
1199
+ @bot.on_message(filters.private & filters.user(ADMIN_IDS) & ~filters.command(["start", "stats", "users", "broadcast", "png", "addvideo", "blur", "clean", "sendto", "clone", "upload", "startshare", "stopshare", "help"]))
1200
  async def catch_admin_steps(client, message):
1201
  state = admin_states.get(message.chat.id, {})
1202
  if state.get("step") == 1: