Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,24 +4,40 @@ from telebot.types import InlineKeyboardMarkup, InlineKeyboardButton, WebAppInfo
|
|
| 4 |
import os
|
| 5 |
from flask import Flask, jsonify, make_response, request
|
| 6 |
from supabase import create_client
|
| 7 |
-
import
|
| 8 |
|
| 9 |
-
# ডিটেইলস
|
| 10 |
BOT_TOKEN = "8628213901:AAFvfHBpZ6tok40ZQuhIDLAVIMrHeiheMNY"
|
| 11 |
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 |
HF_SPACE_URL = "https://pmrony-forwardbot.hf.space"
|
| 15 |
|
| 16 |
-
#
|
| 17 |
-
apihelper.CONNECT_TIMEOUT =
|
| 18 |
-
apihelper.READ_TIMEOUT =
|
| 19 |
|
| 20 |
bot = telebot.TeleBot(BOT_TOKEN, threaded=False)
|
| 21 |
supabase = create_client(SUPABASE_URL, SUPABASE_KEY)
|
| 22 |
app = Flask(__name__)
|
| 23 |
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
@app.route(f'/{BOT_TOKEN}', methods=['POST'])
|
| 27 |
def webhook():
|
|
@@ -35,81 +51,51 @@ def webhook():
|
|
| 35 |
@app.route('/set_webhook')
|
| 36 |
def set_webhook():
|
| 37 |
bot.remove_webhook()
|
| 38 |
-
|
|
|
|
| 39 |
if success:
|
| 40 |
-
return "<h1>✅ Webhook Set
|
| 41 |
else:
|
| 42 |
return "<h1>❌ Webhook Set Failed!</h1>"
|
| 43 |
|
| 44 |
@app.route('/')
|
| 45 |
def index():
|
| 46 |
-
return "Bot is Running
|
| 47 |
|
| 48 |
-
# =================
|
| 49 |
|
| 50 |
@bot.message_handler(commands=['start'])
|
| 51 |
def start(message):
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
'referrer_id': referrer_id if referrer_id != user_id else None
|
| 64 |
-
}).execute()
|
| 65 |
-
|
| 66 |
-
if referrer_id and referrer_id != user_id:
|
| 67 |
-
ref_data = supabase.table('referrals').select('referral_count').eq('user_id', referrer_id).execute()
|
| 68 |
-
if ref_data.data:
|
| 69 |
-
new_count = ref_data.data[0]['referral_count'] + 1
|
| 70 |
-
supabase.table('referrals').update({'referral_count': new_count}).eq('user_id', referrer_id).execute()
|
| 71 |
-
bot.send_message(referrer_id, "🎉 কেউ আপনার রেফারে জয়েন করেছে!")
|
| 72 |
-
except Exception as db_e:
|
| 73 |
-
print(f"Database issue: {db_e}")
|
| 74 |
-
|
| 75 |
-
# কী-বোর্ড এবং মেসেজ পাঠানো
|
| 76 |
-
markup = InlineKeyboardMarkup()
|
| 77 |
-
markup.add(InlineKeyboardButton("Play video 🔞", web_app=WebAppInfo(url=WEB_APP_URL)))
|
| 78 |
-
|
| 79 |
-
# সরাসরি send_message ব্যবহার করা হয়েছে রিলাইবিলিটির জন্য
|
| 80 |
-
bot.send_message(message.chat.id, "ভাইরাল ভিডিও দেখতে নিচের বাটনে ক্লিক করো 👇", reply_markup=markup)
|
| 81 |
-
|
| 82 |
-
except Exception as e:
|
| 83 |
-
print(f"Error in /start: {e}")
|
| 84 |
|
| 85 |
@bot.message_handler(commands=['png'])
|
| 86 |
def add_png(message):
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
except Exception as e:
|
| 96 |
-
print(f"Error in /png: {e}")
|
| 97 |
|
| 98 |
-
|
| 99 |
-
|
| 100 |
try:
|
| 101 |
-
video_url
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
"video_url": video_url,
|
| 106 |
-
"thumbnail_url": thumbnail_url
|
| 107 |
-
}).execute()
|
| 108 |
-
|
| 109 |
-
bot.send_message(message.chat.id, "🎉 অভিনন্দন! ভিডিওটি সফলভাবে অ্যাপে অ্যাড হয়েছে।")
|
| 110 |
-
admin_states[message.chat.id] = {}
|
| 111 |
-
except Exception as e:
|
| 112 |
-
bot.send_message(message.chat.id, "❌ ডাটাবেজে সেভ করতে সমস্যা হয়েছে।")
|
| 113 |
|
| 114 |
@app.route('/api/videos')
|
| 115 |
def api_videos():
|
|
|
|
| 4 |
import os
|
| 5 |
from flask import Flask, jsonify, make_response, request
|
| 6 |
from supabase import create_client
|
| 7 |
+
import threading
|
| 8 |
|
| 9 |
+
# টোকেন ও ডিটেইলস
|
| 10 |
BOT_TOKEN = "8628213901:AAFvfHBpZ6tok40ZQuhIDLAVIMrHeiheMNY"
|
| 11 |
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 |
HF_SPACE_URL = "https://pmrony-forwardbot.hf.space"
|
| 15 |
|
| 16 |
+
# কানেকশন টাইমআউট সর্বোচ্চ করা হলো
|
| 17 |
+
apihelper.CONNECT_TIMEOUT = 100
|
| 18 |
+
apihelper.READ_TIMEOUT = 100
|
| 19 |
|
| 20 |
bot = telebot.TeleBot(BOT_TOKEN, threaded=False)
|
| 21 |
supabase = create_client(SUPABASE_URL, SUPABASE_KEY)
|
| 22 |
app = Flask(__name__)
|
| 23 |
|
| 24 |
+
# --- ডাটাবেজ কাজ আলাদা থ্রেডে করার ফাংশন ---
|
| 25 |
+
def save_referral_bg(user_id, referrer_id):
|
| 26 |
+
try:
|
| 27 |
+
user_check = supabase.table('referrals').select('*').eq('user_id', user_id).execute()
|
| 28 |
+
if not user_check.data:
|
| 29 |
+
supabase.table('referrals').insert({
|
| 30 |
+
'user_id': user_id, 'referral_count': 0,
|
| 31 |
+
'referrer_id': referrer_id if referrer_id != user_id else None
|
| 32 |
+
}).execute()
|
| 33 |
+
if referrer_id and referrer_id != user_id:
|
| 34 |
+
ref_data = supabase.table('referrals').select('referral_count').eq('user_id', referrer_id).execute()
|
| 35 |
+
if ref_data.data:
|
| 36 |
+
new_count = ref_data.data[0]['referral_count'] + 1
|
| 37 |
+
supabase.table('referrals').update({'referral_count': new_count}).eq('user_id', referrer_id).execute()
|
| 38 |
+
bot.send_message(referrer_id, "🎉 কেউ আপনার রেফারে জয়েন করেছে!")
|
| 39 |
+
except:
|
| 40 |
+
pass
|
| 41 |
|
| 42 |
@app.route(f'/{BOT_TOKEN}', methods=['POST'])
|
| 43 |
def webhook():
|
|
|
|
| 51 |
@app.route('/set_webhook')
|
| 52 |
def set_webhook():
|
| 53 |
bot.remove_webhook()
|
| 54 |
+
# ওয়েব হুক সেট করার সময় ১০ সেকেন্ড ওয়েট করবে
|
| 55 |
+
success = bot.set_webhook(url=f"{HF_SPACE_URL}/{BOT_TOKEN}", timeout=60)
|
| 56 |
if success:
|
| 57 |
+
return "<h1>✅ Webhook Set Success!</h1>"
|
| 58 |
else:
|
| 59 |
return "<h1>❌ Webhook Set Failed!</h1>"
|
| 60 |
|
| 61 |
@app.route('/')
|
| 62 |
def index():
|
| 63 |
+
return "Bot is Running Perfectly!"
|
| 64 |
|
| 65 |
+
# ================= COMMANDS =================
|
| 66 |
|
| 67 |
@bot.message_handler(commands=['start'])
|
| 68 |
def start(message):
|
| 69 |
+
user_id = message.from_user.id
|
| 70 |
+
args = message.text.split()
|
| 71 |
+
referrer_id = int(args[1]) if len(args) > 1 and args[1].isdigit() else None
|
| 72 |
+
|
| 73 |
+
# ডাটাবেজের কাজ হবে ব্যাকগ্রাউন্ডে, যাতে বট স্লো না হয়
|
| 74 |
+
threading.Thread(target=save_referral_bg, args=(user_id, referrer_id)).start()
|
| 75 |
+
|
| 76 |
+
# ইউজারকে সাথে সাথে রিপ্লাই দিবে
|
| 77 |
+
markup = InlineKeyboardMarkup()
|
| 78 |
+
markup.add(InlineKeyboardButton("Play video 🔞", web_app=WebAppInfo(url=WEB_APP_URL)))
|
| 79 |
+
bot.send_message(message.chat.id, "ভাইরাল ভিডিও দেখতে নিচের বাটনে ক্লিক করো 👇", reply_markup=markup)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
|
| 81 |
@bot.message_handler(commands=['png'])
|
| 82 |
def add_png(message):
|
| 83 |
+
parts = message.text.split(maxsplit=1)
|
| 84 |
+
if len(parts) > 1:
|
| 85 |
+
img_url = parts[1].strip()
|
| 86 |
+
bot.send_message(message.chat.id, f"✅ ছবি সেট হয়েছে। এখন এই ছবির জন্য ভিডিওর **Google Drive লিংক** পাঠান।")
|
| 87 |
+
# ভিডিও অ্যাড করার লজিক এখানে সিম্পল রাখা হয়েছে
|
| 88 |
+
bot.register_next_step_handler(message, save_video_final, img_url)
|
| 89 |
+
else:
|
| 90 |
+
bot.send_message(message.chat.id, "সঠিক নিয়ম: `/png https://link.jpg`")
|
|
|
|
|
|
|
| 91 |
|
| 92 |
+
def save_video_final(message, img_url):
|
| 93 |
+
video_url = message.text.strip()
|
| 94 |
try:
|
| 95 |
+
supabase.table('videos').insert({"video_url": video_url, "thumbnail_url": img_url}).execute()
|
| 96 |
+
bot.send_message(message.chat.id, "🎉 সফলভাবে ভিডিও অ্যাড হয়েছে!")
|
| 97 |
+
except:
|
| 98 |
+
bot.send_message(message.chat.id, "❌ ডাটাবেজ এরর!")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
|
| 100 |
@app.route('/api/videos')
|
| 101 |
def api_videos():
|