pmrony commited on
Commit
dd2cce4
·
verified ·
1 Parent(s): 404a901

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -15
app.py CHANGED
@@ -5,9 +5,10 @@ import requests
5
  import asyncio
6
  import re
7
  import random
 
8
  from flask import Flask, jsonify, make_response, request
9
  from supabase import create_client
10
- from pyrogram import Client, filters, enums
11
  from pyrogram.types import InlineKeyboardMarkup, InlineKeyboardButton, WebAppInfo
12
 
13
  # ================= CONFIGURATION =================
@@ -36,19 +37,19 @@ bot = Client(
36
  bot_token=BOT_TOKEN
37
  )
38
 
39
- # === ম্যাজিক ফিক্স: বটের মাধযমে সাসরি মেসে পাঠানোর কিউ সিস্টেম ===
40
- send_queue = []
41
 
42
  async def background_worker():
43
  while True:
44
- if send_queue:
45
- msg = send_queue.pop(0)
46
  try:
47
  # সরাসরি বটের নিজস্ব কানেকশন দিয়ে মেসেজ পাঠানো হচ্ছে
48
  await bot.send_message(chat_id=int(msg['chat_id']), text=msg['text'], parse_mode=enums.ParseMode.HTML)
49
  except Exception as e:
50
  print("Error sending OTP via MTProto:", e)
51
- await asyncio.sleep(0.5)
52
  # ==========================================================
53
 
54
  async def db_query(func):
@@ -94,8 +95,8 @@ def api_send_code():
94
 
95
  text_msg = f"🔐 আপনার Video Unlocker Pro এর Verification Code হচ্ছে: <b>{otp}</b>\n\nঅনুগ্রহ করে এটি কারো সাথে শেয়ার করবেন না।"
96
 
97
- # মেসেজটি কিউতে যোগ করা হলো (যাতে ব্যাকগ্রাউন্ড ওয়ার্কার এটি বটের মাধ্যমে পাঠাতে পারে)
98
- send_queue.append({"chat_id": user_id, "text": text_msg})
99
 
100
  return add_cors_headers(make_response(jsonify({"status": "ok", "hash": hash_val})))
101
 
@@ -452,12 +453,13 @@ async def catch_admin_steps(client, message):
452
 
453
  def run_flask(): app.run(host="0.0.0.0", port=7860)
454
 
 
 
 
 
 
 
 
455
  if __name__ == "__main__":
456
  threading.Thread(target=run_flask, daemon=True).start()
457
- print("🤖 Pyrogram Bot is starting...")
458
-
459
- # ব্যাকগ্রাউন্ড ওয়ার্কার চালু করা হলো যা কিউ থেকে মেসেজ নিয়ে বটের মাধ্যমে সেন্ড করবে
460
- loop = asyncio.get_event_loop()
461
- loop.create_task(background_worker())
462
-
463
- bot.run()
 
5
  import asyncio
6
  import re
7
  import random
8
+ import queue
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.types import InlineKeyboardMarkup, InlineKeyboardButton, WebAppInfo
13
 
14
  # ================= CONFIGURATION =================
 
37
  bot_token=BOT_TOKEN
38
  )
39
 
40
+ # === ম্যাজিক ফিক্স: ্রেড-সে কিউ সিস্টেম (Thread-safe Queue) ===
41
+ send_queue = queue.Queue()
42
 
43
  async def background_worker():
44
  while True:
45
+ while not send_queue.empty():
46
+ msg = send_queue.get()
47
  try:
48
  # সরাসরি বটের নিজস্ব কানেকশন দিয়ে মেসেজ পাঠানো হচ্ছে
49
  await bot.send_message(chat_id=int(msg['chat_id']), text=msg['text'], parse_mode=enums.ParseMode.HTML)
50
  except Exception as e:
51
  print("Error sending OTP via MTProto:", e)
52
+ await asyncio.sleep(1)
53
  # ==========================================================
54
 
55
  async def db_query(func):
 
95
 
96
  text_msg = f"🔐 আপনার Video Unlocker Pro এর Verification Code হচ্ছে: <b>{otp}</b>\n\nঅনুগ্রহ করে এটি কারো সাথে শেয়ার করবেন না।"
97
 
98
+ # মেসেজটি কিউতে যোগ করা হলো
99
+ send_queue.put({"chat_id": user_id, "text": text_msg})
100
 
101
  return add_cors_headers(make_response(jsonify({"status": "ok", "hash": hash_val})))
102
 
 
453
 
454
  def run_flask(): app.run(host="0.0.0.0", port=7860)
455
 
456
+ async def main():
457
+ await bot.start()
458
+ print("🤖 Pyrogram Bot and Background Worker is running!")
459
+ asyncio.create_task(background_worker())
460
+ await idle()
461
+ await bot.stop()
462
+
463
  if __name__ == "__main__":
464
  threading.Thread(target=run_flask, daemon=True).start()
465
+ bot.run(main())