danitell2 / app.py
danicor's picture
Update app.py
a8b573c verified
import os
import asyncio
from telethon import TelegramClient, events
# ===============================
# Telegram credentials (ENV)
# ===============================
API_ID = int(os.environ["API_ID"])
API_HASH = os.environ["API_HASH"]
BOT_TOKEN = os.environ["BOT_TOKEN"]
# ===============================
# Persistent storage
# ===============================
UPLOAD_DIR = "/data/uploads"
os.makedirs(UPLOAD_DIR, exist_ok=True)
# ===============================
# Telegram Client (IPv4 forced)
# ===============================
client = TelegramClient(
"bot",
API_ID,
API_HASH,
connection_retries=10,
timeout=30,
use_ipv6=False
)
# ===============================
# Handlers
# ===============================
@client.on(events.NewMessage(pattern="/start"))
async def start_handler(event):
await event.reply(
"🤖 Bot is online.\n"
"هر فایلی بفرستی، روی HuggingFace ذخیره می‌کنم."
)
@client.on(events.NewMessage)
async def file_handler(event):
if not event.file:
return
try:
file_path = await event.download_media(file=UPLOAD_DIR)
await event.reply(
"✅ فایل ذخیره شد\n\n"
f"📂 مسیر:\n{file_path}"
)
print(f"Saved: {file_path}")
except Exception as e:
await event.reply(f"❌ خطا:\n{e}")
print("Download error:", e)
# ===============================
# Main loop
# ===============================
async def main():
print("Starting Telegram bot...")
await client.start(bot_token=BOT_TOKEN)
print("Connected to Telegram")
await client.run_until_disconnected()
if __name__ == "__main__":
asyncio.run(main())