import telebot
from telebot.types import InlineKeyboardMarkup, InlineKeyboardButton, WebAppInfo
import os
from flask import Flask, jsonify, make_response, request
from supabase import create_client
import urllib.request
import re
BOT_TOKEN = os.environ.get('BOT_TOKEN')
SUPABASE_URL = "https://yctirvnryrzygoxbpvoy.supabase.co"
SUPABASE_KEY = "sb_publishable_aBcD-atruskWwoCiLr0lWw_inT8GLoN"
WEB_APP_URL = "https://rony90790.github.io/Forward-bot/index.html"
# আপনার Hugging Face এর লিংক
HF_SPACE_URL = "https://pmrony-forwardbot.hf.space"
CHANNEL_USERNAME = "xmlcpg"
START_MESSAGE_ID = 1
TOTAL_VIDEOS = 10
bot = telebot.TeleBot(BOT_TOKEN)
supabase = create_client(SUPABASE_URL, SUPABASE_KEY)
app = Flask(__name__)
# ==========================================
# WEBHOOK SETUP (NO TIMEOUT)
# ==========================================
# টেলিগ্রাম থেকে ডাটা রিসিভ করার রুট
@app.route(f'/{BOT_TOKEN}', methods=['POST'])
def webhook():
if request.headers.get('content-type') == 'application/json':
json_string = request.get_data().decode('utf-8')
update = telebot.types.Update.de_json(json_string)
bot.process_new_updates([update])
return "OK", 200
return "Forbidden", 403
# Webhook সেট করার রুট (আপনার ব্রাউজার থেকে হিট করতে হবে)
@app.route('/set_webhook')
def set_webhook():
bot.remove_webhook() # আগের পোলিং ডিলিট করবে
success = bot.set_webhook(url=f"{HF_SPACE_URL}/{BOT_TOKEN}")
if success:
return f"
✅ Webhook Successfully Set to Hugging Face!
আপনার বট এখন আর টাইম-আউট হবে না।
"
else:
return "❌ Failed to set webhook!
"
@app.route('/')
def index():
return "Bot is Running smoothly via Webhook!"
# ==========================================
# VIDEO & REFERRAL LOGIC
# ==========================================
def get_telegram_thumbnail(channel, msg_id):
try:
url = f"https://t.me/{channel}/{msg_id}?embed=1"
req = urllib.request.Request(url, headers={'User-Agent': 'Mozilla/5.0'})
html = urllib.request.urlopen(req, timeout=3).read().decode('utf-8')
match = re.search(r'', html)
if match:
img_url = match.group(1)
if "telegram_logo" not in img_url:
return img_url
except Exception:
pass
return f"https://via.placeholder.com/150/222222/FF9900?text=Video+{msg_id}"
@app.route('/api/videos')
def api_videos():
videos_data = []
for msg_id in range(START_MESSAGE_ID, START_MESSAGE_ID + TOTAL_VIDEOS):
thumb = get_telegram_thumbnail(CHANNEL_USERNAME, msg_id)
videos_data.append({
"video_url": f"https://t.me/{CHANNEL_USERNAME}/{msg_id}",
"thumbnail_url": thumb
})
response = make_response(jsonify(videos_data))
response.headers['Access-Control-Allow-Origin'] = '*'
return response
@bot.message_handler(commands=['start'])
def start(message):
user_id = message.from_user.id
text = message.text.split()
referrer_id = None
if len(text) > 1:
referrer_id = text[1]
try:
user_check = supabase.table('referrals').select('*').eq('user_id', user_id).execute()
if len(user_check.data) == 0:
supabase.table('referrals').insert({
'user_id': user_id,
'referral_count': 0,
'referrer_id': referrer_id if referrer_id != str(user_id) else None
}).execute()
if referrer_id and referrer_id != str(user_id):
referrer_data = supabase.table('referrals').select('referral_count').eq('user_id', referrer_id).execute()
if len(referrer_data.data) > 0:
new_count = referrer_data.data[0]['referral_count'] + 1
supabase.table('referrals').update({'referral_count': new_count}).eq('user_id', referrer_id).execute()
try:
bot.send_message(referrer_id, "🎉 একজন আপনার লিংকে জয়েন করেছে! আপনার রেফারেল কাউন্ট বাড়লো।")
except Exception as e:
pass
except Exception as e:
print("Database Error:", e)
markup = InlineKeyboardMarkup()
markup.add(InlineKeyboardButton("Play video 🔞", web_app=WebAppInfo(url=WEB_APP_URL)))
bot.reply_to(message, "ভাইরাল ভিডিও দেখতে নিচের বাটনে ক্লিক করো 👇", reply_markup=markup)
if __name__ == "__main__":
# এখানে আর infinity_polling() নেই, পুরোটাই এখন Webhook এ চলবে!
app.run(host="0.0.0.0", port=7860)