danicor commited on
Commit
a8b573c
·
verified ·
1 Parent(s): 799e4a9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -14
app.py CHANGED
@@ -3,28 +3,38 @@ import asyncio
3
  from telethon import TelegramClient, events
4
 
5
  # ===============================
6
- # Telegram credentials
7
  # ===============================
8
- API_ID = int(os.environ.get("API_ID"))
9
- API_HASH = os.environ.get("API_HASH")
10
- BOT_TOKEN = os.environ.get("BOT_TOKEN")
11
 
12
  # ===============================
13
- # Persistent storage path
14
  # ===============================
15
  UPLOAD_DIR = "/data/uploads"
16
  os.makedirs(UPLOAD_DIR, exist_ok=True)
17
 
18
  # ===============================
19
- # Initialize bot client
20
  # ===============================
21
- client = TelegramClient("bot", API_ID, API_HASH)
 
 
 
 
 
 
 
22
 
 
 
 
23
  @client.on(events.NewMessage(pattern="/start"))
24
  async def start_handler(event):
25
  await event.reply(
26
- "👋 سلام!\n"
27
- "هر فایلی بفرستی، مستقیم روی HuggingFace Spaces ذخیره می‌کنم."
28
  )
29
 
30
  @client.on(events.NewMessage)
@@ -35,18 +45,22 @@ async def file_handler(event):
35
  try:
36
  file_path = await event.download_media(file=UPLOAD_DIR)
37
  await event.reply(
38
- "✅ فایل با موفقیت ذخیره شد\n\n"
39
  f"📂 مسیر:\n{file_path}"
40
  )
41
- print(f"Saved file: {file_path}")
42
 
43
  except Exception as e:
44
- await event.reply(f"❌ خطا در ذخیره فایل:\n{e}")
45
- print(e)
46
 
 
 
 
47
  async def main():
 
48
  await client.start(bot_token=BOT_TOKEN)
49
- print("🤖 Telegram bot is running...")
50
  await client.run_until_disconnected()
51
 
52
  if __name__ == "__main__":
 
3
  from telethon import TelegramClient, events
4
 
5
  # ===============================
6
+ # Telegram credentials (ENV)
7
  # ===============================
8
+ API_ID = int(os.environ["API_ID"])
9
+ API_HASH = os.environ["API_HASH"]
10
+ BOT_TOKEN = os.environ["BOT_TOKEN"]
11
 
12
  # ===============================
13
+ # Persistent storage
14
  # ===============================
15
  UPLOAD_DIR = "/data/uploads"
16
  os.makedirs(UPLOAD_DIR, exist_ok=True)
17
 
18
  # ===============================
19
+ # Telegram Client (IPv4 forced)
20
  # ===============================
21
+ client = TelegramClient(
22
+ "bot",
23
+ API_ID,
24
+ API_HASH,
25
+ connection_retries=10,
26
+ timeout=30,
27
+ use_ipv6=False
28
+ )
29
 
30
+ # ===============================
31
+ # Handlers
32
+ # ===============================
33
  @client.on(events.NewMessage(pattern="/start"))
34
  async def start_handler(event):
35
  await event.reply(
36
+ "🤖 Bot is online.\n"
37
+ "هر فایلی بفرستی، روی HuggingFace ذخیره می‌کنم."
38
  )
39
 
40
  @client.on(events.NewMessage)
 
45
  try:
46
  file_path = await event.download_media(file=UPLOAD_DIR)
47
  await event.reply(
48
+ "✅ فایل ذخیره شد\n\n"
49
  f"📂 مسیر:\n{file_path}"
50
  )
51
+ print(f"Saved: {file_path}")
52
 
53
  except Exception as e:
54
+ await event.reply(f"❌ خطا:\n{e}")
55
+ print("Download error:", e)
56
 
57
+ # ===============================
58
+ # Main loop
59
+ # ===============================
60
  async def main():
61
+ print("Starting Telegram bot...")
62
  await client.start(bot_token=BOT_TOKEN)
63
+ print("Connected to Telegram")
64
  await client.run_until_disconnected()
65
 
66
  if __name__ == "__main__":