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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
|
|
|
| 8 |
|
| 9 |
-
|
| 10 |
-
|
| 11 |
|
| 12 |
-
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
BOT_TOKEN = os.getenv("tgtkn")
|
| 3 |
+
GROUP_ID = -1003799294466
|
| 4 |
+
|
| 5 |
+
import time
|
| 6 |
+
from datetime import datetime
|
| 7 |
+
from telegram import *
|
| 8 |
+
from telegram.ext import *
|
| 9 |
from mdb import *
|
| 10 |
|
| 11 |
+
waiting_for_file = set()
|
| 12 |
+
PAGE_SIZE = 5
|
| 13 |
+
|
| 14 |
+
# =========================
|
| 15 |
+
# π§ DATABASE
|
| 16 |
+
# =========================
|
| 17 |
+
|
| 18 |
+
def get_data(user_id):
|
| 19 |
+
key = f"user_{user_id}"
|
| 20 |
+
data = get(key, "json")
|
| 21 |
+
if not data:
|
| 22 |
+
return {"files": [], "index": {}}
|
| 23 |
+
return data
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
def save_data(user_id, data):
|
| 27 |
+
key = f"user_{user_id}"
|
| 28 |
+
set(key, "json", data)
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
# =========================
|
| 32 |
+
# π START
|
| 33 |
+
# =========================
|
| 34 |
+
|
| 35 |
+
async def start(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
| 36 |
+
kb = [
|
| 37 |
+
[InlineKeyboardButton("π€ Upload", callback_data="upload")],
|
| 38 |
+
[InlineKeyboardButton("π My Files", callback_data="files_0")],
|
| 39 |
+
[InlineKeyboardButton("π Stats", callback_data="stats")]
|
| 40 |
+
]
|
| 41 |
+
await update.message.reply_text(
|
| 42 |
+
"βοΈ Advanced Cloud Bot",
|
| 43 |
+
reply_markup=InlineKeyboardMarkup(kb)
|
| 44 |
+
)
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
# =========================
|
| 48 |
+
# π PAGINATION
|
| 49 |
+
# =========================
|
| 50 |
+
|
| 51 |
+
def build_file_page(data, page):
|
| 52 |
+
files = data["files"]
|
| 53 |
+
start = page * PAGE_SIZE
|
| 54 |
+
end = start + PAGE_SIZE
|
| 55 |
+
|
| 56 |
+
chunk = files[start:end]
|
| 57 |
+
|
| 58 |
+
text = "π Your Files:\n\n"
|
| 59 |
+
kb = []
|
| 60 |
+
|
| 61 |
+
for f in chunk:
|
| 62 |
+
fname = f["file_name"]
|
| 63 |
+
text += f"β’ {fname}\n"
|
| 64 |
+
|
| 65 |
+
kb.append([
|
| 66 |
+
InlineKeyboardButton("βΉοΈ", callback_data=f"info_{fname}"),
|
| 67 |
+
InlineKeyboardButton("π", callback_data=f"del_{fname}")
|
| 68 |
+
])
|
| 69 |
+
|
| 70 |
+
nav = []
|
| 71 |
+
if page > 0:
|
| 72 |
+
nav.append(InlineKeyboardButton("β¬
οΈ", callback_data=f"files_{page-1}"))
|
| 73 |
+
if end < len(files):
|
| 74 |
+
nav.append(InlineKeyboardButton("β‘οΈ", callback_data=f"files_{page+1}"))
|
| 75 |
+
|
| 76 |
+
if nav:
|
| 77 |
+
kb.append(nav)
|
| 78 |
+
|
| 79 |
+
return text, InlineKeyboardMarkup(kb)
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
# =========================
|
| 83 |
+
# π BUTTONS
|
| 84 |
+
# =========================
|
| 85 |
+
|
| 86 |
+
async def buttons(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
| 87 |
+
q = update.callback_query
|
| 88 |
+
await q.answer()
|
| 89 |
+
user_id = q.from_user.id
|
| 90 |
+
data = get_data(user_id)
|
| 91 |
+
|
| 92 |
+
# π€ upload
|
| 93 |
+
if q.data == "upload":
|
| 94 |
+
waiting_for_file.add(user_id)
|
| 95 |
+
await q.message.reply_text("π Send file")
|
| 96 |
+
|
| 97 |
+
# π files pagination
|
| 98 |
+
elif q.data.startswith("files_"):
|
| 99 |
+
page = int(q.data.split("_")[1])
|
| 100 |
+
text, kb = build_file_page(data, page)
|
| 101 |
+
await q.message.reply_text(text, reply_markup=kb)
|
| 102 |
+
|
| 103 |
+
# π stats
|
| 104 |
+
elif q.data == "stats":
|
| 105 |
+
await q.message.reply_text(f"π Files: {len(data['files'])}")
|
| 106 |
+
|
| 107 |
+
# π delete confirm
|
| 108 |
+
elif q.data.startswith("del_"):
|
| 109 |
+
fname = q.data.split("_", 1)[1]
|
| 110 |
+
kb = [
|
| 111 |
+
[InlineKeyboardButton("β
Confirm", callback_data=f"confirmdel_{fname}")],
|
| 112 |
+
[InlineKeyboardButton("β Cancel", callback_data="files_0")]
|
| 113 |
+
]
|
| 114 |
+
await q.message.reply_text(f"Delete {fname}?", reply_markup=InlineKeyboardMarkup(kb))
|
| 115 |
+
|
| 116 |
+
elif q.data.startswith("confirmdel_"):
|
| 117 |
+
fname = q.data.split("_", 1)[1]
|
| 118 |
+
|
| 119 |
+
if fname in data["index"]:
|
| 120 |
+
data["files"] = [f for f in data["files"] if f["file_name"] != fname]
|
| 121 |
+
del data["index"][fname]
|
| 122 |
+
save_data(user_id, data)
|
| 123 |
+
|
| 124 |
+
await q.message.reply_text("π Deleted")
|
| 125 |
+
|
| 126 |
+
# βΉοΈ info
|
| 127 |
+
elif q.data.startswith("info_"):
|
| 128 |
+
fname = q.data.split("_", 1)[1]
|
| 129 |
+
|
| 130 |
+
if fname in data["index"]:
|
| 131 |
+
f = data["index"][fname]
|
| 132 |
+
|
| 133 |
+
kb = [
|
| 134 |
+
[InlineKeyboardButton("π Share", callback_data=f"share_{fname}")],
|
| 135 |
+
[InlineKeyboardButton("β Close", callback_data="files_0")]
|
| 136 |
+
]
|
| 137 |
+
|
| 138 |
+
text = (
|
| 139 |
+
f"π File Info\n\n"
|
| 140 |
+
f"Name: {f['original_name']}\n"
|
| 141 |
+
f"Saved As: {f['file_name']}\n"
|
| 142 |
+
f"Size: {f['file_size']} bytes\n"
|
| 143 |
+
f"Uploaded: {f['upload_date']}\n"
|
| 144 |
+
f"Shared With: {len(f.get('shared_with', []))} users"
|
| 145 |
+
)
|
| 146 |
+
|
| 147 |
+
await q.message.reply_text(text, reply_markup=InlineKeyboardMarkup(kb))
|
| 148 |
+
|
| 149 |
+
# π share
|
| 150 |
+
elif q.data.startswith("share_"):
|
| 151 |
+
fname = q.data.split("_", 1)[1]
|
| 152 |
+
|
| 153 |
+
if fname in data["index"]:
|
| 154 |
+
f = data["index"][fname]
|
| 155 |
+
link = f"https://t.me/{context.bot.username}?start=share_{fname}_{user_id}"
|
| 156 |
+
|
| 157 |
+
await q.message.reply_text(f"π Share Link:\n{link}")
|
| 158 |
+
|
| 159 |
+
|
| 160 |
+
# =========================
|
| 161 |
+
# π€ FILE UPLOAD
|
| 162 |
+
# =========================
|
| 163 |
+
|
| 164 |
+
async def handle_file(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
| 165 |
+
user_id = update.effective_user.id
|
| 166 |
+
|
| 167 |
+
if user_id not in waiting_for_file:
|
| 168 |
+
return
|
| 169 |
+
|
| 170 |
+
doc = update.message.document
|
| 171 |
+
if not doc:
|
| 172 |
+
return
|
| 173 |
+
|
| 174 |
+
data = get_data(user_id)
|
| 175 |
+
|
| 176 |
+
original = doc.file_name
|
| 177 |
+
ext = original.split('.')[-1] if '.' in original else "dat"
|
| 178 |
+
|
| 179 |
+
ts = int(time.time())
|
| 180 |
+
new_name = f"{user_id}_{ts}.{ext}"
|
| 181 |
+
|
| 182 |
+
sent = await context.bot.send_document(
|
| 183 |
+
chat_id=GROUP_ID,
|
| 184 |
+
document=doc.file_id,
|
| 185 |
+
filename=new_name
|
| 186 |
+
)
|
| 187 |
+
|
| 188 |
+
entry = {
|
| 189 |
+
"file_name": new_name,
|
| 190 |
+
"original_name": original,
|
| 191 |
+
"file_id": sent.document.file_id,
|
| 192 |
+
"file_size": doc.file_size,
|
| 193 |
+
"upload_date": datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
|
| 194 |
+
"shared_with": []
|
| 195 |
+
}
|
| 196 |
+
|
| 197 |
+
data["files"].append(entry)
|
| 198 |
+
data["index"][new_name] = entry
|
| 199 |
+
|
| 200 |
+
save_data(user_id, data)
|
| 201 |
+
|
| 202 |
+
waiting_for_file.discard(user_id)
|
| 203 |
+
await update.message.reply_text("β
Uploaded")
|
| 204 |
+
|
| 205 |
+
|
| 206 |
+
# =========================
|
| 207 |
+
# π SEARCH
|
| 208 |
+
# =========================
|
| 209 |
+
|
| 210 |
+
async def search(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
| 211 |
+
user_id = update.effective_user.id
|
| 212 |
+
data = get_data(user_id)
|
| 213 |
+
|
| 214 |
+
if not context.args:
|
| 215 |
+
return await update.message.reply_text("Usage: /search name")
|
| 216 |
+
|
| 217 |
+
query = context.args[0].lower()
|
| 218 |
+
|
| 219 |
+
results = [
|
| 220 |
+
f for f in data["files"]
|
| 221 |
+
if query in f["file_name"].lower() or query in f["original_name"].lower()
|
| 222 |
+
]
|
| 223 |
+
|
| 224 |
+
if not results:
|
| 225 |
+
return await update.message.reply_text("β No results")
|
| 226 |
+
|
| 227 |
+
msg = "π Results:\n\n"
|
| 228 |
+
for f in results[:10]:
|
| 229 |
+
msg += f"β’ {f['file_name']}\n"
|
| 230 |
+
|
| 231 |
+
await update.message.reply_text(msg)
|
| 232 |
+
|
| 233 |
+
|
| 234 |
+
# =========================
|
| 235 |
+
# π₯ GET
|
| 236 |
+
# =========================
|
| 237 |
+
|
| 238 |
+
async def get_file(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
| 239 |
+
user_id = update.effective_user.id
|
| 240 |
+
data = get_data(user_id)
|
| 241 |
+
|
| 242 |
+
if not context.args:
|
| 243 |
+
return
|
| 244 |
+
|
| 245 |
+
fname = context.args[0]
|
| 246 |
+
|
| 247 |
+
if fname in data["index"]:
|
| 248 |
+
await context.bot.send_document(
|
| 249 |
+
chat_id=update.effective_chat.id,
|
| 250 |
+
document=data["index"][fname]["file_id"]
|
| 251 |
+
)
|
| 252 |
+
else:
|
| 253 |
+
await update.message.reply_text("β Not found")
|
| 254 |
+
|
| 255 |
+
|
| 256 |
+
# =========================
|
| 257 |
+
# π RUN
|
| 258 |
+
# =========================
|
| 259 |
+
|
| 260 |
+
app = ApplicationBuilder().token(BOT_TOKEN).build()
|
| 261 |
|
| 262 |
+
app.add_handler(CommandHandler("start", start))
|
| 263 |
+
app.add_handler(CommandHandler("search", search))
|
| 264 |
+
app.add_handler(CommandHandler("get", get_file))
|
| 265 |
|
| 266 |
+
app.add_handler(CallbackQueryHandler(buttons))
|
| 267 |
+
app.add_handler(MessageHandler(filters.Document.ALL, handle_file))
|
| 268 |
|
| 269 |
+
print("π₯ ADVANCED BOT RUNNING...")
|
| 270 |
+
app.run_polling()
|