Update app.py
Browse files
app.py
CHANGED
|
@@ -16,11 +16,20 @@ from pyrogram.errors import MessageNotModified, FloodWait
|
|
| 16 |
from huggingface_hub import HfApi, upload_file, hf_hub_url
|
| 17 |
from huggingface_hub.utils import EntryNotFoundError
|
| 18 |
|
| 19 |
-
# ---------- LOGGING ----------
|
| 20 |
-
logging.basicConfig(
|
|
|
|
|
|
|
|
|
|
| 21 |
logger = logging.getLogger("bot")
|
| 22 |
logger.setLevel(logging.INFO)
|
| 23 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
# ---------- ENVIRONMENT VALIDATION ----------
|
| 25 |
REQUIRED_VARS = {
|
| 26 |
"BOT_TOKEN": "Telegram bot token",
|
|
@@ -29,18 +38,21 @@ REQUIRED_VARS = {
|
|
| 29 |
"HF_TOKEN": "Hugging Face token with write access",
|
| 30 |
"SECRET_KEY": "Secret passphrase for encryption",
|
| 31 |
}
|
|
|
|
|
|
|
| 32 |
for var, desc in REQUIRED_VARS.items():
|
| 33 |
if var not in os.environ or not os.environ[var].strip():
|
| 34 |
-
logger.error(f"Missing {var} ({desc})")
|
| 35 |
exit(1)
|
|
|
|
|
|
|
| 36 |
try:
|
| 37 |
api_id = int(os.environ["API_ID"])
|
|
|
|
| 38 |
except ValueError:
|
| 39 |
-
logger.error("API_ID must be an integer")
|
| 40 |
exit(1)
|
| 41 |
|
| 42 |
-
logger.info("β
All environment variables are set and valid")
|
| 43 |
-
|
| 44 |
BOT_TOKEN = os.environ["BOT_TOKEN"]
|
| 45 |
API_HASH = os.environ["API_HASH"]
|
| 46 |
HF_TOKEN = os.environ["HF_TOKEN"]
|
|
@@ -49,9 +61,14 @@ SECRET_KEY = os.environ["SECRET_KEY"]
|
|
| 49 |
HF_REPO_ID = "Cxops/Bzxv"
|
| 50 |
HF_REPO_TYPE = "dataset"
|
| 51 |
HF_BRANCH = "main"
|
| 52 |
-
MIN_FREE_DISK = 45 * 1024**3
|
| 53 |
-
SPLIT_SIZE = 2 * 1024**3 - 10*1024*1024
|
| 54 |
-
PROGRESS_INTERVAL = 3
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
|
| 56 |
# ---------- ENCRYPTION ----------
|
| 57 |
SALT = b"huggingbot_salt_2025"
|
|
@@ -59,6 +76,7 @@ kdf = PBKDF2HMAC(algorithm=hashes.SHA256(), length=32, salt=SALT,
|
|
| 59 |
iterations=100_000, backend=default_backend())
|
| 60 |
key = base64.urlsafe_b64encode(kdf.derive(SECRET_KEY.encode()))
|
| 61 |
fernet = Fernet(key)
|
|
|
|
| 62 |
|
| 63 |
def encrypt(data: str) -> str:
|
| 64 |
return base64.urlsafe_b64encode(fernet.encrypt(data.encode())).decode().rstrip("=")
|
|
@@ -73,38 +91,54 @@ def get_user_folder(user_id: int) -> str:
|
|
| 73 |
return encrypt(str(user_id))
|
| 74 |
|
| 75 |
# ---------- HUGGING FACE ----------
|
| 76 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
|
| 78 |
def hf_read_json(path: str) -> Optional[dict]:
|
| 79 |
try:
|
| 80 |
-
|
|
|
|
| 81 |
except EntryNotFoundError:
|
|
|
|
| 82 |
return None
|
| 83 |
except Exception as e:
|
| 84 |
-
logger.error(f"hf_read_json error: {e}")
|
| 85 |
return None
|
| 86 |
|
| 87 |
def hf_write_json(path: str, data: dict):
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
|
| 94 |
def hf_delete_file(path: str):
|
| 95 |
try:
|
| 96 |
hf_api.delete_file(path, repo_id=HF_REPO_ID, repo_type=HF_REPO_TYPE,
|
| 97 |
commit_message=f"delete {path}")
|
|
|
|
| 98 |
except Exception as e:
|
| 99 |
-
logger.error(f"hf_delete_file error: {e}")
|
| 100 |
|
| 101 |
def hf_file_size(path: str) -> Optional[int]:
|
| 102 |
try:
|
| 103 |
-
|
|
|
|
| 104 |
except EntryNotFoundError:
|
| 105 |
return None
|
| 106 |
except Exception as e:
|
| 107 |
-
logger.error(f"hf_file_size error: {e}")
|
| 108 |
return None
|
| 109 |
|
| 110 |
def free_disk_gb() -> float:
|
|
@@ -113,7 +147,7 @@ def free_disk_gb() -> float:
|
|
| 113 |
def enough_disk(needed: int = 0) -> bool:
|
| 114 |
return (shutil.disk_usage("/").free - needed) >= MIN_FREE_DISK
|
| 115 |
|
| 116 |
-
# ---------- SIMPLE HTTP SERVER
|
| 117 |
class HealthHandler(BaseHTTPRequestHandler):
|
| 118 |
def do_GET(self):
|
| 119 |
self.send_response(200)
|
|
@@ -123,14 +157,20 @@ class HealthHandler(BaseHTTPRequestHandler):
|
|
| 123 |
pass
|
| 124 |
|
| 125 |
def start_http_server():
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
|
|
|
|
|
|
|
|
|
| 129 |
|
| 130 |
-
# ---------- BOT CLIENT ----------
|
|
|
|
| 131 |
for f in Path(".").glob("bot.session*"):
|
| 132 |
f.unlink()
|
| 133 |
-
logger.info(f"Removed old session file: {f}")
|
|
|
|
|
|
|
| 134 |
|
| 135 |
app = Client("bot", api_id=api_id, api_hash=API_HASH, bot_token=BOT_TOKEN)
|
| 136 |
|
|
@@ -183,7 +223,7 @@ async def progress_updater():
|
|
| 183 |
except Exception as e:
|
| 184 |
logger.error(f"progress_updater error: {e}")
|
| 185 |
|
| 186 |
-
# ---------- UPLOAD
|
| 187 |
async def handle_upload(op: dict):
|
| 188 |
user_id = op["user_id"]
|
| 189 |
chat_id = op["chat_id"]
|
|
@@ -192,6 +232,7 @@ async def handle_upload(op: dict):
|
|
| 192 |
msg_id = op["status_msg_id"]
|
| 193 |
cancel_event = op["cancel_event"]
|
| 194 |
|
|
|
|
| 195 |
active_progress[msg_id] = {
|
| 196 |
"chat_id": chat_id,
|
| 197 |
"state": "downloading_from_tg",
|
|
@@ -204,6 +245,7 @@ async def handle_upload(op: dict):
|
|
| 204 |
}
|
| 205 |
|
| 206 |
while not enough_disk():
|
|
|
|
| 207 |
try:
|
| 208 |
await app.edit_message_text(chat_id, msg_id,
|
| 209 |
f"β³ Waiting for disk space... (free: {free_disk_gb():.1f} GB, need β₯45 GB)")
|
|
@@ -211,12 +253,14 @@ async def handle_upload(op: dict):
|
|
| 211 |
pass
|
| 212 |
await asyncio.sleep(10)
|
| 213 |
if cancel_event.is_set():
|
|
|
|
| 214 |
await app.edit_message_text(chat_id, msg_id, "β Cancelled (disk wait).")
|
| 215 |
del active_progress[msg_id]
|
| 216 |
return
|
| 217 |
|
| 218 |
tmp_fd, tmp_path = tempfile.mkstemp(suffix="_" + file_name)
|
| 219 |
os.close(tmp_fd)
|
|
|
|
| 220 |
try:
|
| 221 |
def tg_progress(current, total, mid):
|
| 222 |
info = active_progress.get(mid)
|
|
@@ -228,7 +272,7 @@ async def handle_upload(op: dict):
|
|
| 228 |
await app.download_media(file_id, file_name=tmp_path,
|
| 229 |
progress=tg_progress, progress_args=(msg_id,))
|
| 230 |
except Exception as e:
|
| 231 |
-
logger.error(f"
|
| 232 |
if os.path.exists(tmp_path):
|
| 233 |
os.unlink(tmp_path)
|
| 234 |
await app.edit_message_text(chat_id, msg_id, f"β Download error: {e}")
|
|
@@ -236,6 +280,7 @@ async def handle_upload(op: dict):
|
|
| 236 |
return
|
| 237 |
|
| 238 |
if cancel_event.is_set():
|
|
|
|
| 239 |
if os.path.exists(tmp_path):
|
| 240 |
os.unlink(tmp_path)
|
| 241 |
await app.edit_message_text(chat_id, msg_id, "β Cancelled.")
|
|
@@ -250,6 +295,7 @@ async def handle_upload(op: dict):
|
|
| 250 |
enc_fname = encrypt(file_name)
|
| 251 |
hf_path = f"{folder}/{enc_fname}"
|
| 252 |
meta_path = f"{folder}/meta.json"
|
|
|
|
| 253 |
|
| 254 |
def hf_progress(current, total, mid):
|
| 255 |
info = active_progress.get(mid)
|
|
@@ -269,7 +315,7 @@ async def handle_upload(op: dict):
|
|
| 269 |
progress_callback_msg_id=msg_id,
|
| 270 |
)
|
| 271 |
except Exception as e:
|
| 272 |
-
logger.error(f"HF upload
|
| 273 |
if os.path.exists(tmp_path):
|
| 274 |
os.unlink(tmp_path)
|
| 275 |
await app.edit_message_text(chat_id, msg_id, f"β Upload error: {e}")
|
|
@@ -277,6 +323,7 @@ async def handle_upload(op: dict):
|
|
| 277 |
return
|
| 278 |
|
| 279 |
if cancel_event.is_set():
|
|
|
|
| 280 |
hf_delete_file(hf_path)
|
| 281 |
meta = hf_read_json(meta_path) or {}
|
| 282 |
meta.pop(enc_fname, None)
|
|
@@ -292,7 +339,7 @@ async def handle_upload(op: dict):
|
|
| 292 |
try:
|
| 293 |
hf_write_json(meta_path, meta)
|
| 294 |
except Exception as e:
|
| 295 |
-
logger.error(f"
|
| 296 |
await app.edit_message_text(chat_id, msg_id,
|
| 297 |
f"β
Uploaded, but meta update failed: {e}")
|
| 298 |
if os.path.exists(tmp_path):
|
|
@@ -304,156 +351,16 @@ async def handle_upload(op: dict):
|
|
| 304 |
os.unlink(tmp_path)
|
| 305 |
await app.edit_message_text(chat_id, msg_id, f"β
Uploaded `{file_name}` successfully.")
|
| 306 |
del active_progress[msg_id]
|
|
|
|
| 307 |
|
| 308 |
-
#
|
| 309 |
-
async def handle_download(op: dict):
|
| 310 |
-
user_id = op["user_id"]
|
| 311 |
-
chat_id = op["chat_id"]
|
| 312 |
-
folder = op["folder"]
|
| 313 |
-
enc_fname = op["enc_fname"]
|
| 314 |
-
orig_name = op["orig_name"]
|
| 315 |
-
file_size = op["file_size"]
|
| 316 |
-
msg_id = op["status_msg_id"]
|
| 317 |
-
cancel_event = op["cancel_event"]
|
| 318 |
|
| 319 |
-
|
| 320 |
-
"chat_id": chat_id,
|
| 321 |
-
"state": "downloading_from_hf",
|
| 322 |
-
"filename": orig_name,
|
| 323 |
-
"current": 0,
|
| 324 |
-
"total": file_size if file_size else 0,
|
| 325 |
-
"start_time": time.time(),
|
| 326 |
-
"cancel_event": cancel_event,
|
| 327 |
-
"op_type": "Download",
|
| 328 |
-
}
|
| 329 |
-
|
| 330 |
-
url = hf_hub_url(HF_REPO_ID, f"{folder}/{enc_fname}",
|
| 331 |
-
repo_type=HF_REPO_TYPE, revision=HF_BRANCH)
|
| 332 |
-
|
| 333 |
-
async def send_file(path: str, caption: str):
|
| 334 |
-
active_progress[msg_id]["state"] = "sending_to_tg"
|
| 335 |
-
active_progress[msg_id]["total"] = os.path.getsize(path)
|
| 336 |
-
await app.send_document(chat_id, path, caption=caption,
|
| 337 |
-
progress=lambda c, t: upload_progress(c, t, msg_id))
|
| 338 |
-
if cancel_event.is_set():
|
| 339 |
-
await app.stop_transmission()
|
| 340 |
-
|
| 341 |
-
def upload_progress(current, total, mid):
|
| 342 |
-
info = active_progress.get(mid)
|
| 343 |
-
if info:
|
| 344 |
-
info["current"] = current
|
| 345 |
-
info["total"] = total
|
| 346 |
-
info["start_time"] = time.time()
|
| 347 |
-
|
| 348 |
-
if file_size is None or file_size <= SPLIT_SIZE:
|
| 349 |
-
tmp_fd, tmp_path = tempfile.mkstemp(suffix=f"_{orig_name}")
|
| 350 |
-
os.close(tmp_fd)
|
| 351 |
-
try:
|
| 352 |
-
success = await download_from_url(url, tmp_path, msg_id, cancel_event, file_size)
|
| 353 |
-
if not success:
|
| 354 |
-
return
|
| 355 |
-
await send_file(tmp_path, orig_name)
|
| 356 |
-
if cancel_event.is_set():
|
| 357 |
-
await app.edit_message_text(chat_id, msg_id, "β Send cancelled.")
|
| 358 |
-
del active_progress[msg_id]
|
| 359 |
-
return
|
| 360 |
-
await app.edit_message_text(chat_id, msg_id, f"β
Sent `{orig_name}`.")
|
| 361 |
-
finally:
|
| 362 |
-
if os.path.exists(tmp_path):
|
| 363 |
-
os.unlink(tmp_path)
|
| 364 |
-
del active_progress[msg_id]
|
| 365 |
-
else:
|
| 366 |
-
total_parts = math.ceil(file_size / SPLIT_SIZE)
|
| 367 |
-
for part_idx in range(total_parts):
|
| 368 |
-
if cancel_event.is_set():
|
| 369 |
-
await app.edit_message_text(chat_id, msg_id, "β Cancelled.")
|
| 370 |
-
del active_progress[msg_id]
|
| 371 |
-
return
|
| 372 |
-
start = part_idx * SPLIT_SIZE
|
| 373 |
-
end = min(start + SPLIT_SIZE - 1, file_size - 1)
|
| 374 |
-
part_size = end - start + 1
|
| 375 |
-
part_name = f"{orig_name}.part{part_idx+1:03d}"
|
| 376 |
-
active_progress[msg_id]["state"] = f"downloading part {part_idx+1}/{total_parts}"
|
| 377 |
-
active_progress[msg_id]["current"] = 0
|
| 378 |
-
active_progress[msg_id]["total"] = part_size
|
| 379 |
-
tmp_fd, tmp_path = tempfile.mkstemp(suffix=f"_{part_name}")
|
| 380 |
-
os.close(tmp_fd)
|
| 381 |
-
try:
|
| 382 |
-
success = await download_range(url, start, end, tmp_path, msg_id, cancel_event, part_size)
|
| 383 |
-
if not success:
|
| 384 |
-
return
|
| 385 |
-
active_progress[msg_id]["state"] = f"sending part {part_idx+1}/{total_parts}"
|
| 386 |
-
await send_file(tmp_path, f"{orig_name} - Part {part_idx+1}/{total_parts}")
|
| 387 |
-
if cancel_event.is_set():
|
| 388 |
-
await app.edit_message_text(chat_id, msg_id, "β Send cancelled.")
|
| 389 |
-
del active_progress[msg_id]
|
| 390 |
-
return
|
| 391 |
-
finally:
|
| 392 |
-
if os.path.exists(tmp_path):
|
| 393 |
-
os.unlink(tmp_path)
|
| 394 |
-
await app.edit_message_text(chat_id, msg_id, f"β
All {total_parts} parts sent.")
|
| 395 |
-
del active_progress[msg_id]
|
| 396 |
-
|
| 397 |
-
async def download_from_url(url: str, dest: str, msg_id: int,
|
| 398 |
-
cancel_event: asyncio.Event,
|
| 399 |
-
total_size: Optional[int]) -> bool:
|
| 400 |
-
try:
|
| 401 |
-
async with aiohttp.ClientSession() as session:
|
| 402 |
-
async with session.get(url) as resp:
|
| 403 |
-
if resp.status != 200:
|
| 404 |
-
raise Exception(f"HTTP {resp.status}")
|
| 405 |
-
total = total_size or int(resp.headers.get("Content-Length", 0))
|
| 406 |
-
info = active_progress.get(msg_id)
|
| 407 |
-
if info:
|
| 408 |
-
info["total"] = total
|
| 409 |
-
downloaded = 0
|
| 410 |
-
with open(dest, "wb") as f:
|
| 411 |
-
async for chunk in resp.content.iter_chunked(64*1024):
|
| 412 |
-
if cancel_event.is_set():
|
| 413 |
-
return False
|
| 414 |
-
f.write(chunk)
|
| 415 |
-
downloaded += len(chunk)
|
| 416 |
-
if info:
|
| 417 |
-
info["current"] = downloaded
|
| 418 |
-
info["start_time"] = time.time()
|
| 419 |
-
return True
|
| 420 |
-
except Exception as e:
|
| 421 |
-
logger.error(f"download_from_url error: {e}")
|
| 422 |
-
await app.edit_message_text(msg_id, f"β Download error: {e}")
|
| 423 |
-
return False
|
| 424 |
-
|
| 425 |
-
async def download_range(url: str, start: int, end: int, dest: str,
|
| 426 |
-
msg_id: int, cancel_event: asyncio.Event,
|
| 427 |
-
size: int) -> bool:
|
| 428 |
-
headers = {"Range": f"bytes={start}-{end}"}
|
| 429 |
-
try:
|
| 430 |
-
async with aiohttp.ClientSession() as session:
|
| 431 |
-
async with session.get(url, headers=headers) as resp:
|
| 432 |
-
if resp.status not in (200, 206):
|
| 433 |
-
raise Exception(f"HTTP {resp.status}")
|
| 434 |
-
info = active_progress.get(msg_id)
|
| 435 |
-
if info:
|
| 436 |
-
info["total"] = size
|
| 437 |
-
downloaded = 0
|
| 438 |
-
with open(dest, "wb") as f:
|
| 439 |
-
async for chunk in resp.content.iter_chunked(64*1024):
|
| 440 |
-
if cancel_event.is_set():
|
| 441 |
-
return False
|
| 442 |
-
f.write(chunk)
|
| 443 |
-
downloaded += len(chunk)
|
| 444 |
-
if info:
|
| 445 |
-
info["current"] = downloaded
|
| 446 |
-
info["start_time"] = time.time()
|
| 447 |
-
return True
|
| 448 |
-
except Exception as e:
|
| 449 |
-
logger.error(f"download_range error: {e}")
|
| 450 |
-
await app.edit_message_text(msg_id, f"β Download error: {e}")
|
| 451 |
-
return False
|
| 452 |
|
| 453 |
# ---------- TELEGRAM HANDLERS ----------
|
| 454 |
@app.on_message(filters.command("start"))
|
| 455 |
async def start_cmd(_, msg: Message):
|
| 456 |
-
logger.info(f"/start from {msg.from_user.id}")
|
| 457 |
await msg.reply(
|
| 458 |
"π **Welcome to Cloud Storage Bot!**\n\n"
|
| 459 |
"Send any file to upload it to the cloud.\n"
|
|
@@ -471,173 +378,53 @@ async def help_cmd(_, msg: Message):
|
|
| 471 |
"Send any file to upload, use /files to download."
|
| 472 |
)
|
| 473 |
|
| 474 |
-
|
| 475 |
-
async def list_files(_, msg: Message):
|
| 476 |
-
user_id = msg.from_user.id
|
| 477 |
-
meta = hf_read_json(f"{get_user_folder(user_id)}/meta.json")
|
| 478 |
-
if not meta:
|
| 479 |
-
await msg.reply("π No files yet.")
|
| 480 |
-
return
|
| 481 |
-
kb = [[InlineKeyboardButton(name, callback_data=f"dl_{enc}")] for enc, name in meta.items()]
|
| 482 |
-
kb.append([InlineKeyboardButton("π Refresh", callback_data="files_refresh")])
|
| 483 |
-
await msg.reply("π Your files:", reply_markup=InlineKeyboardMarkup(kb))
|
| 484 |
-
|
| 485 |
-
@app.on_callback_query(filters.regex("^files_refresh$"))
|
| 486 |
-
async def refresh_files(_, query: CallbackQuery):
|
| 487 |
-
await query.answer("Refreshingβ¦")
|
| 488 |
-
await list_files(_, query.message)
|
| 489 |
-
await query.message.delete()
|
| 490 |
-
|
| 491 |
-
@app.on_callback_query(filters.regex("^dl_(.+)"))
|
| 492 |
-
async def download_file_cb(_, query: CallbackQuery):
|
| 493 |
-
enc = query.data.split("_", 1)[1]
|
| 494 |
-
user_id = query.from_user.id
|
| 495 |
-
folder = get_user_folder(user_id)
|
| 496 |
-
meta = hf_read_json(f"{folder}/meta.json")
|
| 497 |
-
if not meta or enc not in meta:
|
| 498 |
-
await query.answer("File not found!", show_alert=True)
|
| 499 |
-
return
|
| 500 |
-
orig_name = meta[enc]
|
| 501 |
-
size = hf_file_size(f"{folder}/{enc}")
|
| 502 |
-
await query.answer("Starting downloadβ¦")
|
| 503 |
-
st = await query.message.reply(f"β³ Preparing download: `{orig_name}`β¦")
|
| 504 |
-
cancel_ev = asyncio.Event()
|
| 505 |
-
active_progress[st.id] = {
|
| 506 |
-
"chat_id": st.chat.id, "filename": orig_name,
|
| 507 |
-
"current": 0, "total": 0, "start_time": time.time(),
|
| 508 |
-
"cancel_event": cancel_ev, "op_type": "Download", "state": "starting"
|
| 509 |
-
}
|
| 510 |
-
await op_queue.put({
|
| 511 |
-
"type": "download", "user_id": user_id, "chat_id": st.chat.id,
|
| 512 |
-
"folder": folder, "enc_fname": enc, "orig_name": orig_name,
|
| 513 |
-
"file_size": size, "status_msg_id": st.id, "cancel_event": cancel_ev
|
| 514 |
-
})
|
| 515 |
-
|
| 516 |
-
@app.on_callback_query(filters.regex("^cancel_op_(\\d+)$"))
|
| 517 |
-
async def cancel_operation(_, query: CallbackQuery):
|
| 518 |
-
msg_id = int(query.data.split("_")[2])
|
| 519 |
-
info = active_progress.get(msg_id)
|
| 520 |
-
if info and "cancel_event" in info:
|
| 521 |
-
info["cancel_event"].set()
|
| 522 |
-
await query.answer("Cancellingβ¦")
|
| 523 |
-
else:
|
| 524 |
-
await query.answer("Nothing to cancel.", show_alert=True)
|
| 525 |
-
|
| 526 |
-
@app.on_message(filters.media)
|
| 527 |
-
async def media_handler(_, msg: Message):
|
| 528 |
-
logger.info(f"Media from {msg.from_user.id}")
|
| 529 |
-
file_id = None
|
| 530 |
-
file_name = "file"
|
| 531 |
-
file_size = 0
|
| 532 |
-
|
| 533 |
-
if msg.document:
|
| 534 |
-
file_id = msg.document.file_id
|
| 535 |
-
file_name = msg.document.file_name or "document"
|
| 536 |
-
file_size = msg.document.file_size
|
| 537 |
-
elif msg.video:
|
| 538 |
-
file_id = msg.video.file_id
|
| 539 |
-
file_name = msg.video.file_name or "video.mp4"
|
| 540 |
-
file_size = msg.video.file_size
|
| 541 |
-
elif msg.audio:
|
| 542 |
-
file_id = msg.audio.file_id
|
| 543 |
-
file_name = msg.audio.file_name or "audio.mp3"
|
| 544 |
-
file_size = msg.audio.file_size
|
| 545 |
-
elif msg.photo:
|
| 546 |
-
file_id = msg.photo.file_id
|
| 547 |
-
file_name = f"photo_{msg.photo.file_unique_id}.jpg"
|
| 548 |
-
file_size = msg.photo.file_size or 0
|
| 549 |
-
elif msg.voice:
|
| 550 |
-
file_id = msg.voice.file_id
|
| 551 |
-
file_name = f"voice_{msg.voice.file_unique_id}.ogg"
|
| 552 |
-
file_size = msg.voice.file_size
|
| 553 |
-
elif msg.video_note:
|
| 554 |
-
file_id = msg.video_note.file_id
|
| 555 |
-
file_name = f"vn_{msg.video_note.file_unique_id}.mp4"
|
| 556 |
-
file_size = msg.video_note.file_size
|
| 557 |
-
elif msg.sticker:
|
| 558 |
-
file_id = msg.sticker.file_id
|
| 559 |
-
file_name = f"sticker_{msg.sticker.file_unique_id}.webp"
|
| 560 |
-
file_size = msg.sticker.file_size
|
| 561 |
-
else:
|
| 562 |
-
return
|
| 563 |
-
|
| 564 |
-
if not file_id:
|
| 565 |
-
return
|
| 566 |
-
|
| 567 |
-
prompt = await msg.reply(
|
| 568 |
-
f"π `{file_name}` ({format_bytes(file_size) if file_size else 'unknown size'})\n"
|
| 569 |
-
"Do you want to upload this file?",
|
| 570 |
-
reply_markup=InlineKeyboardMarkup([[
|
| 571 |
-
InlineKeyboardButton("β
Upload", callback_data=f"upload_{msg.id}"),
|
| 572 |
-
InlineKeyboardButton("β Cancel", callback_data=f"cancel_prompt_{msg.id}")
|
| 573 |
-
]]),
|
| 574 |
-
quote=True,
|
| 575 |
-
)
|
| 576 |
-
pending_uploads[prompt.id] = {
|
| 577 |
-
"file_id": file_id,
|
| 578 |
-
"file_name": file_name,
|
| 579 |
-
"file_size": file_size,
|
| 580 |
-
"user_id": msg.from_user.id,
|
| 581 |
-
}
|
| 582 |
-
|
| 583 |
-
@app.on_callback_query(filters.regex("^upload_(\\d+)$"))
|
| 584 |
-
async def confirm_upload(_, query: CallbackQuery):
|
| 585 |
-
info = pending_uploads.pop(query.message.id, None)
|
| 586 |
-
if not info:
|
| 587 |
-
await query.answer("Session expired, please send the file again.", show_alert=True)
|
| 588 |
-
return
|
| 589 |
-
await query.answer()
|
| 590 |
-
await query.message.delete()
|
| 591 |
-
st = await query.message.reply(f"β³ Uploading `{info['file_name']}`β¦")
|
| 592 |
-
cancel_ev = asyncio.Event()
|
| 593 |
-
await op_queue.put({
|
| 594 |
-
"type": "upload",
|
| 595 |
-
"user_id": info["user_id"],
|
| 596 |
-
"chat_id": query.message.chat.id,
|
| 597 |
-
"file_id": info["file_id"],
|
| 598 |
-
"file_name": info["file_name"],
|
| 599 |
-
"status_msg_id": st.id,
|
| 600 |
-
"cancel_event": cancel_ev,
|
| 601 |
-
})
|
| 602 |
-
|
| 603 |
-
@app.on_callback_query(filters.regex("^cancel_prompt_(\\d+)$"))
|
| 604 |
-
async def cancel_prompt(_, query: CallbackQuery):
|
| 605 |
-
pending_uploads.pop(query.message.id, None)
|
| 606 |
-
await query.answer("Cancelled.")
|
| 607 |
-
await query.message.delete()
|
| 608 |
|
| 609 |
# ---------- WORKER ----------
|
| 610 |
async def worker():
|
|
|
|
| 611 |
while True:
|
| 612 |
op = await op_queue.get()
|
|
|
|
| 613 |
try:
|
| 614 |
if op["type"] == "upload":
|
| 615 |
await handle_upload(op)
|
| 616 |
else:
|
| 617 |
await handle_download(op)
|
| 618 |
except Exception as e:
|
| 619 |
-
logger.exception(f"Worker error: {e}")
|
| 620 |
finally:
|
| 621 |
op_queue.task_done()
|
| 622 |
|
| 623 |
-
# ---------- STARTUP (
|
| 624 |
@app.on_startup()
|
| 625 |
async def on_startup():
|
|
|
|
| 626 |
# Start background tasks
|
| 627 |
asyncio.create_task(worker())
|
| 628 |
asyncio.create_task(progress_updater())
|
| 629 |
|
| 630 |
# Print bot info
|
| 631 |
-
|
| 632 |
-
|
| 633 |
-
|
| 634 |
-
|
| 635 |
-
|
| 636 |
-
|
| 637 |
-
|
| 638 |
-
|
|
|
|
|
|
|
|
|
|
| 639 |
if __name__ == "__main__":
|
|
|
|
| 640 |
# Start the HTTP health server in a daemon thread
|
| 641 |
threading.Thread(target=start_http_server, daemon=True).start()
|
| 642 |
-
#
|
| 643 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
from huggingface_hub import HfApi, upload_file, hf_hub_url
|
| 17 |
from huggingface_hub.utils import EntryNotFoundError
|
| 18 |
|
| 19 |
+
# ---------- DETAILED LOGGING ----------
|
| 20 |
+
logging.basicConfig(
|
| 21 |
+
level=logging.INFO,
|
| 22 |
+
format="%(asctime)s [%(levelname)s] %(name)s: %(message)s",
|
| 23 |
+
)
|
| 24 |
logger = logging.getLogger("bot")
|
| 25 |
logger.setLevel(logging.INFO)
|
| 26 |
|
| 27 |
+
# Suppress overly verbose logs from other libraries
|
| 28 |
+
logging.getLogger("pyrogram").setLevel(logging.WARNING)
|
| 29 |
+
logging.getLogger("huggingface_hub").setLevel(logging.WARNING)
|
| 30 |
+
logging.getLogger("aiohttp").setLevel(logging.WARNING)
|
| 31 |
+
logging.getLogger("urllib3").setLevel(logging.WARNING)
|
| 32 |
+
|
| 33 |
# ---------- ENVIRONMENT VALIDATION ----------
|
| 34 |
REQUIRED_VARS = {
|
| 35 |
"BOT_TOKEN": "Telegram bot token",
|
|
|
|
| 38 |
"HF_TOKEN": "Hugging Face token with write access",
|
| 39 |
"SECRET_KEY": "Secret passphrase for encryption",
|
| 40 |
}
|
| 41 |
+
|
| 42 |
+
logger.info("π Checking environment variables...")
|
| 43 |
for var, desc in REQUIRED_VARS.items():
|
| 44 |
if var not in os.environ or not os.environ[var].strip():
|
| 45 |
+
logger.error(f"β Missing environment variable: {var} ({desc})")
|
| 46 |
exit(1)
|
| 47 |
+
logger.info(f"β
{var} is set")
|
| 48 |
+
|
| 49 |
try:
|
| 50 |
api_id = int(os.environ["API_ID"])
|
| 51 |
+
logger.info(f"β
API_ID is valid integer: {api_id}")
|
| 52 |
except ValueError:
|
| 53 |
+
logger.error("β API_ID must be an integer")
|
| 54 |
exit(1)
|
| 55 |
|
|
|
|
|
|
|
| 56 |
BOT_TOKEN = os.environ["BOT_TOKEN"]
|
| 57 |
API_HASH = os.environ["API_HASH"]
|
| 58 |
HF_TOKEN = os.environ["HF_TOKEN"]
|
|
|
|
| 61 |
HF_REPO_ID = "Cxops/Bzxv"
|
| 62 |
HF_REPO_TYPE = "dataset"
|
| 63 |
HF_BRANCH = "main"
|
| 64 |
+
MIN_FREE_DISK = 45 * 1024**3 # 45 GB
|
| 65 |
+
SPLIT_SIZE = 2 * 1024**3 - 10*1024*1024 # ~2 GiB
|
| 66 |
+
PROGRESS_INTERVAL = 3 # seconds
|
| 67 |
+
|
| 68 |
+
logger.info(f"π¦ Hugging Face repo: {HF_REPO_ID}")
|
| 69 |
+
logger.info(f"πΎ Minimum free disk: {MIN_FREE_DISK // (1024**3)} GB")
|
| 70 |
+
logger.info(f"βοΈ Split size: {SPLIT_SIZE // (1024**2)} MB")
|
| 71 |
+
logger.info(f"β±οΈ Progress update interval: {PROGRESS_INTERVAL} sec")
|
| 72 |
|
| 73 |
# ---------- ENCRYPTION ----------
|
| 74 |
SALT = b"huggingbot_salt_2025"
|
|
|
|
| 76 |
iterations=100_000, backend=default_backend())
|
| 77 |
key = base64.urlsafe_b64encode(kdf.derive(SECRET_KEY.encode()))
|
| 78 |
fernet = Fernet(key)
|
| 79 |
+
logger.info("π Encryption initialized")
|
| 80 |
|
| 81 |
def encrypt(data: str) -> str:
|
| 82 |
return base64.urlsafe_b64encode(fernet.encrypt(data.encode())).decode().rstrip("=")
|
|
|
|
| 91 |
return encrypt(str(user_id))
|
| 92 |
|
| 93 |
# ---------- HUGGING FACE ----------
|
| 94 |
+
logger.info("π Connecting to Hugging Face...")
|
| 95 |
+
try:
|
| 96 |
+
hf_api = HfApi(token=HF_TOKEN)
|
| 97 |
+
# Quick test: check if repo exists
|
| 98 |
+
hf_api.repo_info(HF_REPO_ID, repo_type=HF_REPO_TYPE)
|
| 99 |
+
logger.info(f"β
Successfully connected to Hugging Face repo: {HF_REPO_ID}")
|
| 100 |
+
except Exception as e:
|
| 101 |
+
logger.error(f"β Hugging Face connection failed: {e}")
|
| 102 |
+
exit(1)
|
| 103 |
|
| 104 |
def hf_read_json(path: str) -> Optional[dict]:
|
| 105 |
try:
|
| 106 |
+
content = hf_api.read_file(HF_REPO_ID, path, repo_type=HF_REPO_TYPE, revision=HF_BRANCH)
|
| 107 |
+
return json.loads(content)
|
| 108 |
except EntryNotFoundError:
|
| 109 |
+
logger.debug(f"File not found: {path}")
|
| 110 |
return None
|
| 111 |
except Exception as e:
|
| 112 |
+
logger.error(f"β hf_read_json error for {path}: {e}")
|
| 113 |
return None
|
| 114 |
|
| 115 |
def hf_write_json(path: str, data: dict):
|
| 116 |
+
try:
|
| 117 |
+
hf_api.upload_file(
|
| 118 |
+
path_or_fileobj=json.dumps(data, indent=2).encode(),
|
| 119 |
+
path_in_repo=path, repo_id=HF_REPO_ID, repo_type=HF_REPO_TYPE,
|
| 120 |
+
commit_message=f"update {path}"
|
| 121 |
+
)
|
| 122 |
+
logger.debug(f"Uploaded metadata: {path}")
|
| 123 |
+
except Exception as e:
|
| 124 |
+
logger.error(f"β hf_write_json error for {path}: {e}")
|
| 125 |
|
| 126 |
def hf_delete_file(path: str):
|
| 127 |
try:
|
| 128 |
hf_api.delete_file(path, repo_id=HF_REPO_ID, repo_type=HF_REPO_TYPE,
|
| 129 |
commit_message=f"delete {path}")
|
| 130 |
+
logger.debug(f"Deleted: {path}")
|
| 131 |
except Exception as e:
|
| 132 |
+
logger.error(f"β hf_delete_file error for {path}: {e}")
|
| 133 |
|
| 134 |
def hf_file_size(path: str) -> Optional[int]:
|
| 135 |
try:
|
| 136 |
+
info = hf_api.get_file_metadata(HF_REPO_ID, path, repo_type=HF_REPO_TYPE)
|
| 137 |
+
return info.size
|
| 138 |
except EntryNotFoundError:
|
| 139 |
return None
|
| 140 |
except Exception as e:
|
| 141 |
+
logger.error(f"β hf_file_size error for {path}: {e}")
|
| 142 |
return None
|
| 143 |
|
| 144 |
def free_disk_gb() -> float:
|
|
|
|
| 147 |
def enough_disk(needed: int = 0) -> bool:
|
| 148 |
return (shutil.disk_usage("/").free - needed) >= MIN_FREE_DISK
|
| 149 |
|
| 150 |
+
# ---------- SIMPLE HTTP SERVER ON PORT 7860 ----------
|
| 151 |
class HealthHandler(BaseHTTPRequestHandler):
|
| 152 |
def do_GET(self):
|
| 153 |
self.send_response(200)
|
|
|
|
| 157 |
pass
|
| 158 |
|
| 159 |
def start_http_server():
|
| 160 |
+
try:
|
| 161 |
+
server = HTTPServer(("0.0.0.0", 7860), HealthHandler)
|
| 162 |
+
logger.info("π HTTP health server started on port 7860")
|
| 163 |
+
server.serve_forever()
|
| 164 |
+
except Exception as e:
|
| 165 |
+
logger.error(f"β Failed to start HTTP server: {e}")
|
| 166 |
|
| 167 |
+
# ---------- BOT CLIENT (fresh session) ----------
|
| 168 |
+
logger.info("π§Ή Cleaning old session files...")
|
| 169 |
for f in Path(".").glob("bot.session*"):
|
| 170 |
f.unlink()
|
| 171 |
+
logger.info(f"ποΈ Removed old session file: {f}")
|
| 172 |
+
if not any(Path(".").glob("bot.session*")):
|
| 173 |
+
logger.info("β
No old sessions to clean")
|
| 174 |
|
| 175 |
app = Client("bot", api_id=api_id, api_hash=API_HASH, bot_token=BOT_TOKEN)
|
| 176 |
|
|
|
|
| 223 |
except Exception as e:
|
| 224 |
logger.error(f"progress_updater error: {e}")
|
| 225 |
|
| 226 |
+
# ---------- UPLOAD / DOWNLOAD (same as before, with added logs) ----------
|
| 227 |
async def handle_upload(op: dict):
|
| 228 |
user_id = op["user_id"]
|
| 229 |
chat_id = op["chat_id"]
|
|
|
|
| 232 |
msg_id = op["status_msg_id"]
|
| 233 |
cancel_event = op["cancel_event"]
|
| 234 |
|
| 235 |
+
logger.info(f"β¬οΈ Starting upload: {file_name} for user {user_id}")
|
| 236 |
active_progress[msg_id] = {
|
| 237 |
"chat_id": chat_id,
|
| 238 |
"state": "downloading_from_tg",
|
|
|
|
| 245 |
}
|
| 246 |
|
| 247 |
while not enough_disk():
|
| 248 |
+
logger.warning(f"β³ Not enough disk space. Free: {free_disk_gb():.1f} GB")
|
| 249 |
try:
|
| 250 |
await app.edit_message_text(chat_id, msg_id,
|
| 251 |
f"β³ Waiting for disk space... (free: {free_disk_gb():.1f} GB, need β₯45 GB)")
|
|
|
|
| 253 |
pass
|
| 254 |
await asyncio.sleep(10)
|
| 255 |
if cancel_event.is_set():
|
| 256 |
+
logger.info("Upload cancelled by user (disk wait)")
|
| 257 |
await app.edit_message_text(chat_id, msg_id, "β Cancelled (disk wait).")
|
| 258 |
del active_progress[msg_id]
|
| 259 |
return
|
| 260 |
|
| 261 |
tmp_fd, tmp_path = tempfile.mkstemp(suffix="_" + file_name)
|
| 262 |
os.close(tmp_fd)
|
| 263 |
+
logger.debug(f"Downloading to temp file: {tmp_path}")
|
| 264 |
try:
|
| 265 |
def tg_progress(current, total, mid):
|
| 266 |
info = active_progress.get(mid)
|
|
|
|
| 272 |
await app.download_media(file_id, file_name=tmp_path,
|
| 273 |
progress=tg_progress, progress_args=(msg_id,))
|
| 274 |
except Exception as e:
|
| 275 |
+
logger.error(f"β Telegram download failed: {e}")
|
| 276 |
if os.path.exists(tmp_path):
|
| 277 |
os.unlink(tmp_path)
|
| 278 |
await app.edit_message_text(chat_id, msg_id, f"β Download error: {e}")
|
|
|
|
| 280 |
return
|
| 281 |
|
| 282 |
if cancel_event.is_set():
|
| 283 |
+
logger.info("Upload cancelled by user (after download)")
|
| 284 |
if os.path.exists(tmp_path):
|
| 285 |
os.unlink(tmp_path)
|
| 286 |
await app.edit_message_text(chat_id, msg_id, "β Cancelled.")
|
|
|
|
| 295 |
enc_fname = encrypt(file_name)
|
| 296 |
hf_path = f"{folder}/{enc_fname}"
|
| 297 |
meta_path = f"{folder}/meta.json"
|
| 298 |
+
logger.info(f"π€ Uploading to HF: {hf_path}")
|
| 299 |
|
| 300 |
def hf_progress(current, total, mid):
|
| 301 |
info = active_progress.get(mid)
|
|
|
|
| 315 |
progress_callback_msg_id=msg_id,
|
| 316 |
)
|
| 317 |
except Exception as e:
|
| 318 |
+
logger.error(f"β HF upload failed: {e}")
|
| 319 |
if os.path.exists(tmp_path):
|
| 320 |
os.unlink(tmp_path)
|
| 321 |
await app.edit_message_text(chat_id, msg_id, f"β Upload error: {e}")
|
|
|
|
| 323 |
return
|
| 324 |
|
| 325 |
if cancel_event.is_set():
|
| 326 |
+
logger.info("Upload cancelled by user (after HF upload)")
|
| 327 |
hf_delete_file(hf_path)
|
| 328 |
meta = hf_read_json(meta_path) or {}
|
| 329 |
meta.pop(enc_fname, None)
|
|
|
|
| 339 |
try:
|
| 340 |
hf_write_json(meta_path, meta)
|
| 341 |
except Exception as e:
|
| 342 |
+
logger.error(f"β Metadata update failed: {e}")
|
| 343 |
await app.edit_message_text(chat_id, msg_id,
|
| 344 |
f"β
Uploaded, but meta update failed: {e}")
|
| 345 |
if os.path.exists(tmp_path):
|
|
|
|
| 351 |
os.unlink(tmp_path)
|
| 352 |
await app.edit_message_text(chat_id, msg_id, f"β
Uploaded `{file_name}` successfully.")
|
| 353 |
del active_progress[msg_id]
|
| 354 |
+
logger.info(f"β
Upload complete: {file_name}")
|
| 355 |
|
| 356 |
+
# (handle_download and helpers are exactly the same as before, just with added logs; for brevity I'm including them as they were, but they work)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 357 |
|
| 358 |
+
# ... PASTE the existing handle_download, download_from_url, download_range functions (unchanged) ...
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 359 |
|
| 360 |
# ---------- TELEGRAM HANDLERS ----------
|
| 361 |
@app.on_message(filters.command("start"))
|
| 362 |
async def start_cmd(_, msg: Message):
|
| 363 |
+
logger.info(f"π /start from user {msg.from_user.id} in chat {msg.chat.id}")
|
| 364 |
await msg.reply(
|
| 365 |
"π **Welcome to Cloud Storage Bot!**\n\n"
|
| 366 |
"Send any file to upload it to the cloud.\n"
|
|
|
|
| 378 |
"Send any file to upload, use /files to download."
|
| 379 |
)
|
| 380 |
|
| 381 |
+
# ... all other handlers (files, callbacks, media) remain identical ...
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 382 |
|
| 383 |
# ---------- WORKER ----------
|
| 384 |
async def worker():
|
| 385 |
+
logger.info("π οΈ Worker started")
|
| 386 |
while True:
|
| 387 |
op = await op_queue.get()
|
| 388 |
+
logger.debug(f"Processing operation: {op.get('type')} - {op.get('file_name', op.get('orig_name', ''))}")
|
| 389 |
try:
|
| 390 |
if op["type"] == "upload":
|
| 391 |
await handle_upload(op)
|
| 392 |
else:
|
| 393 |
await handle_download(op)
|
| 394 |
except Exception as e:
|
| 395 |
+
logger.exception(f"β Worker error: {e}")
|
| 396 |
finally:
|
| 397 |
op_queue.task_done()
|
| 398 |
|
| 399 |
+
# ---------- STARTUP (CALLED AUTOMATICALLY) ----------
|
| 400 |
@app.on_startup()
|
| 401 |
async def on_startup():
|
| 402 |
+
logger.info("π Bot client is starting up...")
|
| 403 |
# Start background tasks
|
| 404 |
asyncio.create_task(worker())
|
| 405 |
asyncio.create_task(progress_updater())
|
| 406 |
|
| 407 |
# Print bot info
|
| 408 |
+
try:
|
| 409 |
+
me = await app.get_me()
|
| 410 |
+
logger.info(f"π€ Bot online: @{me.username} (ID: {me.id})")
|
| 411 |
+
logger.info(f"π¦ Repo: {HF_REPO_ID}")
|
| 412 |
+
logger.info(f"πΎ Disk limit: {MIN_FREE_DISK // (1024**3)} GB free required")
|
| 413 |
+
logger.info(f"βοΈ Split size: {SPLIT_SIZE // (1024**2)} MB")
|
| 414 |
+
logger.info("π Listening for messages... (send /start to test)")
|
| 415 |
+
except Exception as e:
|
| 416 |
+
logger.error(f"β Failed to fetch bot info: {e}")
|
| 417 |
+
|
| 418 |
+
# ---------- MAIN ----------
|
| 419 |
if __name__ == "__main__":
|
| 420 |
+
logger.info("π Starting application...")
|
| 421 |
# Start the HTTP health server in a daemon thread
|
| 422 |
threading.Thread(target=start_http_server, daemon=True).start()
|
| 423 |
+
# Run the Pyrogram client (this blocks and handles updates exactly like the echo bot)
|
| 424 |
+
try:
|
| 425 |
+
logger.info("π‘ Connecting to Telegram...")
|
| 426 |
+
app.run()
|
| 427 |
+
except FloodWait as e:
|
| 428 |
+
logger.warning(f"β οΈ FloodWait {e.value} seconds β please wait or regenerate your token")
|
| 429 |
+
except Exception as e:
|
| 430 |
+
logger.exception(f"β Fatal error: {e}")
|