| import asyncio |
| import logging |
| import aiohttp |
| from pyrogram import Client, filters |
| from pyrogram.types import Message, InlineKeyboardMarkup, InlineKeyboardButton, WebAppInfo |
|
|
| from config import Config |
| from database import fetch_one, row_to_dict |
|
|
| logger = logging.getLogger("main_bot") |
|
|
| MINI_APP_URL = "https://popcornminisite.github.io/PopCornMiniSite/" |
|
|
| async def run_main_bot(): |
| if not Config.MAIN_BOT_TOKEN: |
| logger.warning("MAIN_BOT_TOKEN not set — main bot @PopCornMinibot disabled") |
| return |
|
|
| logger.info("Starting main bot @PopCornMinibot") |
| app = Client( |
| "main_bot", |
| api_id=Config.TELEGRAM_API_ID, |
| api_hash=Config.TELEGRAM_API_HASH, |
| bot_token=Config.MAIN_BOT_TOKEN, |
| workdir="/tmp", |
| ) |
|
|
| @app.on_message(filters.command("start")) |
| async def start(client: Client, message: Message): |
| keyboard = InlineKeyboardMarkup([ |
| [InlineKeyboardButton("🎬 فتح التطبيق", web_app=WebAppInfo(url=MINI_APP_URL))], |
| [InlineKeyboardButton("📡 قناة التحديثات", url="https://t.me/PopCornMini")], |
| ]) |
| await message.reply_text( |
| "🎬 *PopCorn — سينما مشفرة بالكامل*\n\n" |
| "مرحبًا بك في PopCorn! استمتع بمشاهدة الأفلام والمسلسلات\n" |
| "بدون إعلانات، بجودة عالية، وبتشفير كامل.\n\n" |
| "👇 اضغط على الزر لفتح التطبيق", |
| reply_markup=keyboard, |
| ) |
|
|
| @app.on_message(filters.command("help")) |
| async def help(client: Client, message: Message): |
| keyboard = InlineKeyboardMarkup([ |
| [InlineKeyboardButton("🎬 فتح التطبيق", web_app=WebAppInfo(url=MINI_APP_URL))], |
| ]) |
| await message.reply_text( |
| "*مساعدة PopCorn*\n\n" |
| "• /start — فتح التطبيق\n" |
| "• /help — هذه المساعدة\n" |
| "• /about — معلومات عن المشروع\n\n" |
| "للاستفسارات: @MlK_JAMAL", |
| reply_markup=keyboard, |
| ) |
|
|
| @app.on_message(filters.command("about")) |
| async def about(client: Client, message: Message): |
| await message.reply_text( |
| "*PopCorn v2.0*\n\n" |
| "منصة بث آمنة تستخدم التشفير من طرف إلى طرف\n" |
| "• تشفير AES-CBC لكل مقطع\n" |
| "• تحميل موازي مع فك التشفير في المتصفح\n" |
| "• بدون إعلانات • بدون تتبع\n\n" |
| "مشروع أكاديمي لأبحاث التشفير الوسائط المتعددة" |
| ) |
|
|
| @app.on_message(filters.command("profile")) |
| async def profile(client: Client, message: Message): |
| if not message.from_user: |
| return |
| tg_id = message.from_user.id |
| try: |
| user = await fetch_one("SELECT * FROM users WHERE user_id = ?", [tg_id]) |
| if not user: |
| await message.reply_text( |
| "❌ *حساب غير مسجل*\n\n" |
| "لم يتم العثور على حسابك. افتح التطبيق أولاً لتسجيل حسابك.\n" |
| "👇 اضغط على /start لفتح التطبيق" |
| ) |
| return |
| u = row_to_dict(user) |
| text = ( |
| f"👤 *الملف الشخصي*\n\n" |
| f"**الاسم:** {u.get('first_name', '')} {u.get('last_name', '')}\n" |
| f"**المعرف:** @{u.get('username', '—')}\n" |
| f"**النقاط:** {u.get('points', 0)}\n" |
| f"**وقت المشاهدة:** {u.get('total_watch_time', 0) // 3600} ساعة\n" |
| f"**⭐ رصيد النجوم:** {u.get('stars_balance', 0)}\n" |
| f"**🌰 رصيد الكيرنلز:** {u.get('kernels_balance', 0)}\n" |
| f"**تاريخ التسجيل:** {u.get('created_at', '—')}" |
| ) |
| await message.reply_text(text) |
| except Exception as e: |
| logger.error("Profile lookup failed: %s", e) |
| await message.reply_text("❌ حدث خطأ أثناء استرجاع الملف الشخصي.") |
|
|
| await app.start() |
|
|
| |
| try: |
| async with aiohttp.ClientSession() as sess: |
| async def _post(method: str, payload: dict) -> None: |
| async with sess.post( |
| f"https://api.telegram.org/bot{Config.MAIN_BOT_TOKEN}/{method}", |
| json=payload, timeout=aiohttp.ClientTimeout(total=10), |
| ) as resp: |
| data = await resp.json() |
| if not data.get("ok"): |
| logger.warning("TG API %s: %s", method, data.get("description", "unknown")) |
|
|
| await _post("setMyCommands", {"commands": [ |
| {"command": "start", "description": "Open PopCorn app"}, |
| {"command": "help", "description": "Help"}, |
| {"command": "about", "description": "About"}, |
| ]}) |
| await _post("setChatMenuButton", { |
| "menu_button": { |
| "type": "web_app", |
| "text": "Open PopCorn", |
| "web_app": {"url": MINI_APP_URL}, |
| } |
| }) |
| logger.info("Main bot commands and menu button configured") |
| except Exception as e: |
| logger.warning("Could not configure bot menu (HTTP API unreachable): %s", e) |
| logger.info("Bot menu will still work via /start command") |
|
|
| logger.info("Main bot @PopCornMinibot is running") |
| await asyncio.Event().wait() |
|
|