-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmy_bot.py
More file actions
206 lines (182 loc) · 9.01 KB
/
my_bot.py
File metadata and controls
206 lines (182 loc) · 9.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import logging
from telegram import Update, ReplyKeyboardRemove
from telegram.ext import ApplicationBuilder, ContextTypes, CommandHandler, MessageHandler, filters
import aiohttp
import os
import uuid
import asyncio
import json
TOKEN = os.getenv("BOT_TOKEN")
SUPABASE_URL = os.getenv("SUPABASE_URL")
SUPABASE_KEY = os.getenv("SUPABASE_KEY")
ADMIN_ID = int(os.getenv("ADMIN_ID"))
logging.basicConfig(level=logging.INFO)
# Utility to save data to Supabase
async def save_to_supabase(table, data):
url = f"{SUPABASE_URL}/rest/v1/{table}"
headers = {
"apikey": SUPABASE_KEY,
"Authorization": f"Bearer {SUPABASE_KEY}",
"Content-Type": "application/json",
"Prefer": "return=minimal"
}
async with aiohttp.ClientSession() as session:
async with session.post(url, headers=headers, json=data) as resp:
resp.raise_for_status()
return await resp.text()
# Start command
async def start(update: Update, context: ContextTypes.DEFAULT_TYPE):
if update.effective_user.id != ADMIN_ID:
await update.message.reply_text("⛔️ You are not authorized to use this bot.")
return
context.user_data.clear()
await update.message.reply_text("🎬 Movie/Series Add Wizard Started!\nPlease enter the *title*:", parse_mode="Markdown")
context.user_data['step'] = 'title'
# Handle messages
async def handle_message(update: Update, context: ContextTypes.DEFAULT_TYPE):
if update.effective_user.id != ADMIN_ID:
return
step = context.user_data.get('step')
text = update.message.text.strip()
# Step-by-step wizard
if step == 'title':
context.user_data['title'] = text
await update.message.reply_text("Please enter the *poster URL*:", parse_mode="Markdown")
context.user_data['step'] = 'poster_url'
elif step == 'poster_url':
context.user_data['poster_url'] = text
await update.message.reply_text("Please enter the *description*:")
context.user_data['step'] = 'description'
elif step == 'description':
context.user_data['description'] = text
await update.message.reply_text("Please enter the *year* of release:")
context.user_data['step'] = 'year'
elif step == 'year':
context.user_data['year'] = text
await update.message.reply_text("Please enter the *genre* (comma separated):")
context.user_data['step'] = 'genre'
elif step == 'genre':
context.user_data['genre'] = text
await update.message.reply_text("Please enter the *category* (comma separated or JSON array):")
context.user_data['step'] = 'category'
elif step == 'category':
# Convert comma-separated to list for JSONB
context.user_data['category'] = [c.strip() for c in text.split(',')]
await update.message.reply_text("Please enter *preview URLs* (comma separated) or type 'None':")
context.user_data['step'] = 'preview1'
elif step == 'preview1':
if text.lower() == 'none':
context.user_data['preview1'] = {}
else:
urls = [u.strip() for u in text.split(',')]
context.user_data['preview1'] = {"preview": urls}
await update.message.reply_text("Is this a *Movie* or *WebSeries*?")
context.user_data['step'] = 'download_type'
elif step == 'download_type':
if text.lower() not in ['movie', 'webseries']:
await update.message.reply_text("Invalid type. Please enter 'Movie' or 'WebSeries'.")
return
context.user_data['download_type'] = text.lower()
context.user_data['Dow'] = {} # initialize download JSON
if text.lower() == 'movie':
await update.message.reply_text("Please enter 480p download link (or type 'No link'):")
context.user_data['step'] = 'movie_480'
else:
await update.message.reply_text("How many seasons does this series have?")
context.user_data['step'] = 'webseries_seasons'
# ---------------- Movie download links ----------------
elif step == 'movie_480':
context.user_data['Dow']['480p'] = None if text.lower() == 'no link' else text
await update.message.reply_text("Please enter 720p download link (or type 'No link'):")
context.user_data['step'] = 'movie_720'
elif step == 'movie_720':
context.user_data['Dow']['720p'] = None if text.lower() == 'no link' else text
await update.message.reply_text("Please enter 1080p download link (or type 'No link'):")
context.user_data['step'] = 'movie_1080'
elif step == 'movie_1080':
context.user_data['Dow']['1080p'] = None if text.lower() == 'no link' else text
# Save to Supabase
movie_data = {
"title": context.user_data['title'],
"poster_url": context.user_data['poster_url'],
"description": context.user_data['description'],
"year": context.user_data['year'],
"genre": context.user_data['genre'],
"category": context.user_data['category'],
"preview1": context.user_data['preview1'],
"download_type": "movie",
"Dow": context.user_data['Dow']
}
try:
await save_to_supabase("Movies", movie_data)
await update.message.reply_text("✅ Movie successfully added to Supabase!")
except Exception as e:
await update.message.reply_text(f"❌ Error saving movie: {e}")
context.user_data.clear()
# ---------------- WebSeries flow ----------------
elif step == 'webseries_seasons':
try:
num_seasons = int(text)
except:
await update.message.reply_text("Please enter a valid number of seasons:")
return
context.user_data['num_seasons'] = num_seasons
context.user_data['current_season'] = 1
context.user_data['Dow'] = {}
await update.message.reply_text(f"Enter episode info for Season 1 (e.g., '1' or '1-5' or 'Full Season'):")
context.user_data['step'] = 'webseries_episodes'
elif step == 'webseries_episodes':
season = f"Season {context.user_data['current_season']}"
if season not in context.user_data['Dow']:
context.user_data['Dow'][season] = {}
context.user_data['current_episodes'] = text
context.user_data['current_quality'] = '480p'
await update.message.reply_text(f"Enter 480p link for {season} Episode(s) {text} (or type 'No link'):")
context.user_data['step'] = 'webseries_quality'
elif step == 'webseries_quality':
season = f"Season {context.user_data['current_season']}"
ep = context.user_data['current_episodes']
quality = context.user_data['current_quality']
if quality not in context.user_data['Dow'][season]:
context.user_data['Dow'][season][quality] = {}
context.user_data['Dow'][season][quality][ep] = None if update.message.text.lower() == 'no link' else update.message.text
# Move to next quality
if quality == '480p':
context.user_data['current_quality'] = '720p'
await update.message.reply_text(f"Enter 720p link for {season} Episode(s) {ep} (or type 'No link'):")
elif quality == '720p':
context.user_data['current_quality'] = '1080p'
await update.message.reply_text(f"Enter 1080p link for {season} Episode(s) {ep} (or type 'No link'):")
else:
# Move to next season or finish
if context.user_data['current_season'] < context.user_data['num_seasons']:
context.user_data['current_season'] += 1
await update.message.reply_text(f"Enter episode info for Season {context.user_data['current_season']} (e.g., '1' or '1-5' or 'Full Season'):")
context.user_data['step'] = 'webseries_episodes'
else:
# Save to Supabase
movie_data = {
"title": context.user_data['title'],
"poster_url": context.user_data['poster_url'],
"description": context.user_data['description'],
"year": context.user_data['year'],
"genre": context.user_data['genre'],
"category": context.user_data['category'],
"preview1": context.user_data['preview1'],
"download_type": "webseries",
"Dow": context.user_data['Dow']
}
try:
await save_to_supabase("Movies", movie_data)
await update.message.reply_text("✅ WebSeries successfully added to Supabase!")
except Exception as e:
await update.message.reply_text(f"❌ Error saving series: {e}")
context.user_data.clear()
# ---------------- Main ----------------
def main():
app = ApplicationBuilder().token(TOKEN).build()
app.add_handler(CommandHandler("start", start))
app.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, handle_message))
app.run_polling()
if __name__ == '__main__':
main()