Free Mode Toggle - Automatic Stream Cleanup - Manual IMDb/TMDb Override - Added (#35)
Browse files* New Feature added
* Update README.md
* Update README.md
* Update README.md
* Update README.md
* 2.1.0 - SUBSCRIPTION added
* Update start.sh
* Free Mode Toggle, Automatic Stream Cleanup, Manual IMDb/TMDb Override - Added
- π **Free Mode Toggle** β Administrators can turn off the `SUBSCRIPTION` requirement in `config.env` to allow all users immediate access via an automatically generated API token.
- π **Automatic Stream Cleanup** β Deleting a source message in the Telegram channel instantly deletes all corresponding streams and qualities from the Stremio Addon Database and Admin Panel, preventing dead links.
- π·οΈ **Manual IMDb/TMDb Override** β Users can instantly update or fix incorrect metadata for a file by simply editing the Telegram channel message caption and pasting the correct IMDb/TMDB URL.
---------
Co-authored-by: Karan Adhikari <doc.adhikari@gmail.com>
- Backend/fastapi/routes/stream_routes.py +1 -1
- Backend/helper/database.py +37 -0
- Backend/helper/metadata.py +13 -5
- Backend/helper/subscription_checker.py +2 -2
- Backend/pyrofork/plugins/reciever.py +64 -0
- Backend/pyrofork/plugins/start.py +58 -40
- Backend/pyrofork/plugins/subscription.py +42 -4
- Backend/pyrofork/subscription_plugins/start.py +98 -0
- Backend/pyrofork/subscription_plugins/subscription.py +377 -0
- README.md +17 -1
|
@@ -455,4 +455,4 @@ async def get_stream_detail(stream_id: str):
|
|
| 455 |
if rec.get("stream_id") == stream_id:
|
| 456 |
return JSONResponse(make_json_safe(rec))
|
| 457 |
|
| 458 |
-
raise HTTPException(status_code=404, detail="Stream not found")
|
|
|
|
| 455 |
if rec.get("stream_id") == stream_id:
|
| 456 |
return JSONResponse(make_json_safe(rec))
|
| 457 |
|
| 458 |
+
raise HTTPException(status_code=404, detail="Stream not found")
|
|
@@ -990,6 +990,43 @@ class Database:
|
|
| 990 |
|
| 991 |
return None
|
| 992 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 993 |
async def delete_movie_quality(self, tmdb_id: int, db_index: int, id: str) -> bool:
|
| 994 |
db_key = f"storage_{db_index}"
|
| 995 |
movie = await self.dbs[db_key]["movie"].find_one({"tmdb_id": tmdb_id})
|
|
|
|
| 990 |
|
| 991 |
return None
|
| 992 |
|
| 993 |
+
async def delete_media_by_stream_id(self, stream_id_hash: str) -> bool:
|
| 994 |
+
"""Finds and removes a specific stream quality by its hash across all DBs.
|
| 995 |
+
If it's the last quality, it cleans up the movie or episode/season/show."""
|
| 996 |
+
for i in range(1, self.current_db_index + 1):
|
| 997 |
+
db = self.dbs[f"storage_{i}"]
|
| 998 |
+
|
| 999 |
+
# Check Movies
|
| 1000 |
+
movie = await db["movie"].find_one({"telegram.id": stream_id_hash})
|
| 1001 |
+
if movie:
|
| 1002 |
+
movie["telegram"] = [q for q in movie.get("telegram", []) if q.get("id") != stream_id_hash]
|
| 1003 |
+
if len(movie["telegram"]) == 0:
|
| 1004 |
+
await db["movie"].delete_one({"_id": movie["_id"]})
|
| 1005 |
+
else:
|
| 1006 |
+
movie['updated_on'] = datetime.utcnow()
|
| 1007 |
+
await db["movie"].replace_one({"_id": movie["_id"]}, movie)
|
| 1008 |
+
return True
|
| 1009 |
+
|
| 1010 |
+
# Check TV Shows
|
| 1011 |
+
tv = await db["tv"].find_one({"seasons.episodes.telegram.id": stream_id_hash})
|
| 1012 |
+
if tv:
|
| 1013 |
+
for season in tv.get("seasons", []):
|
| 1014 |
+
for episode in season.get("episodes", []):
|
| 1015 |
+
for q in episode.get("telegram", []):
|
| 1016 |
+
if q.get("id") == stream_id_hash:
|
| 1017 |
+
episode["telegram"] = [t for t in episode.get("telegram", []) if t.get("id") != stream_id_hash]
|
| 1018 |
+
if len(episode["telegram"]) == 0:
|
| 1019 |
+
season["episodes"] = [e for e in season.get("episodes", []) if e.get("episode_number") != episode.get("episode_number")]
|
| 1020 |
+
if len(season["episodes"]) == 0:
|
| 1021 |
+
tv["seasons"] = [s for s in tv.get("seasons", []) if s.get("season_number") != season.get("season_number")]
|
| 1022 |
+
if len(tv["seasons"]) == 0:
|
| 1023 |
+
await db["tv"].delete_one({"_id": tv["_id"]})
|
| 1024 |
+
return True
|
| 1025 |
+
tv['updated_on'] = datetime.utcnow()
|
| 1026 |
+
await db["tv"].replace_one({"_id": tv["_id"]}, tv)
|
| 1027 |
+
return True
|
| 1028 |
+
return False
|
| 1029 |
+
|
| 1030 |
async def delete_movie_quality(self, tmdb_id: int, db_index: int, id: str) -> bool:
|
| 1031 |
db_key = f"storage_{db_index}"
|
| 1032 |
movie = await self.dbs[db_key]["movie"].find_one({"tmdb_id": tmdb_id})
|
|
@@ -152,7 +152,7 @@ async def _tmdb_episode_details(tv_id, season, episode):
|
|
| 152 |
return None
|
| 153 |
|
| 154 |
# ----------------- Main Metadata -----------------
|
| 155 |
-
async def metadata(filename: str, channel: int, msg_id) -> dict | None:
|
| 156 |
try:
|
| 157 |
parsed = PTN.parse(filename)
|
| 158 |
except Exception as e:
|
|
@@ -191,10 +191,18 @@ async def metadata(filename: str, channel: int, msg_id) -> dict | None:
|
|
| 191 |
|
| 192 |
|
| 193 |
default_id = None
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 198 |
if not default_id:
|
| 199 |
try:
|
| 200 |
default_id = extract_default_id(filename)
|
|
|
|
| 152 |
return None
|
| 153 |
|
| 154 |
# ----------------- Main Metadata -----------------
|
| 155 |
+
async def metadata(filename: str, channel: int, msg_id, override_id: str = None) -> dict | None:
|
| 156 |
try:
|
| 157 |
parsed = PTN.parse(filename)
|
| 158 |
except Exception as e:
|
|
|
|
| 191 |
|
| 192 |
|
| 193 |
default_id = None
|
| 194 |
+
if override_id:
|
| 195 |
+
try:
|
| 196 |
+
default_id = extract_default_id(override_id) or override_id
|
| 197 |
+
except Exception:
|
| 198 |
+
pass
|
| 199 |
+
|
| 200 |
+
if not default_id:
|
| 201 |
+
try:
|
| 202 |
+
default_id = extract_default_id(Backend.USE_DEFAULT_ID)
|
| 203 |
+
except Exception:
|
| 204 |
+
pass
|
| 205 |
+
|
| 206 |
if not default_id:
|
| 207 |
try:
|
| 208 |
default_id = extract_default_id(filename)
|
|
@@ -30,7 +30,7 @@ async def subscription_checker_loop(bot: Client):
|
|
| 30 |
user_id,
|
| 31 |
"β <b>Subscription Expired</b>\n\n"
|
| 32 |
"Your subscription has expired, and you have been removed from the private group.\n"
|
| 33 |
-
"
|
| 34 |
)
|
| 35 |
LOGGER.info(f"Kicked expired user {user_id}")
|
| 36 |
except Exception as e:
|
|
@@ -46,7 +46,7 @@ async def subscription_checker_loop(bot: Client):
|
|
| 46 |
user_id,
|
| 47 |
f"β οΈ <b>Subscription Expiring Soon</b>\n\n"
|
| 48 |
f"Your subscription will expire on <b>{expiry.strftime('%Y-%m-%d %H:%M UTC')}</b>.\n"
|
| 49 |
-
"
|
| 50 |
)
|
| 51 |
await db.mark_reminder_sent(user_id)
|
| 52 |
LOGGER.info(f"Sent expiry reminder to user {user_id}")
|
|
|
|
| 30 |
user_id,
|
| 31 |
"β <b>Subscription Expired</b>\n\n"
|
| 32 |
"Your subscription has expired, and you have been removed from the private group.\n"
|
| 33 |
+
f"Please go to {Telegram.SUBSCRIPTION_URL} and send /start to renew your subscription and regain access."
|
| 34 |
)
|
| 35 |
LOGGER.info(f"Kicked expired user {user_id}")
|
| 36 |
except Exception as e:
|
|
|
|
| 46 |
user_id,
|
| 47 |
f"β οΈ <b>Subscription Expiring Soon</b>\n\n"
|
| 48 |
f"Your subscription will expire on <b>{expiry.strftime('%Y-%m-%d %H:%M UTC')}</b>.\n"
|
| 49 |
+
f"Please go to {Telegram.SUBSCRIPTION_URL} and send /start to renew your plan before you lose access to the group!"
|
| 50 |
)
|
| 51 |
await db.mark_reminder_sent(user_id)
|
| 52 |
LOGGER.info(f"Sent expiry reminder to user {user_id}")
|
|
@@ -72,3 +72,67 @@ async def file_receive_handler(client: Client, message: Message):
|
|
| 72 |
else:
|
| 73 |
await message.reply_text("> Channel is not in AUTH_CHANNEL")
|
| 74 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
else:
|
| 73 |
await message.reply_text("> Channel is not in AUTH_CHANNEL")
|
| 74 |
|
| 75 |
+
|
| 76 |
+
@Client.on_edited_message(filters.channel & (filters.document | filters.video))
|
| 77 |
+
async def file_edited_handler(client: Client, message: Message):
|
| 78 |
+
if str(message.chat.id) in Telegram.AUTH_CHANNEL:
|
| 79 |
+
try:
|
| 80 |
+
if message.video or (message.document and message.document.mime_type.startswith("video/")):
|
| 81 |
+
file = message.video or message.document
|
| 82 |
+
title = message.caption or file.file_name
|
| 83 |
+
msg_id = message.id
|
| 84 |
+
size = get_readable_file_size(file.file_size)
|
| 85 |
+
channel = str(message.chat.id).replace("-100", "")
|
| 86 |
+
|
| 87 |
+
from Backend.helper.metadata import extract_default_id
|
| 88 |
+
override_id = extract_default_id(message.caption) if message.caption else None
|
| 89 |
+
|
| 90 |
+
# Only proceed if we found a valid manual overide ID in the caption update
|
| 91 |
+
if override_id:
|
| 92 |
+
LOGGER.info(f"Detected override ID '{override_id}' in edited message {msg_id}")
|
| 93 |
+
|
| 94 |
+
from Backend.helper.encrypt import encode_string
|
| 95 |
+
stream_id_hash = await encode_string({"chat_id": int(channel), "msg_id": msg_id})
|
| 96 |
+
|
| 97 |
+
# Wipe the old streaming quality reference from the old associated media
|
| 98 |
+
await db.delete_media_by_stream_id(stream_id_hash)
|
| 99 |
+
|
| 100 |
+
# Reprocess metadata completely
|
| 101 |
+
metadata_info = await metadata(clean_filename(title), int(channel), msg_id, override_id=override_id)
|
| 102 |
+
if metadata_info is None:
|
| 103 |
+
LOGGER.warning(f"Metadata failed for edited file: {title} (ID: {msg_id})")
|
| 104 |
+
return
|
| 105 |
+
|
| 106 |
+
title = remove_urls(title)
|
| 107 |
+
if not title.endswith(('.mkv', '.mp4')):
|
| 108 |
+
title += '.mkv'
|
| 109 |
+
|
| 110 |
+
# Add the new quality to the correct DB movie/show
|
| 111 |
+
await file_queue.put((metadata_info, int(channel), msg_id, size, title))
|
| 112 |
+
else:
|
| 113 |
+
pass # ignore edits on other types
|
| 114 |
+
except Exception as e:
|
| 115 |
+
LOGGER.error(f"Error handling edited generic file {message.id}: {e}")
|
| 116 |
+
|
| 117 |
+
@Client.on_deleted_messages(filters.channel)
|
| 118 |
+
async def file_deleted_handler(client: Client, messages: list[Message]):
|
| 119 |
+
# pyrogram provides a list of deleted messages.
|
| 120 |
+
try:
|
| 121 |
+
from Backend.helper.encrypt import encode_string
|
| 122 |
+
|
| 123 |
+
for message in messages:
|
| 124 |
+
if message.chat and str(message.chat.id) in Telegram.AUTH_CHANNEL:
|
| 125 |
+
channel = str(message.chat.id).replace("-100", "")
|
| 126 |
+
msg_id = message.id
|
| 127 |
+
|
| 128 |
+
try:
|
| 129 |
+
stream_id_hash = await encode_string({"chat_id": int(channel), "msg_id": msg_id})
|
| 130 |
+
deleted = await db.delete_media_by_stream_id(stream_id_hash)
|
| 131 |
+
|
| 132 |
+
if deleted:
|
| 133 |
+
LOGGER.info(f"Automatically purged deleted message {msg_id} from database.")
|
| 134 |
+
except Exception as ex:
|
| 135 |
+
LOGGER.error(f"Failed to scrub deleted message {msg_id}: {ex}")
|
| 136 |
+
|
| 137 |
+
except Exception as e:
|
| 138 |
+
LOGGER.error(f"Error handling deleted messages: {e}")
|
|
@@ -17,58 +17,76 @@ async def send_start_message(client: Client, message: Message):
|
|
| 17 |
base_url = Telegram.BASE_URL
|
| 18 |
addon_url = f"{base_url}/stremio/manifest.json"
|
| 19 |
|
| 20 |
-
# If subscriptions are NOT enabled
|
| 21 |
-
if not Telegram.SUBSCRIPTION
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
return
|
| 23 |
|
| 24 |
-
# Subscription logic
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
if user and user.get("
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
await db.mark_user_expired(user_id)
|
| 36 |
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
return await message.reply_text(
|
| 41 |
-
'<b>Welcome to the Telegram Stremio Private Group!</b>\n\n'
|
| 42 |
-
'Currently, no subscription plans are set up. Please contact the administrator.',
|
| 43 |
-
quote=True,
|
| 44 |
-
parse_mode=enums.ParseMode.HTML
|
| 45 |
-
)
|
| 46 |
-
|
| 47 |
-
keyboard_buttons = []
|
| 48 |
-
for plan in plans:
|
| 49 |
-
keyboard_buttons.append([InlineKeyboardButton(f"{plan['days']} Days - βΉ{plan['price']}", callback_data=f"plan_{plan['_id']}")])
|
| 50 |
-
|
| 51 |
-
keyboard = InlineKeyboardMarkup(keyboard_buttons)
|
| 52 |
-
|
| 53 |
return await message.reply_text(
|
| 54 |
'<b>Welcome to the Telegram Stremio Private Group!</b>\n\n'
|
| 55 |
-
'
|
| 56 |
-
'Please select a subscription plan below to continue:',
|
| 57 |
-
reply_markup=keyboard,
|
| 58 |
quote=True,
|
| 59 |
parse_mode=enums.ParseMode.HTML
|
| 60 |
)
|
| 61 |
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
|
| 70 |
await message.reply_text(
|
| 71 |
-
'π <b>Welcome to the Telegram Stremio
|
| 72 |
'Your subscription is active. Here is your personal addon link:\n\n'
|
| 73 |
'π¬ <b>Stremio Addon β Install Link:</b>\n'
|
| 74 |
f'<code>{addon_url}</code>\n\n'
|
|
|
|
| 17 |
base_url = Telegram.BASE_URL
|
| 18 |
addon_url = f"{base_url}/stremio/manifest.json"
|
| 19 |
|
| 20 |
+
# If subscriptions are NOT enabled
|
| 21 |
+
if not Telegram.SUBSCRIPTION:
|
| 22 |
+
# Generate or fetch API token for free user
|
| 23 |
+
user_name = (message.from_user.first_name or message.from_user.username or f"User {user_id}") if message.from_user else f"Chat {user_id}"
|
| 24 |
+
try:
|
| 25 |
+
# Ensure user gets a token
|
| 26 |
+
token_doc = await db.add_api_token(name=user_name, user_id=user_id)
|
| 27 |
+
token_str = token_doc.get("token")
|
| 28 |
+
addon_url = f"{base_url}/stremio/{token_str}/manifest.json"
|
| 29 |
+
except Exception as e:
|
| 30 |
+
print(f"DEBUG: Error ensuring token for free user: {e}")
|
| 31 |
+
|
| 32 |
+
await message.reply_text(
|
| 33 |
+
'π <b>Welcome to the Telegram Stremio Media Server!</b>\n\n'
|
| 34 |
+
'Here is your personal Stremio Addon link:\n\n'
|
| 35 |
+
'π¬ <b>Stremio Addon β Install Link:</b>\n'
|
| 36 |
+
f'<code>{addon_url}</code>\n\n'
|
| 37 |
+
'Tap the link above β <b>Install</b> in Stremio to start watching!',
|
| 38 |
+
quote=True,
|
| 39 |
+
parse_mode=enums.ParseMode.HTML
|
| 40 |
+
)
|
| 41 |
return
|
| 42 |
|
| 43 |
+
# Subscription logic (When SUBSCRIPTION=True)
|
| 44 |
+
user = await db.get_user(user_id)
|
| 45 |
+
now = datetime.utcnow()
|
| 46 |
+
|
| 47 |
+
# Check if user has an active subscription
|
| 48 |
+
is_active = False
|
| 49 |
+
if user and user.get("subscription_status") == "active":
|
| 50 |
+
if user.get("subscription_expiry") and user.get("subscription_expiry") > now:
|
| 51 |
+
is_active = True
|
| 52 |
+
else:
|
| 53 |
+
await db.mark_user_expired(user_id)
|
|
|
|
| 54 |
|
| 55 |
+
if not is_active:
|
| 56 |
+
plans = await db.get_subscription_plans()
|
| 57 |
+
if not plans:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
return await message.reply_text(
|
| 59 |
'<b>Welcome to the Telegram Stremio Private Group!</b>\n\n'
|
| 60 |
+
'Currently, no subscription plans are set up. Please contact the administrator.',
|
|
|
|
|
|
|
| 61 |
quote=True,
|
| 62 |
parse_mode=enums.ParseMode.HTML
|
| 63 |
)
|
| 64 |
|
| 65 |
+
keyboard_buttons = []
|
| 66 |
+
for plan in plans:
|
| 67 |
+
keyboard_buttons.append([InlineKeyboardButton(f"{plan['days']} Days - βΉ{plan['price']}", callback_data=f"plan_{plan['_id']}")])
|
| 68 |
|
| 69 |
+
keyboard = InlineKeyboardMarkup(keyboard_buttons)
|
| 70 |
+
|
| 71 |
+
return await message.reply_text(
|
| 72 |
+
'<b>Welcome to the Telegram Stremio Private Group!</b>\n\n'
|
| 73 |
+
'Access to this bot and the Stremio Addon requires an active subscription.\n'
|
| 74 |
+
'Please select a subscription plan below to continue:',
|
| 75 |
+
reply_markup=keyboard,
|
| 76 |
+
quote=True,
|
| 77 |
+
parse_mode=enums.ParseMode.HTML
|
| 78 |
+
)
|
| 79 |
+
|
| 80 |
+
# User is active, fetch their token
|
| 81 |
+
all_tokens = await db.get_all_api_tokens()
|
| 82 |
+
token_doc = next((t for t in all_tokens if t.get("user_id") == user_id), None)
|
| 83 |
+
|
| 84 |
+
if token_doc and "token" in token_doc:
|
| 85 |
+
token_str = token_doc["token"]
|
| 86 |
+
addon_url = f"{base_url}/stremio/{token_str}/manifest.json"
|
| 87 |
|
| 88 |
await message.reply_text(
|
| 89 |
+
'π <b>Welcome back to the Telegram Stremio Subscription Manager!</b>\n\n'
|
| 90 |
'Your subscription is active. Here is your personal addon link:\n\n'
|
| 91 |
'π¬ <b>Stremio Addon β Install Link:</b>\n'
|
| 92 |
f'<code>{addon_url}</code>\n\n'
|
|
@@ -229,8 +229,27 @@ async def admin_review(client: Client, callback_query: CallbackQuery):
|
|
| 229 |
|
| 230 |
await client.send_message(target_user_id, success_text)
|
| 231 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 232 |
# Update acting admin's message
|
| 233 |
-
status_caption = f"β
<b>Approved
|
| 234 |
await callback_query.message.edit_caption(status_caption)
|
| 235 |
|
| 236 |
# Update all OTHER admins' copies
|
|
@@ -242,7 +261,7 @@ async def admin_review(client: Client, callback_query: CallbackQuery):
|
|
| 242 |
await client.edit_message_caption(
|
| 243 |
chat_id=am["chat_id"],
|
| 244 |
message_id=am["message_id"],
|
| 245 |
-
caption=
|
| 246 |
)
|
| 247 |
except Exception:
|
| 248 |
pass
|
|
@@ -257,8 +276,27 @@ async def admin_review(client: Client, callback_query: CallbackQuery):
|
|
| 257 |
"β <b>Payment Rejected</b>\n\nYour recent payment submission was rejected by the admin. Please contact the admin or try submitting again."
|
| 258 |
)
|
| 259 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 260 |
# Update acting admin's message
|
| 261 |
-
status_caption = f"β <b>Rejected
|
| 262 |
await callback_query.message.edit_caption(status_caption)
|
| 263 |
|
| 264 |
# Update all OTHER admins' copies
|
|
@@ -270,7 +308,7 @@ async def admin_review(client: Client, callback_query: CallbackQuery):
|
|
| 270 |
await client.edit_message_caption(
|
| 271 |
chat_id=am["chat_id"],
|
| 272 |
message_id=am["message_id"],
|
| 273 |
-
caption=
|
| 274 |
)
|
| 275 |
except Exception:
|
| 276 |
pass
|
|
|
|
| 229 |
|
| 230 |
await client.send_message(target_user_id, success_text)
|
| 231 |
|
| 232 |
+
# Determine how to format the user/payment info
|
| 233 |
+
duration = user_pre["pending_payment"].get("duration", "?")
|
| 234 |
+
price = user_pre["pending_payment"].get("price", "?")
|
| 235 |
+
|
| 236 |
+
try:
|
| 237 |
+
target_user = await client.get_users(target_user_id)
|
| 238 |
+
user_mention = target_user.mention
|
| 239 |
+
username_str = f"@{target_user.username}" if target_user.username else "N/A"
|
| 240 |
+
except Exception:
|
| 241 |
+
user_mention = f"User {target_user_id}"
|
| 242 |
+
username_str = "N/A"
|
| 243 |
+
|
| 244 |
+
info_text = (
|
| 245 |
+
f"π€ <b>User:</b> {user_mention}\n"
|
| 246 |
+
f"π <b>User ID:</b> <code>{target_user_id}</code>\n"
|
| 247 |
+
f"π <b>Username:</b> {username_str}\n\n"
|
| 248 |
+
f"π¦ <b>Plan:</b> {duration} days (βΉ{price})"
|
| 249 |
+
)
|
| 250 |
+
|
| 251 |
# Update acting admin's message
|
| 252 |
+
status_caption = f"β
<b>Approved by {admin_name}</b>\n\n{info_text}"
|
| 253 |
await callback_query.message.edit_caption(status_caption)
|
| 254 |
|
| 255 |
# Update all OTHER admins' copies
|
|
|
|
| 261 |
await client.edit_message_caption(
|
| 262 |
chat_id=am["chat_id"],
|
| 263 |
message_id=am["message_id"],
|
| 264 |
+
caption=status_caption
|
| 265 |
)
|
| 266 |
except Exception:
|
| 267 |
pass
|
|
|
|
| 276 |
"β <b>Payment Rejected</b>\n\nYour recent payment submission was rejected by the admin. Please contact the admin or try submitting again."
|
| 277 |
)
|
| 278 |
|
| 279 |
+
# Determine how to format the user/payment info
|
| 280 |
+
duration = user_pre["pending_payment"].get("duration", "?")
|
| 281 |
+
price = user_pre["pending_payment"].get("price", "?")
|
| 282 |
+
|
| 283 |
+
try:
|
| 284 |
+
target_user = await client.get_users(target_user_id)
|
| 285 |
+
user_mention = target_user.mention
|
| 286 |
+
username_str = f"@{target_user.username}" if target_user.username else "N/A"
|
| 287 |
+
except Exception:
|
| 288 |
+
user_mention = f"User {target_user_id}"
|
| 289 |
+
username_str = "N/A"
|
| 290 |
+
|
| 291 |
+
info_text = (
|
| 292 |
+
f"π€ <b>User:</b> {user_mention}\n"
|
| 293 |
+
f"π <b>User ID:</b> <code>{target_user_id}</code>\n"
|
| 294 |
+
f"π <b>Username:</b> {username_str}\n\n"
|
| 295 |
+
f"π¦ <b>Plan:</b> {duration} days (βΉ{price})"
|
| 296 |
+
)
|
| 297 |
+
|
| 298 |
# Update acting admin's message
|
| 299 |
+
status_caption = f"β <b>Rejected by {admin_name}</b>\n\n{info_text}"
|
| 300 |
await callback_query.message.edit_caption(status_caption)
|
| 301 |
|
| 302 |
# Update all OTHER admins' copies
|
|
|
|
| 308 |
await client.edit_message_caption(
|
| 309 |
chat_id=am["chat_id"],
|
| 310 |
message_id=am["message_id"],
|
| 311 |
+
caption=status_caption
|
| 312 |
)
|
| 313 |
except Exception:
|
| 314 |
pass
|
|
@@ -0,0 +1,98 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from pyrogram import filters, Client, enums
|
| 2 |
+
from pyrogram.types import Message, InlineKeyboardMarkup, InlineKeyboardButton
|
| 3 |
+
from Backend.config import Telegram
|
| 4 |
+
from Backend import db
|
| 5 |
+
from datetime import datetime
|
| 6 |
+
|
| 7 |
+
print("DEBUG: Subscription Bot start.py PLUGIN LOADED SUCCESSFULLY!")
|
| 8 |
+
|
| 9 |
+
@Client.on_message(filters.command('start'), group=10)
|
| 10 |
+
async def send_start_message(client: Client, message: Message):
|
| 11 |
+
try:
|
| 12 |
+
user_id = (message.from_user.id if message.from_user else None) or (message.sender_chat.id if message.sender_chat else None) or message.chat.id
|
| 13 |
+
print(f"DEBUG: Subscription Bot received /start command from {user_id}")
|
| 14 |
+
|
| 15 |
+
base_url = Telegram.BASE_URL
|
| 16 |
+
addon_url = f"{base_url}/stremio/manifest.json"
|
| 17 |
+
|
| 18 |
+
# If subscriptions are NOT enabled
|
| 19 |
+
if not Telegram.SUBSCRIPTION:
|
| 20 |
+
# Generate or fetch API token for free user
|
| 21 |
+
user_name = (message.from_user.first_name or message.from_user.username or f"User {user_id}") if message.from_user else f"Chat {user_id}"
|
| 22 |
+
try:
|
| 23 |
+
# Ensure user gets a token
|
| 24 |
+
token_doc = await db.add_api_token(name=user_name, user_id=user_id)
|
| 25 |
+
token_str = token_doc.get("token")
|
| 26 |
+
addon_url = f"{base_url}/stremio/{token_str}/manifest.json"
|
| 27 |
+
except Exception as e:
|
| 28 |
+
print(f"DEBUG: Error ensuring token for free user: {e}")
|
| 29 |
+
|
| 30 |
+
await message.reply_text(
|
| 31 |
+
'π <b>Welcome to the Telegram Stremio Media Server!</b>\n\n'
|
| 32 |
+
'Here is your personal Stremio Addon link:\n\n'
|
| 33 |
+
'π¬ <b>Stremio Addon β Install Link:</b>\n'
|
| 34 |
+
f'<code>{addon_url}</code>\n\n'
|
| 35 |
+
'Tap the link above β <b>Install</b> in Stremio to start watching!',
|
| 36 |
+
quote=True,
|
| 37 |
+
parse_mode=enums.ParseMode.HTML
|
| 38 |
+
)
|
| 39 |
+
return
|
| 40 |
+
|
| 41 |
+
# Subscription logic (When SUBSCRIPTION=True)
|
| 42 |
+
user = await db.get_user(user_id)
|
| 43 |
+
now = datetime.utcnow()
|
| 44 |
+
|
| 45 |
+
# Check if user has an active subscription
|
| 46 |
+
is_active = False
|
| 47 |
+
if user and user.get("subscription_status") == "active":
|
| 48 |
+
if user.get("subscription_expiry") and user.get("subscription_expiry") > now:
|
| 49 |
+
is_active = True
|
| 50 |
+
else:
|
| 51 |
+
await db.mark_user_expired(user_id)
|
| 52 |
+
|
| 53 |
+
if not is_active:
|
| 54 |
+
plans = await db.get_subscription_plans()
|
| 55 |
+
if not plans:
|
| 56 |
+
return await message.reply_text(
|
| 57 |
+
'<b>Welcome to the Telegram Stremio Private Group!</b>\n\n'
|
| 58 |
+
'Currently, no subscription plans are set up. Please contact the administrator.',
|
| 59 |
+
quote=True,
|
| 60 |
+
parse_mode=enums.ParseMode.HTML
|
| 61 |
+
)
|
| 62 |
+
|
| 63 |
+
keyboard_buttons = []
|
| 64 |
+
for plan in plans:
|
| 65 |
+
keyboard_buttons.append([InlineKeyboardButton(f"{plan['days']} Days - βΉ{plan['price']}", callback_data=f"plan_{plan['_id']}")])
|
| 66 |
+
|
| 67 |
+
keyboard = InlineKeyboardMarkup(keyboard_buttons)
|
| 68 |
+
|
| 69 |
+
return await message.reply_text(
|
| 70 |
+
'<b>Welcome to the Telegram Stremio Private Group!</b>\n\n'
|
| 71 |
+
'Access to the Stremio Addon requires an active subscription.\n'
|
| 72 |
+
'Please select a subscription plan below to continue:',
|
| 73 |
+
reply_markup=keyboard,
|
| 74 |
+
quote=True,
|
| 75 |
+
parse_mode=enums.ParseMode.HTML
|
| 76 |
+
)
|
| 77 |
+
|
| 78 |
+
# User is active, fetch their token
|
| 79 |
+
all_tokens = await db.get_all_api_tokens()
|
| 80 |
+
token_doc = next((t for t in all_tokens if t.get("user_id") == user_id), None)
|
| 81 |
+
|
| 82 |
+
if token_doc and "token" in token_doc:
|
| 83 |
+
token_str = token_doc["token"]
|
| 84 |
+
addon_url = f"{base_url}/stremio/{token_str}/manifest.json"
|
| 85 |
+
|
| 86 |
+
await message.reply_text(
|
| 87 |
+
'π <b>Welcome back to the Telegram Stremio Subscription Manager!</b>\n\n'
|
| 88 |
+
'Your subscription is active. Here is your personal addon link:\n\n'
|
| 89 |
+
'π¬ <b>Stremio Addon β Install Link:</b>\n'
|
| 90 |
+
f'<code>{addon_url}</code>\n\n'
|
| 91 |
+
'Tap the link above β <b>Install</b> in Stremio to start watching!',
|
| 92 |
+
quote=True,
|
| 93 |
+
parse_mode=enums.ParseMode.HTML
|
| 94 |
+
)
|
| 95 |
+
|
| 96 |
+
except Exception as e:
|
| 97 |
+
await message.reply_text(f"β οΈ Error: {e}")
|
| 98 |
+
print(f"Error in Subscription Bot /start handler: {e}")
|
|
@@ -0,0 +1,377 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from pyrogram import Client, filters
|
| 2 |
+
from pyrogram.types import Message, CallbackQuery, InlineKeyboardMarkup, InlineKeyboardButton
|
| 3 |
+
from Backend.config import Telegram
|
| 4 |
+
from Backend import db
|
| 5 |
+
from datetime import datetime, timedelta
|
| 6 |
+
import asyncio
|
| 7 |
+
|
| 8 |
+
from bson.objectid import ObjectId
|
| 9 |
+
|
| 10 |
+
@Client.on_callback_query(filters.regex(r"^plan_([a-fA-F0-9]{24})$"))
|
| 11 |
+
async def plan_selection(client: Client, callback_query: CallbackQuery):
|
| 12 |
+
if not Telegram.SUBSCRIPTION:
|
| 13 |
+
return await callback_query.answer("Subscriptions are not enabled.", show_alert=True)
|
| 14 |
+
|
| 15 |
+
plan_id = callback_query.matches[0].group(1)
|
| 16 |
+
|
| 17 |
+
plans = await db.get_subscription_plans()
|
| 18 |
+
plan = next((p for p in plans if p["_id"] == plan_id), None)
|
| 19 |
+
|
| 20 |
+
if not plan:
|
| 21 |
+
return await callback_query.answer("Invalid plan.", show_alert=True)
|
| 22 |
+
|
| 23 |
+
duration = plan["days"]
|
| 24 |
+
await callback_query.answer()
|
| 25 |
+
|
| 26 |
+
user_id = callback_query.from_user.id if callback_query.from_user else callback_query.message.chat.id
|
| 27 |
+
first_name = callback_query.from_user.first_name if callback_query.from_user else callback_query.message.chat.title
|
| 28 |
+
username = callback_query.from_user.username if callback_query.from_user else callback_query.message.chat.username
|
| 29 |
+
|
| 30 |
+
# Store initial user interaction if needed
|
| 31 |
+
await db.update_user_interaction(user_id, first_name, username)
|
| 32 |
+
|
| 33 |
+
# Calculate expiry
|
| 34 |
+
user = await db.get_user(user_id)
|
| 35 |
+
now = datetime.utcnow()
|
| 36 |
+
current_expiry = user.get("subscription_expiry") if user else None
|
| 37 |
+
|
| 38 |
+
if current_expiry and current_expiry > now:
|
| 39 |
+
new_expiry = current_expiry + timedelta(days=int(duration))
|
| 40 |
+
else:
|
| 41 |
+
new_expiry = now + timedelta(days=int(duration))
|
| 42 |
+
|
| 43 |
+
expiry_str = new_expiry.strftime("%Y-%m-%d %H:%M UTC")
|
| 44 |
+
|
| 45 |
+
text = (
|
| 46 |
+
f"<b>β
Plan Selected: {plan['days']} Days</b>\n\n"
|
| 47 |
+
f"<b>π° Price:</b> βΉ{plan['price']}\n"
|
| 48 |
+
f"<b>π
Expiry (if approved now):</b> {expiry_str}\n\n"
|
| 49 |
+
f"<b>π Payment Instructions:</b>\n"
|
| 50 |
+
f"1. Pay βΉ{plan['price']} to the admin.\n"
|
| 51 |
+
f"2. <b>Send your payment screenshot directly here (in this chat)</b>.\n"
|
| 52 |
+
f" The admin will review and activate your subscription."
|
| 53 |
+
)
|
| 54 |
+
|
| 55 |
+
# Set pending payment state (price stored for admin display)
|
| 56 |
+
await db.set_pending_payment(user_id, int(duration), 0, price=plan.get("price", 0))
|
| 57 |
+
|
| 58 |
+
# Add cancel button
|
| 59 |
+
keyboard = InlineKeyboardMarkup([
|
| 60 |
+
[InlineKeyboardButton("β Cancel", callback_data="cancel_payment")]
|
| 61 |
+
])
|
| 62 |
+
|
| 63 |
+
dm_sent = False
|
| 64 |
+
try:
|
| 65 |
+
msg = await client.send_message(
|
| 66 |
+
chat_id=user_id,
|
| 67 |
+
text=text,
|
| 68 |
+
reply_markup=keyboard,
|
| 69 |
+
)
|
| 70 |
+
dm_sent = True
|
| 71 |
+
|
| 72 |
+
async def delete_msg(message):
|
| 73 |
+
await asyncio.sleep(120)
|
| 74 |
+
try:
|
| 75 |
+
await message.delete()
|
| 76 |
+
except Exception:
|
| 77 |
+
pass
|
| 78 |
+
|
| 79 |
+
asyncio.create_task(delete_msg(msg))
|
| 80 |
+
except Exception as e:
|
| 81 |
+
print(f"Could not DM user {user_id}: {e}")
|
| 82 |
+
|
| 83 |
+
if dm_sent:
|
| 84 |
+
await callback_query.answer("β
Check your DM for payment instructions!", show_alert=True)
|
| 85 |
+
else:
|
| 86 |
+
# Fallback: reply in current chat if DM fails (user hasn't started bot)
|
| 87 |
+
fallback_msg = await callback_query.message.reply_text(
|
| 88 |
+
text + "\n\nβ οΈ <i>Please start a DM with the bot first by clicking its username, then send your screenshot there.</i>",
|
| 89 |
+
reply_markup=keyboard,
|
| 90 |
+
quote=True,
|
| 91 |
+
)
|
| 92 |
+
await callback_query.answer()
|
| 93 |
+
|
| 94 |
+
async def delete_msg(message):
|
| 95 |
+
await asyncio.sleep(120)
|
| 96 |
+
try:
|
| 97 |
+
await message.delete()
|
| 98 |
+
except Exception:
|
| 99 |
+
pass
|
| 100 |
+
|
| 101 |
+
asyncio.create_task(delete_msg(fallback_msg))
|
| 102 |
+
|
| 103 |
+
@Client.on_callback_query(filters.regex(r"^cancel_payment$"))
|
| 104 |
+
async def cancel_payment_handler(client: Client, callback_query: CallbackQuery):
|
| 105 |
+
user_id = callback_query.from_user.id if callback_query.from_user else callback_query.message.chat.id
|
| 106 |
+
|
| 107 |
+
# Remove pending payment
|
| 108 |
+
await db.reject_payment(user_id)
|
| 109 |
+
|
| 110 |
+
await callback_query.answer("Payment process cancelled.", show_alert=True)
|
| 111 |
+
try:
|
| 112 |
+
await callback_query.message.delete()
|
| 113 |
+
except Exception:
|
| 114 |
+
pass
|
| 115 |
+
|
| 116 |
+
|
| 117 |
+
@Client.on_message(filters.photo & filters.private)
|
| 118 |
+
async def handle_payment_screenshot(client: Client, message: Message):
|
| 119 |
+
if not Telegram.SUBSCRIPTION:
|
| 120 |
+
return
|
| 121 |
+
|
| 122 |
+
# Safely resolve sender ID
|
| 123 |
+
sender_id = (message.from_user.id if message.from_user else None) \
|
| 124 |
+
or (message.sender_chat.id if message.sender_chat else None) \
|
| 125 |
+
or message.chat.id
|
| 126 |
+
|
| 127 |
+
try:
|
| 128 |
+
print(f"DEBUG: handle_payment_screenshot triggered by {sender_id}")
|
| 129 |
+
# Check if user has a pending payment request
|
| 130 |
+
user = await db.get_user(sender_id)
|
| 131 |
+
print(f"DEBUG: user from DB = {user}")
|
| 132 |
+
if not user or "pending_payment" not in user:
|
| 133 |
+
# No active payment flow - tell the user what to do
|
| 134 |
+
print(f"DEBUG: No pending_payment found for {sender_id}")
|
| 135 |
+
await message.reply_text(
|
| 136 |
+
"βΉοΈ We received your photo, but you don't have an active payment request.\n\n"
|
| 137 |
+
"Please use /start to select a subscription plan first, then send your payment screenshot.",
|
| 138 |
+
quote=True
|
| 139 |
+
)
|
| 140 |
+
return
|
| 141 |
+
|
| 142 |
+
pending = user["pending_payment"]
|
| 143 |
+
duration = pending.get("duration", "?")
|
| 144 |
+
price = pending.get("price", "?")
|
| 145 |
+
|
| 146 |
+
# --- Admin notification ---
|
| 147 |
+
keyboard = InlineKeyboardMarkup([
|
| 148 |
+
[
|
| 149 |
+
InlineKeyboardButton("β
Approve", callback_data=f"approve_{sender_id}"),
|
| 150 |
+
InlineKeyboardButton("β Reject", callback_data=f"reject_{sender_id}")
|
| 151 |
+
]
|
| 152 |
+
])
|
| 153 |
+
|
| 154 |
+
user_mention = message.from_user.mention if message.from_user else f"User {sender_id}"
|
| 155 |
+
username_str = f"@{message.from_user.username}" if (message.from_user and message.from_user.username) else "N/A"
|
| 156 |
+
|
| 157 |
+
admin_text = (
|
| 158 |
+
f"<b>π° New Payment Screenshot Received</b>\n\n"
|
| 159 |
+
f"<b>π€ User:</b> {user_mention}\n"
|
| 160 |
+
f"<b>π User ID:</b> <code>{sender_id}</code>\n"
|
| 161 |
+
f"<b>π Username:</b> {username_str}\n\n"
|
| 162 |
+
f"<b>π¦ Plan Details:</b>\n"
|
| 163 |
+
f" β’ Duration: <b>{duration} days</b>\n"
|
| 164 |
+
f" β’ Price: <b>βΉ{price}</b>\n\n"
|
| 165 |
+
f"Please review the screenshot above and approve or reject."
|
| 166 |
+
)
|
| 167 |
+
|
| 168 |
+
approver_ids = Telegram.APPROVER_IDS if Telegram.APPROVER_IDS else [Telegram.OWNER_ID]
|
| 169 |
+
admin_messages = []
|
| 170 |
+
for approver_id in approver_ids:
|
| 171 |
+
try:
|
| 172 |
+
sent = await message.copy(approver_id, caption=admin_text, reply_markup=keyboard)
|
| 173 |
+
admin_messages.append({"chat_id": approver_id, "message_id": sent.id})
|
| 174 |
+
except Exception as e:
|
| 175 |
+
print(f"Failed to forward screenshot to approver {approver_id}: {e}")
|
| 176 |
+
|
| 177 |
+
# Update pending state with screenshot message ID + all admin message IDs
|
| 178 |
+
await db.set_pending_payment(sender_id, duration, message.id, price=price,
|
| 179 |
+
admin_messages=admin_messages)
|
| 180 |
+
|
| 181 |
+
if admin_messages:
|
| 182 |
+
await message.reply_text(
|
| 183 |
+
"β
<b>Screenshot Received!</b>\n\n"
|
| 184 |
+
"Your payment screenshot has been forwarded to the admin for review.\n"
|
| 185 |
+
"You will be notified once it's approved. Thank you! π",
|
| 186 |
+
quote=True
|
| 187 |
+
)
|
| 188 |
+
else:
|
| 189 |
+
await message.reply_text(
|
| 190 |
+
"β οΈ Your screenshot was received but we could not reach the admin. "
|
| 191 |
+
"Please contact the admin directly.",
|
| 192 |
+
quote=True
|
| 193 |
+
)
|
| 194 |
+
|
| 195 |
+
except Exception as e:
|
| 196 |
+
print(f"Error in handle_payment_screenshot: {e}")
|
| 197 |
+
await message.reply_text(
|
| 198 |
+
f"β οΈ Something went wrong while processing your screenshot. Please try again or contact the admin.\n\nError: {e}",
|
| 199 |
+
quote=True
|
| 200 |
+
)
|
| 201 |
+
|
| 202 |
+
|
| 203 |
+
@Client.on_callback_query(filters.regex(r"^(approve|reject)_(\d+)$"))
|
| 204 |
+
async def admin_review(client: Client, callback_query: CallbackQuery):
|
| 205 |
+
approver_ids = Telegram.APPROVER_IDS if Telegram.APPROVER_IDS else [Telegram.OWNER_ID]
|
| 206 |
+
if callback_query.from_user.id not in approver_ids:
|
| 207 |
+
return await callback_query.answer("You are not authorized to perform this action.", show_alert=True)
|
| 208 |
+
|
| 209 |
+
action = callback_query.matches[0].group(1)
|
| 210 |
+
target_user_id = int(callback_query.matches[0].group(2))
|
| 211 |
+
acting_admin = callback_query.from_user
|
| 212 |
+
admin_name = acting_admin.first_name or acting_admin.username or f"Admin {acting_admin.id}"
|
| 213 |
+
|
| 214 |
+
# Fetch admin_messages BEFORE any DB write (approve/reject unsets pending_payment)
|
| 215 |
+
user_pre = await db.get_user(target_user_id)
|
| 216 |
+
if not user_pre or "pending_payment" not in user_pre:
|
| 217 |
+
return await callback_query.answer("This request has already been processed.", show_alert=True)
|
| 218 |
+
|
| 219 |
+
admin_messages = user_pre["pending_payment"].get("admin_messages", [])
|
| 220 |
+
|
| 221 |
+
if action == "approve":
|
| 222 |
+
user_data = await db.approve_payment(target_user_id)
|
| 223 |
+
if user_data:
|
| 224 |
+
# Generate or retrieve existing API token for this user
|
| 225 |
+
try:
|
| 226 |
+
user_obj = await db.get_user(target_user_id)
|
| 227 |
+
user_name = (user_obj.get("first_name") or user_obj.get("username") or str(target_user_id)) if user_obj else str(target_user_id)
|
| 228 |
+
token_doc = await db.add_api_token(name=user_name, user_id=target_user_id)
|
| 229 |
+
token_str = token_doc.get("token")
|
| 230 |
+
addon_url = f"{Telegram.BASE_URL}/stremio/{token_str}/manifest.json"
|
| 231 |
+
except Exception as te:
|
| 232 |
+
token_str = None
|
| 233 |
+
addon_url = None
|
| 234 |
+
|
| 235 |
+
# Generate invite link for the group
|
| 236 |
+
try:
|
| 237 |
+
invite_link = await client.create_chat_invite_link(
|
| 238 |
+
chat_id=Telegram.SUBSCRIPTION_GROUP_ID,
|
| 239 |
+
member_limit=1,
|
| 240 |
+
expire_date=datetime.utcnow() + timedelta(days=1)
|
| 241 |
+
)
|
| 242 |
+
invite_text = f"\n\nπ <b>Group Invite:</b> {invite_link.invite_link}"
|
| 243 |
+
except Exception:
|
| 244 |
+
invite_text = ""
|
| 245 |
+
|
| 246 |
+
expiry_str = user_data["subscription_expiry"].strftime("%Y-%m-%d")
|
| 247 |
+
|
| 248 |
+
# Build confirmation message for user
|
| 249 |
+
success_text = (
|
| 250 |
+
f"π <b>Payment Approved!</b>\n\n"
|
| 251 |
+
f"Your subscription is now active until <b>{expiry_str}</b>."
|
| 252 |
+
f"{invite_text}"
|
| 253 |
+
)
|
| 254 |
+
if addon_url:
|
| 255 |
+
success_text += (
|
| 256 |
+
f"\n\nπ¬ <b>Stremio Addon β Install Link:</b>\n"
|
| 257 |
+
f"<code>{addon_url}</code>\n\n"
|
| 258 |
+
f"Tap the link above β <b>Install</b> in Stremio to start watching!"
|
| 259 |
+
)
|
| 260 |
+
|
| 261 |
+
await client.send_message(target_user_id, success_text)
|
| 262 |
+
|
| 263 |
+
# Determine how to format the user/payment info
|
| 264 |
+
duration = user_pre["pending_payment"].get("duration", "?")
|
| 265 |
+
price = user_pre["pending_payment"].get("price", "?")
|
| 266 |
+
|
| 267 |
+
try:
|
| 268 |
+
target_user = await client.get_users(target_user_id)
|
| 269 |
+
user_mention = target_user.mention
|
| 270 |
+
username_str = f"@{target_user.username}" if target_user.username else "N/A"
|
| 271 |
+
except Exception:
|
| 272 |
+
user_mention = f"User {target_user_id}"
|
| 273 |
+
username_str = "N/A"
|
| 274 |
+
|
| 275 |
+
info_text = (
|
| 276 |
+
f"π€ <b>User:</b> {user_mention}\n"
|
| 277 |
+
f"π <b>User ID:</b> <code>{target_user_id}</code>\n"
|
| 278 |
+
f"π <b>Username:</b> {username_str}\n\n"
|
| 279 |
+
f"π¦ <b>Plan:</b> {duration} days (βΉ{price})"
|
| 280 |
+
)
|
| 281 |
+
|
| 282 |
+
# Update acting admin's message
|
| 283 |
+
status_caption = f"β
<b>Approved by {admin_name}</b>\n\n{info_text}"
|
| 284 |
+
await callback_query.message.edit_caption(status_caption)
|
| 285 |
+
|
| 286 |
+
# Update all OTHER admins' copies
|
| 287 |
+
acting_msg_id = callback_query.message.id
|
| 288 |
+
for am in admin_messages:
|
| 289 |
+
if am["message_id"] == acting_msg_id:
|
| 290 |
+
continue
|
| 291 |
+
try:
|
| 292 |
+
await client.edit_message_caption(
|
| 293 |
+
chat_id=am["chat_id"],
|
| 294 |
+
message_id=am["message_id"],
|
| 295 |
+
caption=status_caption
|
| 296 |
+
)
|
| 297 |
+
except Exception:
|
| 298 |
+
pass
|
| 299 |
+
else:
|
| 300 |
+
await callback_query.answer("Could not approve β no pending payment found.", show_alert=True)
|
| 301 |
+
|
| 302 |
+
elif action == "reject":
|
| 303 |
+
success = await db.reject_payment(target_user_id)
|
| 304 |
+
if success:
|
| 305 |
+
await client.send_message(
|
| 306 |
+
target_user_id,
|
| 307 |
+
"β <b>Payment Rejected</b>\n\nYour recent payment submission was rejected by the admin. Please contact the admin or try submitting again."
|
| 308 |
+
)
|
| 309 |
+
|
| 310 |
+
# Determine how to format the user/payment info
|
| 311 |
+
duration = user_pre["pending_payment"].get("duration", "?")
|
| 312 |
+
price = user_pre["pending_payment"].get("price", "?")
|
| 313 |
+
|
| 314 |
+
try:
|
| 315 |
+
target_user = await client.get_users(target_user_id)
|
| 316 |
+
user_mention = target_user.mention
|
| 317 |
+
username_str = f"@{target_user.username}" if target_user.username else "N/A"
|
| 318 |
+
except Exception:
|
| 319 |
+
user_mention = f"User {target_user_id}"
|
| 320 |
+
username_str = "N/A"
|
| 321 |
+
|
| 322 |
+
info_text = (
|
| 323 |
+
f"π€ <b>User:</b> {user_mention}\n"
|
| 324 |
+
f"π <b>User ID:</b> <code>{target_user_id}</code>\n"
|
| 325 |
+
f"π <b>Username:</b> {username_str}\n\n"
|
| 326 |
+
f"π¦ <b>Plan:</b> {duration} days (βΉ{price})"
|
| 327 |
+
)
|
| 328 |
+
|
| 329 |
+
# Update acting admin's message
|
| 330 |
+
status_caption = f"β <b>Rejected by {admin_name}</b>\n\n{info_text}"
|
| 331 |
+
await callback_query.message.edit_caption(status_caption)
|
| 332 |
+
|
| 333 |
+
# Update all OTHER admins' copies
|
| 334 |
+
acting_msg_id = callback_query.message.id
|
| 335 |
+
for am in admin_messages:
|
| 336 |
+
if am["message_id"] == acting_msg_id:
|
| 337 |
+
continue
|
| 338 |
+
try:
|
| 339 |
+
await client.edit_message_caption(
|
| 340 |
+
chat_id=am["chat_id"],
|
| 341 |
+
message_id=am["message_id"],
|
| 342 |
+
caption=status_caption
|
| 343 |
+
)
|
| 344 |
+
except Exception:
|
| 345 |
+
pass
|
| 346 |
+
else:
|
| 347 |
+
await callback_query.answer("Could not reject β no pending payment found.", show_alert=True)
|
| 348 |
+
|
| 349 |
+
|
| 350 |
+
@Client.on_message(filters.command("status"))
|
| 351 |
+
async def check_status(client: Client, message: Message):
|
| 352 |
+
if not Telegram.SUBSCRIPTION:
|
| 353 |
+
return
|
| 354 |
+
|
| 355 |
+
user_id = (message.from_user.id if message.from_user else None) or (message.sender_chat.id if message.sender_chat else None) or message.chat.id
|
| 356 |
+
|
| 357 |
+
user = await db.get_user(user_id)
|
| 358 |
+
if not user or user.get("subscription_status") != "active":
|
| 359 |
+
return await message.reply_text("You do not have an active subscription.")
|
| 360 |
+
|
| 361 |
+
expiry = user.get("subscription_expiry")
|
| 362 |
+
if not expiry:
|
| 363 |
+
return await message.reply_text("Error retrieving expiry date.")
|
| 364 |
+
|
| 365 |
+
now = datetime.utcnow()
|
| 366 |
+
if now > expiry:
|
| 367 |
+
return await message.reply_text("Your subscription has expired.")
|
| 368 |
+
|
| 369 |
+
remaining = expiry - now
|
| 370 |
+
days = remaining.days
|
| 371 |
+
hours = remaining.seconds // 3600
|
| 372 |
+
|
| 373 |
+
await message.reply_text(
|
| 374 |
+
f"<b>Subscription Status:</b> Active β
\n"
|
| 375 |
+
f"<b>Expiry Date:</b> {expiry.strftime('%Y-%m-%d %H:%M UTC')}\n"
|
| 376 |
+
f"<b>Time Remaining:</b> {days} days and {hours} hours"
|
| 377 |
+
)
|
|
@@ -86,6 +86,11 @@ This project is a **next-generation Telegram Stremio Media Server** that allows
|
|
| 86 |
- π **Bot-wise Analysis** β Detailed bot performance analytics available in the admin dashboard for monitoring and optimization.
|
| 87 |
- π§Ή **Deleted File Detection** β Automatic detection of deleted files on every restart, with admin capability to manually remove them from the database.
|
| 88 |
- π οΈ **Additional Admin Features** β Various small enhancements and improvements for administrators.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
|
| 90 |
|
| 91 |
## βοΈ How It Works
|
|
@@ -166,6 +171,18 @@ If you initially uploaded a **CAMRip or low-quality version**, you can easily re
|
|
| 166 |
|
| 167 |
---
|
| 168 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 169 |
|
| 170 |
### Behind The Scenes
|
| 171 |
|
|
@@ -708,4 +725,3 @@ If you want to use **only** your **Telegram Stremio Media Server addon** for met
|
|
| 708 |
|:---:|:---:|:---:|:---:|
|
| 709 |
|[`Karan`](https://github.com/Weebzone)|[`Stremio`](https://github.com/Stremio)|[`ChatGPT`](https://github.com/OPENAI)|[`VFlix Prime`](https://t.me/vflixprime2)|
|
| 710 |
|Author|Stremio SDK|Refactor|Community Support
|
| 711 |
-
|
|
|
|
| 86 |
- π **Bot-wise Analysis** β Detailed bot performance analytics available in the admin dashboard for monitoring and optimization.
|
| 87 |
- π§Ή **Deleted File Detection** β Automatic detection of deleted files on every restart, with admin capability to manually remove them from the database.
|
| 88 |
- π οΈ **Additional Admin Features** β Various small enhancements and improvements for administrators.
|
| 89 |
+
- π **Free Mode Toggle** β Administrators can turn off the `SUBSCRIPTION` requirement in `config.env` to allow all users immediate access via an automatically generated API token.
|
| 90 |
+
- π **Automatic Stream Cleanup** β Deleting a source message in the Telegram channel instantly deletes all corresponding streams and qualities from the Stremio Addon Database and Admin Panel, preventing dead links.
|
| 91 |
+
- π·οΈ **Manual IMDb/TMDb Override** β Users can instantly update or fix incorrect metadata for a file by simply editing the Telegram channel message caption and pasting the correct IMDb/TMDB URL.
|
| 92 |
+
|
| 93 |
+
|
| 94 |
|
| 95 |
|
| 96 |
## βοΈ How It Works
|
|
|
|
| 171 |
|
| 172 |
---
|
| 173 |
|
| 174 |
+
### π·οΈ Fixing Incorrect Metadata (Manual Override)
|
| 175 |
+
|
| 176 |
+
If the addon incorrectly identifies a movie or TV show, or if the metadata is entirely missing, you can fix it effortlessly by editing the message in your Telegram channel:
|
| 177 |
+
|
| 178 |
+
1. Copy the correct **IMDb URL** or **TMDB URL** for the movie/show.
|
| 179 |
+
2. Edit the message caption in your Telegram **AUTH CHANNEL** and paste the URL.
|
| 180 |
+
3. The bot will automatically wipe the old, incorrect database entry for that file and instantly re-fetch the metadata using your provided link.
|
| 181 |
+
|
| 182 |
+
β
The Stremio addon catalog will update dynamically to reflect the correctly identified media.
|
| 183 |
+
|
| 184 |
+
---
|
| 185 |
+
|
| 186 |
|
| 187 |
### Behind The Scenes
|
| 188 |
|
|
|
|
| 725 |
|:---:|:---:|:---:|:---:|
|
| 726 |
|[`Karan`](https://github.com/Weebzone)|[`Stremio`](https://github.com/Stremio)|[`ChatGPT`](https://github.com/OPENAI)|[`VFlix Prime`](https://t.me/vflixprime2)|
|
| 727 |
|Author|Stremio SDK|Refactor|Community Support
|
|
|