pmrony commited on
Commit
ae7cadb
·
verified ·
1 Parent(s): db8a350

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -15
app.py CHANGED
@@ -11,7 +11,6 @@ SUPABASE_URL = "https://yctirvnryrzygoxbpvoy.supabase.co"
11
  SUPABASE_KEY = "sb_publishable_aBcD-atruskWwoCiLr0lWw_inT8GLoN"
12
  WEB_APP_URL = "https://rony90790.github.io/Forward-bot/index.html"
13
 
14
- # আপনার Hugging Face এর লিংক
15
  HF_SPACE_URL = "https://pmrony-forwardbot.hf.space"
16
 
17
  CHANNEL_USERNAME = "xmlcpg"
@@ -22,24 +21,26 @@ bot = telebot.TeleBot(BOT_TOKEN)
22
  supabase = create_client(SUPABASE_URL, SUPABASE_KEY)
23
  app = Flask(__name__)
24
 
 
 
 
25
  # ==========================================
26
- # WEBHOOK SETUP (NO TIMEOUT)
27
  # ==========================================
28
 
29
- # টেলিগ্রাম থেকে ডাটা রিসিভ করার রুট
30
  @app.route(f'/{BOT_TOKEN}', methods=['POST'])
31
  def webhook():
32
- if request.headers.get('content-type') == 'application/json':
 
33
  json_string = request.get_data().decode('utf-8')
34
  update = telebot.types.Update.de_json(json_string)
35
  bot.process_new_updates([update])
36
  return "OK", 200
37
  return "Forbidden", 403
38
 
39
- # Webhook সেট করার রুট (আপনার ব্রাউজার থেকে হিট করতে হবে)
40
  @app.route('/set_webhook')
41
  def set_webhook():
42
- bot.remove_webhook() # আগের পোলিং ডিলিট করবে
43
  success = bot.set_webhook(url=f"{HF_SPACE_URL}/{BOT_TOKEN}")
44
  if success:
45
  return f"<h1>✅ Webhook Successfully Set to Hugging Face!</h1><p>আপনার বট এখন আর টাইম-আউট হবে না।</p>"
@@ -55,6 +56,12 @@ def index():
55
  # ==========================================
56
 
57
  def get_telegram_thumbnail(channel, msg_id):
 
 
 
 
 
 
58
  try:
59
  url = f"https://t.me/{channel}/{msg_id}?embed=1"
60
  req = urllib.request.Request(url, headers={'User-Agent': 'Mozilla/5.0'})
@@ -63,10 +70,15 @@ def get_telegram_thumbnail(channel, msg_id):
63
  if match:
64
  img_url = match.group(1)
65
  if "telegram_logo" not in img_url:
 
66
  return img_url
67
  except Exception:
68
  pass
69
- return f"https://via.placeholder.com/150/222222/FF9900?text=Video+{msg_id}"
 
 
 
 
70
 
71
  @app.route('/api/videos')
72
  def api_videos():
@@ -88,27 +100,33 @@ def start(message):
88
 
89
  referrer_id = None
90
  if len(text) > 1:
91
- referrer_id = text[1]
 
 
 
 
92
 
93
  try:
94
  user_check = supabase.table('referrals').select('*').eq('user_id', user_id).execute()
95
 
96
- if len(user_check.data) == 0:
 
97
  supabase.table('referrals').insert({
98
  'user_id': user_id,
99
  'referral_count': 0,
100
- 'referrer_id': referrer_id if referrer_id != str(user_id) else None
101
  }).execute()
102
 
103
- if referrer_id and referrer_id != str(user_id):
 
104
  referrer_data = supabase.table('referrals').select('referral_count').eq('user_id', referrer_id).execute()
105
- if len(referrer_data.data) > 0:
106
  new_count = referrer_data.data[0]['referral_count'] + 1
107
  supabase.table('referrals').update({'referral_count': new_count}).eq('user_id', referrer_id).execute()
108
 
109
  try:
110
  bot.send_message(referrer_id, "🎉 একজন আপনার লিংকে জয়েন করেছে! আপনার রেফারেল কাউন্ট বাড়লো।")
111
- except Exception as e:
112
  pass
113
  except Exception as e:
114
  print("Database Error:", e)
@@ -118,5 +136,5 @@ def start(message):
118
  bot.reply_to(message, "ভাইরাল ভিডিও দেখতে নিচের বাটনে ক্লিক করো 👇", reply_markup=markup)
119
 
120
  if __name__ == "__main__":
121
- # এআর infinity_polling() নে, পুোটখন Webhook চলবে!
122
- app.run(host="0.0.0.0", port=7860)
 
11
  SUPABASE_KEY = "sb_publishable_aBcD-atruskWwoCiLr0lWw_inT8GLoN"
12
  WEB_APP_URL = "https://rony90790.github.io/Forward-bot/index.html"
13
 
 
14
  HF_SPACE_URL = "https://pmrony-forwardbot.hf.space"
15
 
16
  CHANNEL_USERNAME = "xmlcpg"
 
21
  supabase = create_client(SUPABASE_URL, SUPABASE_KEY)
22
  app = Flask(__name__)
23
 
24
+ # ইন-মেমরি ক্যাশে (যাতে বারবার টেলিগ্রাম পেজ স্ক্র্যাপ করে স্পেস স্লো না হয়)
25
+ thumbnail_cache = {}
26
+
27
  # ==========================================
28
+ # WEBHOOK SETUP
29
  # ==========================================
30
 
 
31
  @app.route(f'/{BOT_TOKEN}', methods=['POST'])
32
  def webhook():
33
+ # Content-type একটু ফ্লেক্সিবল করা হলো টেলিগ্রামের জন্য
34
+ if 'application/json' in request.headers.get('content-type', ''):
35
  json_string = request.get_data().decode('utf-8')
36
  update = telebot.types.Update.de_json(json_string)
37
  bot.process_new_updates([update])
38
  return "OK", 200
39
  return "Forbidden", 403
40
 
 
41
  @app.route('/set_webhook')
42
  def set_webhook():
43
+ bot.remove_webhook()
44
  success = bot.set_webhook(url=f"{HF_SPACE_URL}/{BOT_TOKEN}")
45
  if success:
46
  return f"<h1>✅ Webhook Successfully Set to Hugging Face!</h1><p>আপনার বট এখন আর টাইম-আউট হবে না।</p>"
 
56
  # ==========================================
57
 
58
  def get_telegram_thumbnail(channel, msg_id):
59
+ cache_key = f"{channel}_{msg_id}"
60
+
61
+ # ক্যাশে থাকলে দ্রুত রিটার্ন করবে (API ফাস্ট হবে)
62
+ if cache_key in thumbnail_cache:
63
+ return thumbnail_cache[cache_key]
64
+
65
  try:
66
  url = f"https://t.me/{channel}/{msg_id}?embed=1"
67
  req = urllib.request.Request(url, headers={'User-Agent': 'Mozilla/5.0'})
 
70
  if match:
71
  img_url = match.group(1)
72
  if "telegram_logo" not in img_url:
73
+ thumbnail_cache[cache_key] = img_url
74
  return img_url
75
  except Exception:
76
  pass
77
+
78
+ # কোনো ছবি না পেলে ডামি ছবি
79
+ fallback = f"https://via.placeholder.com/150/222222/FF9900?text=Video+{msg_id}"
80
+ thumbnail_cache[cache_key] = fallback
81
+ return fallback
82
 
83
  @app.route('/api/videos')
84
  def api_videos():
 
100
 
101
  referrer_id = None
102
  if len(text) > 1:
103
+ try:
104
+ # Type error এড়াতে int এ কনভার্ট
105
+ referrer_id = int(text[1])
106
+ except ValueError:
107
+ referrer_id = None
108
 
109
  try:
110
  user_check = supabase.table('referrals').select('*').eq('user_id', user_id).execute()
111
 
112
+ # যদি ডাটাবেজে ইউজার না থাকে
113
+ if not user_check.data:
114
  supabase.table('referrals').insert({
115
  'user_id': user_id,
116
  'referral_count': 0,
117
+ 'referrer_id': referrer_id if referrer_id != user_id else None
118
  }).execute()
119
 
120
+ # যদি নতুন ইউজার কোনো রেফারের মাধ্যমে জয়েন করে
121
+ if referrer_id and referrer_id != user_id:
122
  referrer_data = supabase.table('referrals').select('referral_count').eq('user_id', referrer_id).execute()
123
+ if referrer_data.data:
124
  new_count = referrer_data.data[0]['referral_count'] + 1
125
  supabase.table('referrals').update({'referral_count': new_count}).eq('user_id', referrer_id).execute()
126
 
127
  try:
128
  bot.send_message(referrer_id, "🎉 একজন আপনার লিংকে জয়েন করেছে! আপনার রেফারেল কাউন্ট বাড়লো।")
129
+ except Exception:
130
  pass
131
  except Exception as e:
132
  print("Database Error:", e)
 
136
  bot.reply_to(message, "ভাইরাল ভিডিও দেখতে নিচের বাটনে ক্লিক করো 👇", reply_markup=markup)
137
 
138
  if __name__ == "__main__":
139
+ # Threaded=True দিলে কই সএকাধিকউজা ওয়েবঅ্যবং বট ব্যবহার করতে পারবে
140
+ app.run(host="0.0.0.0", port=7860, threaded=True)