File size: 1,723 Bytes
3fe345b
b407c05
 
 
 
a8b573c
b407c05
a8b573c
 
 
b407c05
 
a8b573c
b407c05
 
481beab
3fe345b
b407c05
a8b573c
b407c05
a8b573c
 
 
 
 
 
 
 
b407c05
a8b573c
 
 
b407c05
 
 
a8b573c
 
b407c05
 
 
 
 
 
 
 
 
 
a8b573c
b407c05
 
a8b573c
b407c05
 
a8b573c
 
b407c05
a8b573c
 
 
b407c05
a8b573c
b407c05
a8b573c
b407c05
 
 
 
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
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())