-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathSaveit.py
More file actions
85 lines (68 loc) · 2.36 KB
/
Saveit.py
File metadata and controls
85 lines (68 loc) · 2.36 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
from telethon import TelegramClient, events
import asyncio
import os
import time
from datetime import datetime as dt
from dotenv import load_dotenv
# Load environment variables from .env file
load_dotenv()
# API info to get from my.telegram.org
api_id = os.getenv('API_ID')
api_hash = os.getenv('API_HASH')
handler = os.getenv('HANDLER')
client = TelegramClient('save', api_id, api_hash)
your_user_id = None
@client.on(events.NewMessage(pattern=rf'\{handler}'))
async def download(event):
global your_user_id
if your_user_id is None:
me = await client.get_me()
your_user_id = me.id
if event.sender_id != your_user_id:
return
pvpv = event.sender_id
saved_messages_chat_id = "me"
inpv = await event.client.send_message(pvpv, "Downloading...")
if event.reply_to_msg_id:
ok = await event.get_reply_message()
sssender = ok.sender_id
else:
return await event.reply("Reply to a message with media to save it.", time=8)
await event.delete()
if not (ok and ok.media):
await inpv.edit("No media found in the replied message.")
return
download_path = "downloads/"
if not os.path.exists(download_path):
os.makedirs(download_path)
# دانلود فایل
try:
if hasattr(ok.media, "document") or hasattr(ok.media, "photo"):
# save as original file
file_path = await event.client.download_media(ok, file=download_path)
else:
await inpv.edit("Unsupported media type.")
return
except Exception as err:
await inpv.edit(f"Failed to download file: {str(err)}")
return
try:
await event.client.send_file(
saved_messages_chat_id,
file_path,
caption=f"File saved from {sssender}",
force_document=True
)
except Exception as err:
await inpv.edit(f"Failed to send file to Saved Messages: {str(err)}")
return
await inpv.delete()
async def main():
async with client:
global your_user_id
me = await client.get_me()
your_user_id = me.id
print(f"Bot is running as user: {me.username} (ID: {your_user_id})")
await client.run_until_disconnected()
if __name__ == "__main__":
asyncio.run(main())