Update bot.py
Browse files
bot.py
CHANGED
|
@@ -9,6 +9,13 @@ from pyrogram import Client, filters
|
|
| 9 |
from pyrogram.errors import FloodWait, RPCError
|
| 10 |
from pyrogram.types import InputMediaPhoto, InputMediaVideo, InputMediaDocument, InputMediaAudio
|
| 11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
logging.basicConfig(
|
| 13 |
level=logging.INFO,
|
| 14 |
format='%(asctime)s - %(levelname)s - %(message)s'
|
|
@@ -38,26 +45,27 @@ app = Client(
|
|
| 38 |
lang_code="ar"
|
| 39 |
)
|
| 40 |
|
| 41 |
-
# متغير عام للقناة الهدف الفعلية (قد تتغير بعد التحقق)
|
| 42 |
TARGET_CHAT = None
|
| 43 |
|
| 44 |
-
# ============ معرفة نوع الملف ============
|
| 45 |
def get_media_info(message):
|
| 46 |
if message.photo:
|
| 47 |
-
return "photo",
|
| 48 |
elif message.video:
|
| 49 |
-
return "video",
|
| 50 |
elif message.document:
|
| 51 |
-
|
|
|
|
| 52 |
elif message.audio:
|
| 53 |
-
|
|
|
|
| 54 |
elif message.voice:
|
| 55 |
-
return "voice",
|
| 56 |
elif message.video_note:
|
| 57 |
-
return "video_note",
|
| 58 |
elif message.animation:
|
| 59 |
-
return "animation",
|
| 60 |
-
return None,
|
| 61 |
|
| 62 |
# ============ اسم المصدر ============
|
| 63 |
def get_source_name(message):
|
|
@@ -89,7 +97,7 @@ async def flush_media_group(chat_id, mg_id):
|
|
| 89 |
|
| 90 |
downloaded = []
|
| 91 |
for i, msg in enumerate(messages):
|
| 92 |
-
m_type,
|
| 93 |
if not m_type:
|
| 94 |
continue
|
| 95 |
|
|
@@ -98,10 +106,11 @@ async def flush_media_group(chat_id, mg_id):
|
|
| 98 |
final_cap = f"{cap}\n\n📌 المصدر: {src_name}".strip() if cap else f"📌 المصدر: {src_name}"
|
| 99 |
|
| 100 |
try:
|
| 101 |
-
|
|
|
|
| 102 |
if file_path and os.path.exists(file_path) and os.path.getsize(file_path) > 0:
|
| 103 |
downloaded.append((m_type, file_path, final_cap))
|
| 104 |
-
logger.info(f"⬇️ تم التنزيل: {msg.id}")
|
| 105 |
else:
|
| 106 |
logger.warning(f"⚠️ فشل تنزيل: {msg.id}")
|
| 107 |
except Exception as e:
|
|
@@ -174,16 +183,18 @@ async def on_media(client, message):
|
|
| 174 |
media_groups[key].append(message)
|
| 175 |
return
|
| 176 |
|
| 177 |
-
m_type,
|
| 178 |
if not m_type:
|
| 179 |
return
|
| 180 |
|
|
|
|
| 181 |
raw_cap = message.caption or ""
|
| 182 |
final_cap = f"{raw_cap}\n\n📌 المصدر: {src_name}".strip() if raw_cap else f"📌 المصدر: {src_name}"
|
| 183 |
|
| 184 |
file_path = None
|
| 185 |
try:
|
| 186 |
-
|
|
|
|
| 187 |
if not file_path or not os.path.exists(file_path) or os.path.getsize(file_path) == 0:
|
| 188 |
logger.warning(f"⚠️ فشل تنزيل الرسالة {message.id}")
|
| 189 |
return
|
|
@@ -239,7 +250,6 @@ async def resolve_target_chat():
|
|
| 239 |
return
|
| 240 |
|
| 241 |
try:
|
| 242 |
-
# جرب كـ integer أولاً
|
| 243 |
chat_id_int = int(raw)
|
| 244 |
chat = await app.get_chat(chat_id_int)
|
| 245 |
TARGET_CHAT = chat_id_int
|
|
@@ -269,10 +279,7 @@ def run_gradio():
|
|
| 269 |
| `API_ID` | رقم | `12345678` |
|
| 270 |
| `API_HASH` | نص | `abc123...` |
|
| 271 |
| `SESSION_STRING` | نص | `BQAD...` |
|
| 272 |
-
| `TARGET_CHAT` | معرف القناة الخاصة | `-
|
| 273 |
-
|
| 274 |
-
### ⚠️ ملاحظة مهمة للقنوات الخاصة:
|
| 275 |
-
الحساب لازم يكون **عضو** في القناة الخاصة. إذا فشل التحقق، يرسل إلى Saved Messages.
|
| 276 |
""")
|
| 277 |
|
| 278 |
demo.launch(server_name="0.0.0.0", server_port=7860)
|
|
@@ -282,7 +289,6 @@ async def main():
|
|
| 282 |
await app.start()
|
| 283 |
me = await app.get_me()
|
| 284 |
|
| 285 |
-
# 🔑 التحقق من القناة الهدف
|
| 286 |
await resolve_target_chat()
|
| 287 |
|
| 288 |
logger.info("=" * 50)
|
|
|
|
| 9 |
from pyrogram.errors import FloodWait, RPCError
|
| 10 |
from pyrogram.types import InputMediaPhoto, InputMediaVideo, InputMediaDocument, InputMediaAudio
|
| 11 |
|
| 12 |
+
# ============ إصلاح MIN_CHANNEL_ID في Pyrogram ============
|
| 13 |
+
# القنوات الجديدة لها معرفات < -1002147483647
|
| 14 |
+
# المصدر: https://github.com/pyrogram/pyrogram/pull/1430
|
| 15 |
+
import pyrogram
|
| 16 |
+
pyrogram.utils.MIN_CHANNEL_ID = -1007852516352
|
| 17 |
+
pyrogram.utils.MIN_CHAT_ID = -999999999999
|
| 18 |
+
|
| 19 |
logging.basicConfig(
|
| 20 |
level=logging.INFO,
|
| 21 |
format='%(asctime)s - %(levelname)s - %(message)s'
|
|
|
|
| 45 |
lang_code="ar"
|
| 46 |
)
|
| 47 |
|
|
|
|
| 48 |
TARGET_CHAT = None
|
| 49 |
|
| 50 |
+
# ============ معرفة نوع الملف + الامتداد ============
|
| 51 |
def get_media_info(message):
|
| 52 |
if message.photo:
|
| 53 |
+
return "photo", ".jpg"
|
| 54 |
elif message.video:
|
| 55 |
+
return "video", ".mp4"
|
| 56 |
elif message.document:
|
| 57 |
+
ext = os.path.splitext(message.document.file_name or "")[1] or ""
|
| 58 |
+
return "doc", ext
|
| 59 |
elif message.audio:
|
| 60 |
+
ext = os.path.splitext(message.audio.file_name or "")[1] or ".mp3"
|
| 61 |
+
return "audio", ext
|
| 62 |
elif message.voice:
|
| 63 |
+
return "voice", ".ogg"
|
| 64 |
elif message.video_note:
|
| 65 |
+
return "video_note", ".mp4"
|
| 66 |
elif message.animation:
|
| 67 |
+
return "animation", ".mp4"
|
| 68 |
+
return None, ""
|
| 69 |
|
| 70 |
# ============ اسم المصدر ============
|
| 71 |
def get_source_name(message):
|
|
|
|
| 97 |
|
| 98 |
downloaded = []
|
| 99 |
for i, msg in enumerate(messages):
|
| 100 |
+
m_type, ext = get_media_info(msg)
|
| 101 |
if not m_type:
|
| 102 |
continue
|
| 103 |
|
|
|
|
| 106 |
final_cap = f"{cap}\n\n📌 المصدر: {src_name}".strip() if cap else f"📌 المصدر: {src_name}"
|
| 107 |
|
| 108 |
try:
|
| 109 |
+
# 🔑 إضافة الامتداد لتجنب PHOTO_EXT_INVALID
|
| 110 |
+
file_path = await msg.download(file_name=f"{DOWNLOAD_DIR}/{chat_id}_{msg.id}{ext}")
|
| 111 |
if file_path and os.path.exists(file_path) and os.path.getsize(file_path) > 0:
|
| 112 |
downloaded.append((m_type, file_path, final_cap))
|
| 113 |
+
logger.info(f"⬇️ تم التنزيل: {msg.id} -> {os.path.basename(file_path)}")
|
| 114 |
else:
|
| 115 |
logger.warning(f"⚠️ فشل تنزيل: {msg.id}")
|
| 116 |
except Exception as e:
|
|
|
|
| 183 |
media_groups[key].append(message)
|
| 184 |
return
|
| 185 |
|
| 186 |
+
m_type, ext = get_media_info(message)
|
| 187 |
if not m_type:
|
| 188 |
return
|
| 189 |
|
| 190 |
+
src_name = get_source_name(message)
|
| 191 |
raw_cap = message.caption or ""
|
| 192 |
final_cap = f"{raw_cap}\n\n📌 المصدر: {src_name}".strip() if raw_cap else f"📌 المصدر: {src_name}"
|
| 193 |
|
| 194 |
file_path = None
|
| 195 |
try:
|
| 196 |
+
# 🔑 إضافة الامتداد لتجنب PHOTO_EXT_INVALID
|
| 197 |
+
file_path = await message.download(file_name=f"{DOWNLOAD_DIR}/{chat_id}_{message.id}{ext}")
|
| 198 |
if not file_path or not os.path.exists(file_path) or os.path.getsize(file_path) == 0:
|
| 199 |
logger.warning(f"⚠️ فشل تنزيل الرسالة {message.id}")
|
| 200 |
return
|
|
|
|
| 250 |
return
|
| 251 |
|
| 252 |
try:
|
|
|
|
| 253 |
chat_id_int = int(raw)
|
| 254 |
chat = await app.get_chat(chat_id_int)
|
| 255 |
TARGET_CHAT = chat_id_int
|
|
|
|
| 279 |
| `API_ID` | رقم | `12345678` |
|
| 280 |
| `API_HASH` | نص | `abc123...` |
|
| 281 |
| `SESSION_STRING` | نص | `BQAD...` |
|
| 282 |
+
| `TARGET_CHAT` | معرف القناة الخاصة | `-1004307601544` |
|
|
|
|
|
|
|
|
|
|
| 283 |
""")
|
| 284 |
|
| 285 |
demo.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
| 289 |
await app.start()
|
| 290 |
me = await app.get_me()
|
| 291 |
|
|
|
|
| 292 |
await resolve_target_chat()
|
| 293 |
|
| 294 |
logger.info("=" * 50)
|