Update bot.py
Browse files
bot.py
CHANGED
|
@@ -3,8 +3,6 @@ import os
|
|
| 3 |
import logging
|
| 4 |
import random
|
| 5 |
import sys
|
| 6 |
-
from threading import Thread
|
| 7 |
-
from http.server import HTTPServer, SimpleHTTPRequestHandler
|
| 8 |
from telethon import TelegramClient
|
| 9 |
from telethon.sessions import StringSession
|
| 10 |
from telethon.tl.functions.channels import InviteToChannelRequest
|
|
@@ -14,26 +12,11 @@ from telethon.errors import (
|
|
| 14 |
UserChannelsTooMuchError, UserAlreadyParticipantError
|
| 15 |
)
|
| 16 |
|
| 17 |
-
#
|
| 18 |
-
class HealthHandler(SimpleHTTPRequestHandler):
|
| 19 |
-
def do_GET(self):
|
| 20 |
-
self.send_response(200)
|
| 21 |
-
self.send_header("Content-type", "text/html; charset=utf-8")
|
| 22 |
-
self.end_headers()
|
| 23 |
-
self.wfile.write("<h1>✅ البوت يعمل الآن في الخلفية...</h1><p>راجع سجلات الـ Logs لمتابعة التفاصيل.</p>".encode("utf-8"))
|
| 24 |
-
def log_message(self, format, *args): pass
|
| 25 |
-
|
| 26 |
-
def run_health_server():
|
| 27 |
-
server = HTTPServer(("0.0.0.0", 7860), HealthHandler)
|
| 28 |
-
server.serve_forever()
|
| 29 |
-
|
| 30 |
-
# تشغيل السيرفر في الخلفية
|
| 31 |
-
Thread(target=run_health_server, daemon=True).start()
|
| 32 |
-
|
| 33 |
-
# 2. إعدادات البوت واللوق
|
| 34 |
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
| 35 |
logger = logging.getLogger(__name__)
|
| 36 |
|
|
|
|
| 37 |
API_ID = int(os.getenv("API_ID", "0"))
|
| 38 |
API_HASH = os.getenv("API_HASH", "")
|
| 39 |
SESSION_STRING = os.getenv("SESSION_STRING") or os.getenv("STRING_SESSION") or ""
|
|
@@ -42,89 +25,68 @@ TARGET_CHANNEL = os.getenv("TARGET_CHANNEL", "")
|
|
| 42 |
LIMIT = int(os.getenv("LIMIT", "50"))
|
| 43 |
BASE_DELAY = int(os.getenv("DELAY_SECONDS", "300"))
|
| 44 |
|
| 45 |
-
async def get_current_members(client, target_channel):
|
| 46 |
-
logger.info("🔍 جاري فحص أعضاء جروبك الحاليين...")
|
| 47 |
-
current_members = set()
|
| 48 |
-
try:
|
| 49 |
-
target = await client.get_entity(target_channel)
|
| 50 |
-
async for member in client.iter_participants(target):
|
| 51 |
-
current_members.add(member.id)
|
| 52 |
-
logger.info(f"✅ تم العثور على {len(current_members)} عضو في جروبك.")
|
| 53 |
-
except Exception as e:
|
| 54 |
-
logger.warning(f"⚠️ فشل فحص الأعضاء الحاليين: {e}")
|
| 55 |
-
return current_members
|
| 56 |
-
|
| 57 |
async def start_transfer():
|
| 58 |
-
logger.info("🚀 بد
|
| 59 |
|
| 60 |
-
if not SESSION_STRING:
|
| 61 |
-
logger.error("❌ خطأ: SESSION_STRING غير موجودة في الـ Secrets!")
|
| 62 |
-
return
|
| 63 |
-
|
| 64 |
client = TelegramClient(StringSession(SESSION_STRING.strip()), API_ID, API_HASH)
|
| 65 |
|
| 66 |
try:
|
| 67 |
await client.connect()
|
| 68 |
if not await client.is_user_authorized():
|
| 69 |
-
logger.error("❌
|
| 70 |
return
|
| 71 |
|
| 72 |
source = await client.get_entity(SOURCE_CHANNEL)
|
| 73 |
target = await client.get_entity(TARGET_CHANNEL)
|
| 74 |
|
| 75 |
-
#
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
# جلب الأعضاء للفلترة
|
| 79 |
-
participants = await client.get_participants(source, limit=LIMIT * 5)
|
| 80 |
random.shuffle(participants)
|
| 81 |
|
| 82 |
success, processed = 0, 0
|
|
|
|
| 83 |
for member in participants:
|
| 84 |
if processed >= LIMIT: break
|
| 85 |
-
|
| 86 |
-
# فلترة: بوتات، محذوفين، أو موجودين أصلاً
|
| 87 |
-
if member.bot or member.deleted or member.id in already_in:
|
| 88 |
-
continue
|
| 89 |
-
|
| 90 |
-
# فلترة النشطين فقط
|
| 91 |
-
if not isinstance(member.status, (UserStatusOnline, UserStatusRecently)):
|
| 92 |
-
continue
|
| 93 |
|
| 94 |
processed += 1
|
| 95 |
name = member.first_name or "User"
|
| 96 |
-
logger.info(f"[{processed}/{LIMIT}] محاولة إضافة: {name}")
|
| 97 |
|
| 98 |
try:
|
|
|
|
| 99 |
await client(InviteToChannelRequest(target, [InputPeerUser(member.id, member.access_hash or 0)]))
|
| 100 |
success += 1
|
| 101 |
-
logger.info(f"✅ ت
|
| 102 |
-
|
| 103 |
-
delay = BASE_DELAY + random.randint(10, 60)
|
| 104 |
-
logger.info(f"⏳ انتظار {delay} ثانية...")
|
| 105 |
-
await asyncio.sleep(delay)
|
| 106 |
|
| 107 |
-
except FloodWaitError as e:
|
| 108 |
-
logger.warning(f"⏳ فلود من تيليجرام! انتظار {e.seconds} ثانية.")
|
| 109 |
-
await asyncio.sleep(e.seconds + 5)
|
| 110 |
except PeerFloodError:
|
| 111 |
-
|
| 112 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
except UserPrivacyRestrictedError:
|
| 114 |
-
logger.info(f"🔒 خصوصية
|
| 115 |
except Exception as e:
|
| 116 |
-
logger.info(f"❌
|
| 117 |
|
| 118 |
-
logger.info(f"🏁 انته
|
| 119 |
|
| 120 |
except Exception as e:
|
| 121 |
-
logger.error(f"❌ خطأ
|
| 122 |
finally:
|
| 123 |
await client.disconnect()
|
| 124 |
|
| 125 |
if __name__ == "__main__":
|
| 126 |
-
# تشغيل
|
| 127 |
-
|
| 128 |
asyncio.run(start_transfer())
|
| 129 |
-
|
| 130 |
-
|
|
|
|
|
|
| 3 |
import logging
|
| 4 |
import random
|
| 5 |
import sys
|
|
|
|
|
|
|
| 6 |
from telethon import TelegramClient
|
| 7 |
from telethon.sessions import StringSession
|
| 8 |
from telethon.tl.functions.channels import InviteToChannelRequest
|
|
|
|
| 12 |
UserChannelsTooMuchError, UserAlreadyParticipantError
|
| 13 |
)
|
| 14 |
|
| 15 |
+
# إعداد اللوق
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
| 17 |
logger = logging.getLogger(__name__)
|
| 18 |
|
| 19 |
+
# المتغيرات
|
| 20 |
API_ID = int(os.getenv("API_ID", "0"))
|
| 21 |
API_HASH = os.getenv("API_HASH", "")
|
| 22 |
SESSION_STRING = os.getenv("SESSION_STRING") or os.getenv("STRING_SESSION") or ""
|
|
|
|
| 25 |
LIMIT = int(os.getenv("LIMIT", "50"))
|
| 26 |
BASE_DELAY = int(os.getenv("DELAY_SECONDS", "300"))
|
| 27 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
async def start_transfer():
|
| 29 |
+
logger.info("🚀 بدأت المحاولة باستخدام الكود المحدث (تخطي الحظر)...")
|
| 30 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
client = TelegramClient(StringSession(SESSION_STRING.strip()), API_ID, API_HASH)
|
| 32 |
|
| 33 |
try:
|
| 34 |
await client.connect()
|
| 35 |
if not await client.is_user_authorized():
|
| 36 |
+
logger.error("❌ الجلسة غير صالحة!")
|
| 37 |
return
|
| 38 |
|
| 39 |
source = await client.get_entity(SOURCE_CHANNEL)
|
| 40 |
target = await client.get_entity(TARGET_CHANNEL)
|
| 41 |
|
| 42 |
+
# جلب الأعضاء
|
| 43 |
+
participants = await client.get_participants(source, limit=500)
|
|
|
|
|
|
|
|
|
|
| 44 |
random.shuffle(participants)
|
| 45 |
|
| 46 |
success, processed = 0, 0
|
| 47 |
+
|
| 48 |
for member in participants:
|
| 49 |
if processed >= LIMIT: break
|
| 50 |
+
if member.bot or member.deleted: continue
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
processed += 1
|
| 53 |
name = member.first_name or "User"
|
| 54 |
+
logger.info(f"[{processed}/{LIMIT}] محاولة إجبارية لإضافة: {name}")
|
| 55 |
|
| 56 |
try:
|
| 57 |
+
# محاولة الإضافة
|
| 58 |
await client(InviteToChannelRequest(target, [InputPeerUser(member.id, member.access_hash or 0)]))
|
| 59 |
success += 1
|
| 60 |
+
logger.info(f"✅ نجحت إضافة {name}!")
|
| 61 |
+
await asyncio.sleep(BASE_DELAY)
|
|
|
|
|
|
|
|
|
|
| 62 |
|
|
|
|
|
|
|
|
|
|
| 63 |
except PeerFloodError:
|
| 64 |
+
# 🛠 هنا التعديل: بدلاً من التوقف (Break)، سنقوم بالانتظار والتخطي (Continue)
|
| 65 |
+
logger.warning(f"🚨 تيليجرام رفض إضافة {name} (Flood). سأتخطاه وأجرب العضو التالي بعد 30 ثانية...")
|
| 66 |
+
await asyncio.sleep(30)
|
| 67 |
+
continue
|
| 68 |
+
|
| 69 |
+
except UserAlreadyParticipantError:
|
| 70 |
+
logger.info(f"⏭ {name} موجود بالفعل.")
|
| 71 |
+
except FloodWaitError as e:
|
| 72 |
+
logger.warning(f"⏳ انتظار إجباري {e.seconds} ثانية.")
|
| 73 |
+
await asyncio.sleep(e.seconds + 2)
|
| 74 |
except UserPrivacyRestrictedError:
|
| 75 |
+
logger.info(f"🔒 خصوصية {name} تمنع الإضافة.")
|
| 76 |
except Exception as e:
|
| 77 |
+
logger.info(f"❌ فشل مع {name}: {type(e).__name__}")
|
| 78 |
|
| 79 |
+
logger.info(f"🏁 انتهت الدورة. تم إضافة {success} أعضاء.")
|
| 80 |
|
| 81 |
except Exception as e:
|
| 82 |
+
logger.error(f"❌ خطأ نظام: {e}")
|
| 83 |
finally:
|
| 84 |
await client.disconnect()
|
| 85 |
|
| 86 |
if __name__ == "__main__":
|
| 87 |
+
# تشغيل مستمر
|
| 88 |
+
while True:
|
| 89 |
asyncio.run(start_transfer())
|
| 90 |
+
logger.info("💤 انتظار 10 دقائق قبل دورة عشوائية جديدة...")
|
| 91 |
+
import time
|
| 92 |
+
time.sleep(600)
|