Aayco

Save Content

Mar 9th, 2025
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 8.34 KB | None | 0 0
  1. #Save Content
  2. #pip install telethon (other modules is built-in)
  3. from telethon import TelegramClient, events
  4. import shutil
  5. import time
  6. from datetime import datetime
  7. import os
  8. import re
  9. api_id = 'api id'
  10. api_hash = 'api hash'
  11. phone = 'phone number with +'
  12. #starting your client
  13. client = TelegramClient('love_you_cutie', api_id, api_hash).start(phone=phone)
  14. #i will not explain this function as long it's so clear
  15. @client.on(events.NewMessage(pattern='/test'))
  16. async def ping_handler(event):
  17.     me = await client.get_me()
  18.     if event.sender_id != me.id:
  19.        return
  20.     else:
  21.        await event.reply(f"i'm still working bro...")
  22. #download all media in channel by invite link or channel id or username
  23. @client.on(events.NewMessage(pattern='/all (.+)'))    
  24. async def download_media(event):
  25.     try:
  26.         #get your id from session
  27.         me = await client.get_me()
  28.         #message url
  29.         url = event.pattern_match.group(1).replace('https://t.me/', '') if 'https://t.me/+' not in event.pattern_match.group(1) else (re.search(r'https://t\.me/c/(\d+)/\d+', event.pattern_match.group(1)).group(1) if re.search(r'https://t\.me/c/(\d+)/\d+', event.pattern_match.group(1)) else event.pattern_match.group(1))
  30.         #get channel info
  31.         print(url)
  32.         info = await client.get_entity(url)
  33.         #channel id
  34.         channel_id = info.id
  35.         #download path
  36.         download_path = f"private_content\\{channel_id}"
  37.         #if download path not created
  38.         if not os.path.exists(download_path):
  39.             #create it
  40.             os.makedirs(download_path)
  41.         #the start message
  42.         start_message = await event.reply(f'info about {channel_id}:\nname: {info.title}\nid: {channel_id}\n\ndownload from channel {info.title} started')
  43.         #for every message in channel or group
  44.         async for message in client.iter_messages(channel_id, limit=None):
  45.             #download it if it's media (pic, vid, doc (anything except text messages))
  46.             if message.media:
  47.                 #download media and save it to file path
  48.                 file_path = await client.download_media(message.media, download_path)
  49.                 #editing the message with downloaded file path
  50.                 await start_message.edit(f"file downloaded {file_path}")
  51.                 try:
  52.                     #sending file to telegram
  53.                     await client.send_file(me.id, f"{file_path}", caption=f"file {download_path} downloaded and sent at {datetime.now()}")
  54.                     #delete file after sending
  55.                     os.remove(file_path)
  56.                 except Exception as e:
  57.                     #if error happens send it
  58.                     await event.reply(f"Error: {e}")
  59.             else:
  60.                 #save all channel messages to txt file
  61.                 file = f'{download_path}{channel_id}.txt'
  62.                 with open(file, 'a') as f:
  63.                     f.write(str(message.text)+'\n')
  64.         #send the file
  65.         await client.send_file(me.id, f"{file}", caption=f"file {file} downloaded and sent to {me.first_name} at {datetime.now()}")
  66.         #delete the file
  67.         os.remove(file)
  68.     except Exception as e:
  69.         #if error happens send it
  70.         await event.reply(f"Error: {e}")
  71. #download specific media in channel
  72. @client.on(events.NewMessage(pattern='/down (.+)'))    
  73. async def download_media(event):
  74.     try:
  75.         #get your id from session
  76.         me = await client.get_me()
  77.         #message url
  78.         url = (re.search(r'https://t\.me/([^/]+)/\d+', event.pattern_match.group(1))) if 'https://t.me/c/' not in event.pattern_match.group(1) else (re.search(r'https://t\.me/c/(\d+)/\d+', event.pattern_match.group(1))).group(1)
  79.         #get channel info
  80.         info = await client.get_entity(int(url) if url.isdigit() else str(url.group(1)))
  81.         #channel id
  82.         channel_id = info.id
  83.         #get the message id from link
  84.         match = re.search(r'/(\d+)$', event.pattern_match.group(1))
  85.         #if message id found
  86.         if match:
  87.            #save message id to cute var like you :3
  88.            message_id = match.group(1)
  89.         #download path
  90.         download_path = f"private_content\\{channel_id}"
  91.         #if download path not created
  92.         if not os.path.exists(download_path):
  93.             #create it
  94.             os.makedirs(download_path)
  95.         #the start message
  96.         start_message = await event.reply(f'info about {channel_id}:\nname: {info.title}\nid: {channel_id}\n\ndownload from channel {info.title} started')
  97.         #download specific message in channel or group
  98.         message = await client.get_messages(channel_id, ids=int(message_id))
  99.         #download it if it's media (pic, vid, doc (anything except text messages))
  100.         if message.media:
  101.             #download media and save it to file path
  102.             file_path = await client.download_media(message.media, download_path)
  103.             #editing the message with downloaded file path
  104.             await start_message.edit(f"file downloaded {file_path}")
  105.             try:
  106.                 #sending file to telegram
  107.                 await client.send_file(me.id, file=file_path, caption=f"file {download_path} downloaded and sent at {datetime.now()}")
  108.                 #delete file after sending
  109.                 os.remove(file_path)
  110.             except Exception as e:
  111.                 #if error happens send it
  112.                 await event.reply(f"Error: {e}")
  113.         else:
  114.             #save all channel messages to txt file
  115.             file = f'{download_path}{channel_id}.txt'
  116.             with open(file, 'a') as f:
  117.                 f.write(str(message.text)+'\n')
  118.         if not message.media:
  119.            #send the file
  120.            await client.send_file(me.id, f"{file}", caption=f"file {file} downloaded and sent to {me.first_name} at {datetime.now()}")
  121.            #delete the file
  122.            os.remove(file)
  123.     except Exception as e:
  124.         #if error happens send it
  125.         await event.reply(f"Error: {e}")
  126. #function to download view once media        
  127. @client.on(events.NewMessage(pattern='/get'))    
  128. async def download_media(event):
  129.     try:
  130.         #get your id from session
  131.         me = await client.get_me()
  132.         #get replied message
  133.         replied_message = await event.get_reply_message()
  134.         #get the sender info from replied message object
  135.         sender = await replied_message.get_sender()
  136.         #get the sender id or username from sender object
  137.         sender_info = sender.id if sender.username==None else sender.id
  138.         #media download path
  139.         download_path = f"private_content\\{sender_info}"
  140.         #check if replied message is media
  141.         if replied_message.media:
  142.            #get the expire date of the file
  143.            ttl_seconds = replied_message.media.ttl_seconds if replied_message.media else None
  144.            #ignore it it's always None anyway :3
  145.            ttl_period = replied_message.ttl_period if hasattr(replied_message, 'ttl_period') else None
  146.            #download the message media
  147.            file_path = await replied_message.download_media(file=download_path)
  148.            #if ttl_seconds is lower than 31 then it have expire date
  149.            if int(ttl_seconds) < 31 and ttl_period==None:
  150.                #send downloaded file to saved messages
  151.                await client.send_file(me.id, f"{file_path}", caption=f"file {file_path} from {sender_info} downloaded and sent at {datetime.now()} btw the media will expire after {ttl_seconds} from open time")
  152.             #if ttl_seconds is higher than 31 then it's view once
  153.            elif int(ttl_seconds) > 31 and ttl_period==None:
  154.                #send downloaded file to saved messages
  155.                await client.send_file(me.id, f"{file_path}", caption=f"file {file_path} from {sender_info} downloaded and sent at {datetime.now()}")
  156.            else:
  157.                #it's not view once media :(
  158.                await event.reply('reply to view once media with /get command to download it')
  159.         else:
  160.             #the message is text not media
  161.             await event.reply('the message should be media')
  162.     except Exception as e:
  163.         #if error happens send it
  164.         await event.reply(f"Error: {e}")            
  165. with client:
  166.     #run the code without stopping
  167.     print('bot is running... hit https://github.com/iAayco/Save-Content-Userbot/ for any help or ask')
  168.     client.run_until_disconnected()
Advertisement
Add Comment
Please, Sign In to add comment