Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,26 +1,30 @@
|
|
| 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 |
|
| 7 |
-
|
|
|
|
| 8 |
SUPABASE_URL = "https://yctirvnryrzygoxbpvoy.supabase.co"
|
| 9 |
SUPABASE_KEY = "sb_publishable_aBcD-atruskWwoCiLr0lWw_inT8GLoN"
|
| 10 |
WEB_APP_URL = "https://rony90790.github.io/Forward-bot/index.html"
|
| 11 |
HF_SPACE_URL = "https://pmrony-forwardbot.hf.space"
|
| 12 |
|
|
|
|
|
|
|
|
|
|
| 13 |
bot = telebot.TeleBot(BOT_TOKEN)
|
| 14 |
supabase = create_client(SUPABASE_URL, SUPABASE_KEY)
|
| 15 |
app = Flask(__name__)
|
| 16 |
|
| 17 |
-
# অ্যাডমিন
|
| 18 |
admin_states = {}
|
| 19 |
|
| 20 |
@app.route(f'/{BOT_TOKEN}', methods=['POST'])
|
| 21 |
def webhook():
|
| 22 |
-
if
|
| 23 |
-
json_string =
|
| 24 |
update = telebot.types.Update.de_json(json_string)
|
| 25 |
bot.process_new_updates([update])
|
| 26 |
return "OK", 200
|
|
@@ -28,114 +32,85 @@ def webhook():
|
|
| 28 |
|
| 29 |
@app.route('/set_webhook')
|
| 30 |
def set_webhook():
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
@app.route('/')
|
| 39 |
def index():
|
| 40 |
-
return "Bot is Running
|
| 41 |
|
| 42 |
-
# ==================================
|
| 43 |
-
# ADMIN COMMAND TO ADD VIDEO
|
| 44 |
-
# ==========================================
|
| 45 |
|
| 46 |
@bot.message_handler(commands=['png'])
|
| 47 |
def add_png(message):
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
else:
|
| 56 |
-
bot.reply_to(message, "⚠️ সঠিক নিয়ম: `/png <ছবির লিংক>` এভাবে দিন।", parse_mode="Markdown")
|
| 57 |
-
except Exception as e:
|
| 58 |
-
bot.reply_to(message, f"Error: {e}")
|
| 59 |
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
def add_video_link(message):
|
| 63 |
video_url = message.text.strip()
|
| 64 |
thumbnail_url = admin_states[message.chat.id]["thumbnail_url"]
|
| 65 |
|
| 66 |
try:
|
| 67 |
-
# Supabase ডাটাবেজে সেভ করা
|
| 68 |
supabase.table('videos').insert({
|
| 69 |
"video_url": video_url,
|
| 70 |
"thumbnail_url": thumbnail_url
|
| 71 |
}).execute()
|
| 72 |
-
bot.reply_to(message, "
|
| 73 |
except Exception as e:
|
| 74 |
-
bot.reply_to(message, f"❌
|
| 75 |
|
| 76 |
-
|
| 77 |
-
del admin_states[message.chat.id]
|
| 78 |
|
| 79 |
-
# ==================================
|
| 80 |
-
# API FOR MINI APP
|
| 81 |
-
# ==========================================
|
| 82 |
|
| 83 |
@app.route('/api/videos')
|
| 84 |
def api_videos():
|
| 85 |
try:
|
| 86 |
-
# ডাটাবেজ থেকে সর্বশেষ অ্যাড করা ভিডিওগুলো আনবে
|
| 87 |
res = supabase.table('videos').select('*').order('id', desc=True).execute()
|
| 88 |
-
|
| 89 |
-
except
|
| 90 |
-
|
| 91 |
-
videos_data = []
|
| 92 |
-
|
| 93 |
-
response = make_response(jsonify(videos_data))
|
| 94 |
-
response.headers['Access-Control-Allow-Origin'] = '*'
|
| 95 |
-
return response
|
| 96 |
|
| 97 |
-
# ==================================
|
| 98 |
-
# BOT START / REFERRAL LOGIC
|
| 99 |
-
# ==========================================
|
| 100 |
|
| 101 |
@bot.message_handler(commands=['start'])
|
| 102 |
def start(message):
|
| 103 |
user_id = message.from_user.id
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
referrer_id = None
|
| 107 |
-
if len(text) > 1:
|
| 108 |
-
try:
|
| 109 |
-
referrer_id = int(text[1])
|
| 110 |
-
except ValueError:
|
| 111 |
-
referrer_id = None
|
| 112 |
|
| 113 |
try:
|
| 114 |
user_check = supabase.table('referrals').select('*').eq('user_id', user_id).execute()
|
| 115 |
-
|
| 116 |
if not user_check.data:
|
| 117 |
supabase.table('referrals').insert({
|
| 118 |
-
'user_id': user_id,
|
| 119 |
-
'referral_count': 0,
|
| 120 |
'referrer_id': referrer_id if referrer_id != user_id else None
|
| 121 |
}).execute()
|
| 122 |
|
| 123 |
if referrer_id and referrer_id != user_id:
|
| 124 |
-
|
| 125 |
-
if
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
try:
|
| 130 |
-
bot.send_message(referrer_id, "🎉 একজন আপনার লিংকে জয়েন করেছে! আপনার রেফারেল কাউন্ট বাড়লো।")
|
| 131 |
-
except Exception:
|
| 132 |
-
pass
|
| 133 |
-
except Exception as e:
|
| 134 |
-
print("[DATABASE ERROR]", e)
|
| 135 |
|
| 136 |
markup = InlineKeyboardMarkup()
|
| 137 |
markup.add(InlineKeyboardButton("Play video 🔞", web_app=WebAppInfo(url=WEB_APP_URL)))
|
| 138 |
-
bot.reply_to(message, "ভ
|
| 139 |
|
| 140 |
if __name__ == "__main__":
|
| 141 |
-
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 |
|
| 7 |
+
# টোকেন চেক
|
| 8 |
+
BOT_TOKEN = os.environ.get('BOT_TOKEN')
|
| 9 |
SUPABASE_URL = "https://yctirvnryrzygoxbpvoy.supabase.co"
|
| 10 |
SUPABASE_KEY = "sb_publishable_aBcD-atruskWwoCiLr0lWw_inT8GLoN"
|
| 11 |
WEB_APP_URL = "https://rony90790.github.io/Forward-bot/index.html"
|
| 12 |
HF_SPACE_URL = "https://pmrony-forwardbot.hf.space"
|
| 13 |
|
| 14 |
+
if not BOT_TOKEN:
|
| 15 |
+
print("Error: BOT_TOKEN not found in environment variables!")
|
| 16 |
+
|
| 17 |
bot = telebot.TeleBot(BOT_TOKEN)
|
| 18 |
supabase = create_client(SUPABASE_URL, SUPABASE_KEY)
|
| 19 |
app = Flask(__name__)
|
| 20 |
|
| 21 |
+
# অ্যাডমিন স্টেট
|
| 22 |
admin_states = {}
|
| 23 |
|
| 24 |
@app.route(f'/{BOT_TOKEN}', methods=['POST'])
|
| 25 |
def webhook():
|
| 26 |
+
if request.headers.get('content-type') == 'application/json':
|
| 27 |
+
json_string = request.get_data().decode('utf-8')
|
| 28 |
update = telebot.types.Update.de_json(json_string)
|
| 29 |
bot.process_new_updates([update])
|
| 30 |
return "OK", 200
|
|
|
|
| 32 |
|
| 33 |
@app.route('/set_webhook')
|
| 34 |
def set_webhook():
|
| 35 |
+
try:
|
| 36 |
+
bot.remove_webhook()
|
| 37 |
+
webhook_url = f"{HF_SPACE_URL}/{BOT_TOKEN}"
|
| 38 |
+
success = bot.set_webhook(url=webhook_url)
|
| 39 |
+
if success:
|
| 40 |
+
return f"<h1>✅ Webhook Set Successfully!</h1><p>URL: {webhook_url}</p>"
|
| 41 |
+
else:
|
| 42 |
+
return "<h1>❌ Webhook Set Failed!</h1>"
|
| 43 |
+
except Exception as e:
|
| 44 |
+
return f"<h1>❌ Error:</h1><p>{str(e)}</p>"
|
| 45 |
|
| 46 |
@app.route('/')
|
| 47 |
def index():
|
| 48 |
+
return "Bot is Running!"
|
| 49 |
|
| 50 |
+
# ================= ADMIN LOGIC =================
|
|
|
|
|
|
|
| 51 |
|
| 52 |
@bot.message_handler(commands=['png'])
|
| 53 |
def add_png(message):
|
| 54 |
+
parts = message.text.split(maxsplit=1)
|
| 55 |
+
if len(parts) > 1:
|
| 56 |
+
img_url = parts[1].strip()
|
| 57 |
+
admin_states[message.chat.id] = {"step": 1, "thumbnail_url": img_url}
|
| 58 |
+
bot.reply_to(message, "✅ ছবি সেট হয়েছে। এখন ভিডিওর Google Drive লিংক দিন।")
|
| 59 |
+
else:
|
| 60 |
+
bot.reply_to(message, "সঠিক নিয়ম: `/png https://link-to-image.jpg`")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
+
@bot.message_handler(func=lambda m: admin_states.get(m.chat.id, {}).get("step") == 1)
|
| 63 |
+
def add_video(message):
|
|
|
|
| 64 |
video_url = message.text.strip()
|
| 65 |
thumbnail_url = admin_states[message.chat.id]["thumbnail_url"]
|
| 66 |
|
| 67 |
try:
|
|
|
|
| 68 |
supabase.table('videos').insert({
|
| 69 |
"video_url": video_url,
|
| 70 |
"thumbnail_url": thumbnail_url
|
| 71 |
}).execute()
|
| 72 |
+
bot.reply_to(message, "🎉 সফলভাবে ভিডিও অ্যাড হয়েছে!")
|
| 73 |
except Exception as e:
|
| 74 |
+
bot.reply_to(message, f"❌ ভুল ���য়েছে: {e}")
|
| 75 |
|
| 76 |
+
admin_states[message.chat.id] = {}
|
|
|
|
| 77 |
|
| 78 |
+
# ================= API =================
|
|
|
|
|
|
|
| 79 |
|
| 80 |
@app.route('/api/videos')
|
| 81 |
def api_videos():
|
| 82 |
try:
|
|
|
|
| 83 |
res = supabase.table('videos').select('*').order('id', desc=True).execute()
|
| 84 |
+
return jsonify(res.data)
|
| 85 |
+
except:
|
| 86 |
+
return jsonify([])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
|
| 88 |
+
# ================= START / REFERRAL =================
|
|
|
|
|
|
|
| 89 |
|
| 90 |
@bot.message_handler(commands=['start'])
|
| 91 |
def start(message):
|
| 92 |
user_id = message.from_user.id
|
| 93 |
+
args = message.text.split()
|
| 94 |
+
referrer_id = int(args[1]) if len(args) > 1 and args[1].isdigit() else None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
|
| 96 |
try:
|
| 97 |
user_check = supabase.table('referrals').select('*').eq('user_id', user_id).execute()
|
|
|
|
| 98 |
if not user_check.data:
|
| 99 |
supabase.table('referrals').insert({
|
| 100 |
+
'user_id': user_id, 'referral_count': 0,
|
|
|
|
| 101 |
'referrer_id': referrer_id if referrer_id != user_id else None
|
| 102 |
}).execute()
|
| 103 |
|
| 104 |
if referrer_id and referrer_id != user_id:
|
| 105 |
+
ref_data = supabase.table('referrals').select('referral_count').eq('user_id', referrer_id).execute()
|
| 106 |
+
if ref_data.data:
|
| 107 |
+
supabase.table('referrals').update({'referral_count': ref_data.data[0]['referral_count'] + 1}).eq('user_id', referrer_id).execute()
|
| 108 |
+
bot.send_message(referrer_id, "🎁 কেউ আপনার লিঙ্কে জয়েন করেছে!")
|
| 109 |
+
except: pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
|
| 111 |
markup = InlineKeyboardMarkup()
|
| 112 |
markup.add(InlineKeyboardButton("Play video 🔞", web_app=WebAppInfo(url=WEB_APP_URL)))
|
| 113 |
+
bot.reply_to(message, "ভিডিও দেখতে নিচের বাটনে ক্লিক করুন 👇", reply_markup=markup)
|
| 114 |
|
| 115 |
if __name__ == "__main__":
|
| 116 |
+
app.run(host="0.0.0.0", port=7860)
|