pmrony commited on
Commit
e5db5e0
·
verified ·
1 Parent(s): fb1f369

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -25
app.py CHANGED
@@ -1,10 +1,10 @@
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 traceback
7
- import time
8
 
9
  # ডিটেইলস
10
  BOT_TOKEN = "8628213901:AAFvfHBpZ6tok40ZQuhIDLAVIMrHeiheMNY"
@@ -13,8 +13,11 @@ 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
- bot = telebot.TeleBot(BOT_TOKEN)
17
- # ক্লায়েন্ট ইনিশিয়ালাইজেশন
 
 
 
18
  supabase = create_client(SUPABASE_URL, SUPABASE_KEY)
19
  app = Flask(__name__)
20
 
@@ -42,24 +45,21 @@ def set_webhook():
42
  def index():
43
  return "Bot is Running smoothly!"
44
 
45
- # ================= HANDLERS =================
46
 
47
  @bot.message_handler(commands=['start'])
48
  def start(message):
49
- user_id = message.from_user.id
50
- print(f"[LOG] Start command from {user_id}")
51
-
52
  try:
 
53
  args = message.text.split()
54
  referrer_id = int(args[1]) if len(args) > 1 and args[1].isdigit() else None
55
 
56
- # ডাটাবেজ অপরেশন (Try-Except এর ভতরে রাখা হয়ছে র হলে বট থামে)
57
  try:
58
  user_check = supabase.table('referrals').select('*').eq('user_id', user_id).execute()
59
  if not user_check.data:
60
  supabase.table('referrals').insert({
61
- 'user_id': user_id,
62
- 'referral_count': 0,
63
  'referrer_id': referrer_id if referrer_id != user_id else None
64
  }).execute()
65
 
@@ -69,17 +69,18 @@ def start(message):
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_err:
73
- print(f"[DB ERROR] {db_err}")
74
- # ডাটাবেজে এরর হলেও আমরা বটকে রিপ্লাই দিতে দিব
75
-
76
  markup = InlineKeyboardMarkup()
77
  markup.add(InlineKeyboardButton("Play video 🔞", web_app=WebAppInfo(url=WEB_APP_URL)))
78
- bot.reply_to(message, "ভাইরাল ভিডিও দেখতে নিচের বাটনে ক্লিক করো 👇", reply_markup=markup)
 
 
79
 
80
  except Exception as e:
81
- print(f"[CRITICAL ERROR] {e}")
82
- traceback.print_exc()
83
 
84
  @bot.message_handler(commands=['png'])
85
  def add_png(message):
@@ -88,11 +89,11 @@ def add_png(message):
88
  if len(parts) > 1:
89
  img_url = parts[1].strip()
90
  admin_states[message.chat.id] = {"step": 1, "thumbnail_url": img_url}
91
- bot.reply_to(message, "✅ ছবি সেট হয়েছে। এখন এই ছবির জন্য **Google Drive ভিডিও লিংক** দিন।")
92
  else:
93
- bot.reply_to(message, "সঠিক নিয়ম: `/png https://link-to-image.jpg`")
94
  except Exception as e:
95
- print(f"[ERROR] /png: {e}")
96
 
97
  @bot.message_handler(func=lambda m: admin_states.get(m.chat.id, {}).get("step") == 1)
98
  def add_video_step(message):
@@ -100,23 +101,23 @@ def add_video_step(message):
100
  video_url = message.text.strip()
101
  thumbnail_url = admin_states[message.chat.id]["thumbnail_url"]
102
 
103
- # ডাটাবেজে ভিডিও সেভ
104
  supabase.table('videos').insert({
105
  "video_url": video_url,
106
  "thumbnail_url": thumbnail_url
107
  }).execute()
108
 
109
- bot.reply_to(message, "🎉 অভিনন্দন! ভিডিওটি সফলভাবে অ্যাপে অ্যাড হয়েছে।")
110
  admin_states[message.chat.id] = {}
111
  except Exception as e:
112
- print(f"[DB ERROR] Video Insert: {e}")
113
- bot.reply_to(message, "❌ ডাটাবেজে সেভ করতে সমস্যা হয়েছে। আবার চেষ্টা করুন।")
114
 
115
  @app.route('/api/videos')
116
  def api_videos():
117
  try:
118
  res = supabase.table('videos').select('*').order('id', desc=True).execute()
119
- return jsonify(res.data)
 
 
120
  except:
121
  return jsonify([])
122
 
 
1
  import telebot
2
+ from telebot import apihelper
3
  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 traceback
 
8
 
9
  # ডিটেইলস
10
  BOT_TOKEN = "8628213901:AAFvfHBpZ6tok40ZQuhIDLAVIMrHeiheMNY"
 
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 = 60
18
+ apihelper.READ_TIMEOUT = 60
19
+
20
+ bot = telebot.TeleBot(BOT_TOKEN, threaded=False)
21
  supabase = create_client(SUPABASE_URL, SUPABASE_KEY)
22
  app = Flask(__name__)
23
 
 
45
  def index():
46
  return "Bot is Running smoothly!"
47
 
48
+ # ================= COMMAND HANDLERS =================
49
 
50
  @bot.message_handler(commands=['start'])
51
  def start(message):
 
 
 
52
  try:
53
+ user_id = message.from_user.id
54
  args = message.text.split()
55
  referrer_id = int(args[1]) if len(args) > 1 and args[1].isdigit() else None
56
 
57
+ # ডাটাবেজ টা রা ষ্টা (এটইল রলে মেসেজ ে)
58
  try:
59
  user_check = supabase.table('referrals').select('*').eq('user_id', user_id).execute()
60
  if not user_check.data:
61
  supabase.table('referrals').insert({
62
+ 'user_id': user_id, 'referral_count': 0,
 
63
  'referrer_id': referrer_id if referrer_id != user_id else None
64
  }).execute()
65
 
 
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):
 
89
  if len(parts) > 1:
90
  img_url = parts[1].strip()
91
  admin_states[message.chat.id] = {"step": 1, "thumbnail_url": img_url}
92
+ bot.send_message(message.chat.id, "✅ ছবি সেট হয়েছে। এখন এই ছবির জন্য **Google Drive ভিডিও লিংক** দিন।")
93
  else:
94
+ bot.send_message(message.chat.id, "সঠিক নিয়ম: `/png https://link-to-image.jpg`")
95
  except Exception as e:
96
+ print(f"Error in /png: {e}")
97
 
98
  @bot.message_handler(func=lambda m: admin_states.get(m.chat.id, {}).get("step") == 1)
99
  def add_video_step(message):
 
101
  video_url = message.text.strip()
102
  thumbnail_url = admin_states[message.chat.id]["thumbnail_url"]
103
 
 
104
  supabase.table('videos').insert({
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():
116
  try:
117
  res = supabase.table('videos').select('*').order('id', desc=True).execute()
118
+ response = make_response(jsonify(res.data))
119
+ response.headers['Access-Control-Allow-Origin'] = '*'
120
+ return response
121
  except:
122
  return jsonify([])
123