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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -14
app.py CHANGED
@@ -2,24 +2,64 @@ import telebot
2
  from telebot.types import InlineKeyboardMarkup, InlineKeyboardButton, WebAppInfo
3
  import os
4
  import threading
5
- from flask import Flask
6
  from supabase import create_client
 
 
7
 
8
- # --- কনফিগারেশন ---
9
- BOT_TOKEN = os.environ.get('BOT_TOKEN') # এটি HuggingFace Secret-এ থাকবে
10
  SUPABASE_URL = "https://yctirvnryrzygoxbpvoy.supabase.co"
11
  SUPABASE_KEY = "sb_publishable_aBcD-atruskWwoCiLr0lWw_inT8GLoN"
12
  WEB_APP_URL = "https://rony90790.github.io/Forward-bot/index.html"
13
 
 
 
 
 
 
 
14
  bot = telebot.TeleBot(BOT_TOKEN)
15
  supabase = create_client(SUPABASE_URL, SUPABASE_KEY)
16
  app = Flask(__name__)
17
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
  @app.route('/')
19
  def index():
20
  return "Bot is Running!"
21
 
22
- # --- সার্ট রোরে লজি---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
  @bot.message_handler(commands=['start'])
24
  def start(message):
25
  user_id = message.from_user.id
@@ -27,39 +67,33 @@ def start(message):
27
 
28
  referrer_id = None
29
  if len(text) > 1:
30
- referrer_id = text[1] # যে ইনভাইট করেছে তার ID
31
 
32
- # চেক করবো ইউজার ডেটাবেসে আগে থেকে আছে কিনা
33
  try:
34
  user_check = supabase.table('referrals').select('*').eq('user_id', user_id).execute()
35
 
36
  if len(user_check.data) == 0:
37
- # ইউজার নতুন, তাই ডেটাবেসে যুক্ত করবো
38
  supabase.table('referrals').insert({
39
  'user_id': user_id,
40
  'referral_count': 0,
41
- 'referred_by': referrer_id if referrer_id != str(user_id) else None
42
  }).execute()
43
 
44
- # যদি সে কারো লিংক থেকে আসে, তাহলে ইনভাইটকারীর পয়েন্ট বাড়াবো
45
  if referrer_id and referrer_id != str(user_id):
46
  referrer_data = supabase.table('referrals').select('referral_count').eq('user_id', referrer_id).execute()
47
  if len(referrer_data.data) > 0:
48
  new_count = referrer_data.data[0]['referral_count'] + 1
49
  supabase.table('referrals').update({'referral_count': new_count}).eq('user_id', referrer_id).execute()
50
 
51
- # ইনভাইটকারীকে নোটিফিকেশন পাঠানো (ঐচ্ছিক)
52
  try:
53
  bot.send_message(referrer_id, "🎉 একজন আপনার লিংকে জয়েন করেছে! আপনার রেফারেল কাউন্ট বাড়লো।")
54
- except:
55
- pass
56
  except Exception as e:
57
  print("Database Error:", e)
58
 
59
- # Web App বাটন তৈরি
60
  markup = InlineKeyboardMarkup()
61
  markup.add(InlineKeyboardButton("Play video 🔞", web_app=WebAppInfo(url=WEB_APP_URL)))
62
-
63
  bot.reply_to(message, "ভাইরাল ভিডিও দেখতে নিচের বাটনে ক্লিক করো 👇", reply_markup=markup)
64
 
65
  def run_bot():
 
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
9
 
10
+ BOT_TOKEN = os.environ.get('BOT_TOKEN')
 
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
 
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"
29
+ req = urllib.request.Request(url, headers={'User-Agent': 'Mozilla/5.0'})
30
+ html = urllib.request.urlopen(req, timeout=3).read().decode('utf-8')
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
 
67
 
68
  referrer_id = None
69
  if len(text) > 1:
70
+ referrer_id = text[1]
71
 
 
72
  try:
73
  user_check = supabase.table('referrals').select('*').eq('user_id', user_id).execute()
74
 
75
  if len(user_check.data) == 0:
 
76
  supabase.table('referrals').insert({
77
  'user_id': user_id,
78
  'referral_count': 0,
79
+ 'referrer_id': referrer_id if referrer_id != str(user_id) else None
80
  }).execute()
81
 
 
82
  if referrer_id and referrer_id != str(user_id):
83
  referrer_data = supabase.table('referrals').select('referral_count').eq('user_id', referrer_id).execute()
84
  if len(referrer_data.data) > 0:
85
  new_count = referrer_data.data[0]['referral_count'] + 1
86
  supabase.table('referrals').update({'referral_count': new_count}).eq('user_id', referrer_id).execute()
87
 
 
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
 
 
95
  markup = InlineKeyboardMarkup()
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():