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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -24
app.py CHANGED
@@ -1,8 +1,7 @@
1
  import telebot
2
  from telebot.types import InlineKeyboardMarkup, InlineKeyboardButton, WebAppInfo
3
  import os
4
- import threading
5
- from flask import Flask, jsonify, make_response
6
  from supabase import create_client
7
  import urllib.request
8
  import re
@@ -12,17 +11,49 @@ SUPABASE_URL = "https://yctirvnryrzygoxbpvoy.supabase.co"
12
  SUPABASE_KEY = "sb_publishable_aBcD-atruskWwoCiLr0lWw_inT8GLoN"
13
  WEB_APP_URL = "https://rony90790.github.io/Forward-bot/index.html"
14
 
15
- # ========= আপনার ম্যাজিক সেটিং (খানেই সব কন্ট্োল) =========
16
- CHANNEL_USERNAME = "xmlcpg" # আপনার চ্যানেলের ইউজারনেম দিন (t.me/ ছাড়া)
17
- START_MESSAGE_ID = 1 # কতো নাম্বার মেসেজ থেকে ভিডিও দেখানো শুরু করবে? (আপনি ১ বলেছেন, তাই ১ দিলাম)
18
- TOTAL_VIDEOS = 10 # একসাথে কয়টি ভিডিও অ্যাপে দেখাবে? (যেমন: ১০টি ভিডিও দেখাবে)
19
- # =========================================================
 
20
 
21
  bot = telebot.TeleBot(BOT_TOKEN)
22
  supabase = create_client(SUPABASE_URL, SUPABASE_KEY)
23
  app = Flask(__name__)
24
 
25
- # টেলিগ্রাম থেকে অটোমেটিক আসল ছবি (thumbnail) বের করার ফাংশন
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26
  def get_telegram_thumbnail(channel, msg_id):
27
  try:
28
  url = f"https://t.me/{channel}/{msg_id}?embed=1"
@@ -31,35 +62,25 @@ def get_telegram_thumbnail(channel, msg_id):
31
  match = re.search(r'<meta property="og:image" content="([^"]+)">', html)
32
  if match:
33
  img_url = match.group(1)
34
- # যদি আসল ছবি থাকে তবেই সেটি নেবে
35
  if "telegram_logo" not in img_url:
36
  return img_url
37
- except Exception as e:
38
  pass
39
- # ছবি না পেলে বা ডিলিট হয়ে গেলে একটি ডেমো ছবি দেখাবে
40
  return f"https://via.placeholder.com/150/222222/FF9900?text=Video+{msg_id}"
41
 
42
- @app.route('/')
43
- def index():
44
- return "Bot is Running!"
45
-
46
- # অ্যাপের জন্য অটোমেটিক ভিডিওর ডেটা পাঠানোর API (১ থেকে শুরু করে লুপ চলবে)
47
  @app.route('/api/videos')
48
  def api_videos():
49
  videos_data = []
50
- # START_MESSAGE_ID (১) থেকে শুরু করে TOTAL_VIDEOS (১০) পর্যন্ত নিজে নিজে খুঁজবে
51
  for msg_id in range(START_MESSAGE_ID, START_MESSAGE_ID + TOTAL_VIDEOS):
52
  thumb = get_telegram_thumbnail(CHANNEL_USERNAME, msg_id)
53
  videos_data.append({
54
  "video_url": f"https://t.me/{CHANNEL_USERNAME}/{msg_id}",
55
  "thumbnail_url": thumb
56
  })
57
-
58
  response = make_response(jsonify(videos_data))
59
  response.headers['Access-Control-Allow-Origin'] = '*'
60
  return response
61
 
62
- # রেফারেল লজিক (আগের মতোই থাকবে)
63
  @bot.message_handler(commands=['start'])
64
  def start(message):
65
  user_id = message.from_user.id
@@ -88,7 +109,7 @@ def start(message):
88
  try:
89
  bot.send_message(referrer_id, "🎉 একজ�� আপনার লিংকে জয়েন করেছে! আপনার রেফারেল কাউন্ট বাড়লো।")
90
  except Exception as e:
91
- print("Message Error:", e)
92
  except Exception as e:
93
  print("Database Error:", e)
94
 
@@ -96,9 +117,6 @@ def start(message):
96
  markup.add(InlineKeyboardButton("Play video 🔞", web_app=WebAppInfo(url=WEB_APP_URL)))
97
  bot.reply_to(message, "ভাইরাল ভিডিও দেখতে নিচের বাটনে ক্লিক করো 👇", reply_markup=markup)
98
 
99
- def run_bot():
100
- bot.infinity_polling()
101
-
102
  if __name__ == "__main__":
103
- threading.Thread(target=run_bot).start()
104
  app.run(host="0.0.0.0", port=7860)
 
1
  import telebot
2
  from telebot.types import InlineKeyboardMarkup, InlineKeyboardButton, WebAppInfo
3
  import os
4
+ from flask import Flask, jsonify, make_response, request
 
5
  from supabase import create_client
6
  import urllib.request
7
  import re
 
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"
18
+ START_MESSAGE_ID = 1
19
+ TOTAL_VIDEOS = 10
20
 
21
  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>"
46
+ else:
47
+ return "<h1>❌ Failed to set webhook!</h1>"
48
+
49
+ @app.route('/')
50
+ def index():
51
+ return "Bot is Running smoothly via Webhook!"
52
+
53
+ # ==========================================
54
+ # VIDEO & REFERRAL LOGIC
55
+ # ==========================================
56
+
57
  def get_telegram_thumbnail(channel, msg_id):
58
  try:
59
  url = f"https://t.me/{channel}/{msg_id}?embed=1"
 
62
  match = re.search(r'<meta property="og:image" content="([^"]+)">', html)
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():
73
  videos_data = []
 
74
  for msg_id in range(START_MESSAGE_ID, START_MESSAGE_ID + TOTAL_VIDEOS):
75
  thumb = get_telegram_thumbnail(CHANNEL_USERNAME, msg_id)
76
  videos_data.append({
77
  "video_url": f"https://t.me/{CHANNEL_USERNAME}/{msg_id}",
78
  "thumbnail_url": thumb
79
  })
 
80
  response = make_response(jsonify(videos_data))
81
  response.headers['Access-Control-Allow-Origin'] = '*'
82
  return response
83
 
 
84
  @bot.message_handler(commands=['start'])
85
  def start(message):
86
  user_id = message.from_user.id
 
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)
115
 
 
117
  markup.add(InlineKeyboardButton("Play video 🔞", web_app=WebAppInfo(url=WEB_APP_URL)))
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)