LonamiWebs / Telethon

Pure Python 3 MTProto API Telegram client library, for bots too!
https://docs.telethon.dev
MIT License
9.9k stars 1.4k forks source link

telethon.errors.common.TypeNotFoundError: Could not find a matching Constructor ID for the TLObject that was supposed to be read with ID a6d57763. See the FAQ for more details. #4281

Closed assc0d3r closed 10 months ago

assc0d3r commented 10 months ago

Code that causes the issue

import asyncio import logging from tqdm import tqdm from telethon import TelegramClient, events, errors from telethon.tl.types import PeerChannel, DocumentAttributeFilename, DocumentAttributeVideo, MessageMediaPhoto, PhotoSizeProgressive from decouple import config from telethon.sessions import StringSession

logging.basicConfig( level=logging.INFO, format="[%(asctime)s - %(levelname)s] - %(name)s - %(message)s", datefmt="%d-%b-%y %H:%M:%S", filename='logfile.log', ) logging.getLogger("pyrogram").setLevel(logging.WARNING)

logging.basicConfig(format='[%(levelname) 5s/%(asctime)s] %(name)s: %(message)s',

level=logging.DEBUG)

logging.basicConfig(format='%(asctime)s - %(levelname)s - %(message)s', level=logging.INFO, filename='logfile.log')

logger = logging. getLogger(name) queue = asyncio. Queue()

APP_ID = config("APP_ID", default=None, cast=int) API_HASH = config("API_HASH", default=None) SESSION = config("SESSION") chat_list = config("chat_list")

client = TelegramClient(StringSession(SESSION), APP_ID, API_HASH) SESSION = client.session.save() chat_list = ['https://t.me/+---------------------'] #input the channel link

calculate file size

def convert_size(text): units = ["B", "KB", "MB", "GB", "TB", "PB"] size = 1024 for i in range(len(units)): if (text/size) < 1: return "%.2f%s" % (text, units[i]) text = text/size return 0

Get file information

def get_file_information(message): file = None if message.media is not None: try: if type(message.media) is MessageMediaPhoto: photo = message.media.photo file = { 'id': photo.id, 'access_hash': photo.access_hash, 'type': 'photo', 'datetime': photo.date.astimezone().strftime("%Y/%m/%d %H:%M:%S") } for i in photo. sizes: if type(i) is PhotoSizeProgressive: # file name file["size"] = i.sizes[len(i.sizes)-1] # movie name file["w"] = i.w # movie width file["h"] = i.h # movie height else: document = message.media.document file = { 'id': document.id, 'access_hash': document. access_hash, 'type': document.mime_type, # file type 'size': document.size, # file size 'datetime': document.date.astimezone().strftime("%Y/%m/%d %H:%M:%S") } for i in document.attributes: if type(i) is DocumentAttributeFilename: # file name file["name"] = i.file_name # movie name if type(i) is DocumentAttributeVideo: # Video resolution file["w"] = i.w # movie width file["h"] = i.h # movie height except: print("An error occurred") print(message) return None

 return file

Check if the same file id exists

def check_duplicate_file(message, entity): file = get_file_information(message) if file is None: return False, file if file['id'] in file_list[entity.id]: return True, file file_list[entity.id].append(file['id'])

 return False, file

file_list = {} # record file id

@events.register(events.NewMessage(chats=tuple(chat_list))) async def handler(update):

get group new information

 chat_id = update.message.to_id
 try:
     entity = await client.get_entity(chat_id)
 except ValueError:
     entity = await client.get_entity(PeerChannel(chat_id))
 except Exception as e:
     logger. error(type(e.__class__, e))
     return

 text = ""
 print("Group:{}, new message".format(entity.title))
 is_duplicate, file = check_duplicate_file(update.message, entity)
 if is_duplicate:
     text += "time:{}".format(file['datetime'])
     if 'type' in file: text += ", file type: {}".format(file['type'])
     if 'name' in file:text += ", file name:{}".format(file['name'])
     text += ", file size:{}".format(convert_size(file['size']))
     if 'w' in file and 'h' in file:
         text += ", resolution:{}x{}".format(file['w'],file['h'])
     print(text)
     await client.delete_messages(entity=entity, message_ids=[update.message.id]) # delete message

async def init(): bar = tqdm(chat_list) for i in bar: entity = await client.get_entity(i) file_list[entity.id] = [] # Initialize each group file list total = 0 # count the number of messages processed delete = 0 # Count the number of deleted messages

     # Read group messages (from old to new)
     async for message in client.iter_messages(entity, reverse = True):
         is_duplicate, _ = check_duplicate_file(message, entity)
         if is_duplicate:
             print('Group:{}, delete duplicate files[{}]'.format(entity.title,message.id))
             await client.delete_messages(entity=entity, message_ids=[message.id]) # delete message
             delete += 1
         total += 1
         bar.set_description('Group: {} Initialize check for duplicate files, check quantity: {}, delete: {}'.format(entity.title, total, delete))

 return False

client = TelegramClient('bot', api_id, api_hash)

with client: print("Initialize check for duplicate files") client.loop.run_until_complete(init())

 #print("Start listening for new messages:")
 client.add_event_handler(handler)
 client.run_until_disconnected()

Expected behavior

Group: movies collection Initialize check for duplicate files, check quantity: 64, delete: 0: 100%|█| 1/1 [00:01<00 Start listening for new messages:

this code works like above when its used on PC but while hosting on render it throws the error.

Actual behavior

telethon.errors.common.TypeNotFoundError: Could not find a matching Constructor ID for the TLObject that was supposed to be read with ID a6d57763. See the FAQ for more details. Remaining bytes: b'cw\xd5\xa6\x00\x00\x00\x00g\xba\x01\x00\xb7%\x87e\xfe\xf1\x01\x00The.Crown.SEASON.02.S02.COMPLETE.1080p.10bit.WEBRip.6CH.x265.HEVC-PSA\n\xe2\x94\x83 [\xe2\x96\xa0\xe2\x96\xa0\xe2\x96\xa0\xe2\x96\xa0\xe2\x96\xa5\xe2\x96\xa1\xe2\x96\xa1\xe2\x96\xa1\xe2\x96\xa1\xe2\x96\xa1\xe2\x96\xa1\xe2\x96\xa1\xe2\x96\xa1] 34.25%\n\xe2\x94\xa0 Processed: 2.14 GiB of 6.24 GiB\n\xe2\x94\xa0 Status: Download | ETA: 46m24s\n\xe2\x94\xa0 Speed: 1.51 MiB/s | Elapsed: 2h49m4s\n\xe2\x94\xa0 Engine: Aria2 v1.36.0\n\xe2\x94\xa0 Mode: #Leech | #Aria2\n\xe2\x94\xa0 Seeders: 2 | Leechers: 10\n\xe2\x94\xa0 User: S A E E D | ID: 624947393\n\xe2\x94\x96 /cancel1_24e6ec0ceb03c967\n\n\xe2\x8c\xac Bot Stats\n\xe2\x94\xa0 CPU: 14.7% | F: 243.06GB [69.2%]\n\xe2\x94\xa0 RAM: 51.8% | UPTIME: 3h23m34s\n\xe2\x94\x96 DL: 1.51MB/s | UL: 0B/s\x00\x00\x00T\x02\xa3H\x15\xc4\xb5\x1c\x01\x00\x00\x00\x83\x8bw\x15\xc4\xb5\x1c\x01\x00\x00\x00k\xdb\xbb5\x00\x00\x00\x00\x10\xe1\xb4\x98\xe1\xb4\x80\xc9\xa2\xe1\xb4\x87s\n1/1\x00\x00\x00\nstatus ref\x00\x15\xc4\xb5\x1c\x1a\x00\x00\x00\xc9\x0ba\xbd\x00\x00\x00\x00E\x00\x00\x00\x8bo\x82\x00\x00\x00\x00E\x00\x00\x00\xc9\x0ba\xbda\x00\x00\x00\n\x00\x00\x00\xc9\x0ba\xbd\x83\x00\x00\x00\x07\x00\x00\x00\xc9\x0ba\xbd\x96\x00\x00\x00\x04\x00\x00\x00\xc9\x0ba\xbd\xa4\x00\x00\x00\x06\x00\x00\x00\xc9\x0ba\xbd\xb8\x00\x00\x00\x08\x00\x00\x00\xc9\x0ba\xbd\xcb\x00\x00\x00\x07\x00\x00\x00\xc9\x0ba\xbd\xe3\x00\x00\x00\x05\x00\x00\x00\r[co\xea\x00\x00\x00\x06\x00\x00\x00\r[co\xf3\x00\x00\x00\x06\x00\x00\x00\xc9\x0ba\xbd\xfc\x00\x00\x00\x08\x00\x00\x00\xc9\x0ba\xbd\t\x01\x00\x00\t\x00\x00\x00\xc9\x0ba\xbd\x18\x01\x00\x00\x05\x00\x00\x00q\x05\xa2(\x1e\x01\x00\x00\t\x00\x00\x00\xc9\x0ba\xbd*\x01\x00\x00\x03\x00\x00\x00q\x05\xa2(.\x01\x00\x00\t\x00\x00\x00\xc7\x8a\xefl:\x01\x00\x00\x19\x00\x00\x00\xc9\x0ba\xbdW\x01\x00\x00\t\x00\x00\x00`\x8bo\x82W\x01\x00\x00\t\x00\x00\x00\xc9\x0ba\xbdc\x01\x00\x00\x04\x00\x00\x00\xc9\x0ba\xbdp\x01\x00\x00\x02\x00\x00\x00\xc9\x0ba\xbd\x86\x01\x00\x00\x04\x00\x00\x00\xc9\x0ba\xbd\x93\x01\x00\x00\x07\x00\x00\x00\xc9\x0ba\xbd\xa6\x01\x00\x00\x03\x00\x00\x00\xc9\x0ba\xbd\xb5\x01\x00\x00\x03\x00\x00\x00T>\x87e\x80:\t\x00k\x02\x15\x00\x00\x00\x00\x00\x15\xc4\xb5\x1c\x01\x00\x00\x00a\xaca\x82@!\x04\x00\xf2.\x8bn\x00\x00\x00\x00[ \n\x06n\x88=\xb1&Unknown Mirror Leech Group 4GB Premium\x00\x0fUnknown_X_Leech\x11\x1cn\x1c\x02\x00\x00\x00:\xba1\x1bj\xd0\xf8T\x0b\x01\x08\x08\xccV\x18\xf7\xa2\x8a(\x03\x05\x00\x00\x00\xa3\x9f?e\x18\x04\x12\x9f\x00\x04\x02\x00\xff\xff\xff\x7f\x15\xc4\xb5\x1c\x01\x00\x00\x00\xb0\xec\xf6?+@\x00\x02\x07\r\xd4\x96\x01\x00\x00\x00\xfeY\xf8\xde$\x00\xc83\x11Unknown_X_Leech I\x00\x00\x15Unknown_X_LeechIRobot\x00\x00\x06\xf7\xd1\x82\x02\x00\x00\x00J\xbe1\x1b\x86\xe3\x99S\x0e\x01\x08\x08\xcb\xf9<\x9f\xf6\xb3E\x14U6\x07\x00\x04\x00\x00\x00\x02\x00\x00\x00' raise error File "/usr/local/lib/python3.11/site-packages/telethon/extensions/binaryreader.py", line 140, in tgread_object ^^^^^^^^^^^^^^^^^^^^^^ _reply_to = reader.tgread_object() File "/usr/local/lib/python3.11/site-packages/telethon/tl/types/init.py", line 17303, in from_reader ^^^^^^^^^^^^^^^^^^^^^^^ return clazz.from_reader(self) File "/usr/local/lib/python3.11/site-packages/telethon/extensions/binaryreader.py", line 142, in tgread_object ^^^^^^^^^^^^^^^^^^^^^^ _message = reader.tgread_object() File "/usr/local/lib/python3.11/site-packages/telethon/tl/types/init.py", line 32477, in from_reader ^^^^^^^^^^^^^^^^^^^^^^^ return clazz.from_reader(self) File "/usr/local/lib/python3.11/site-packages/telethon/extensions/binaryreader.py", line 142, in tgread_object ^^^^^^^^^^^^^^^^^^^^^^ _x = reader.tgread_object() File "/usr/local/lib/python3.11/site-packages/telethon/tl/types/updates.py", line 72, in from_reader ^^^^^^^^^^^^^^^^^^^^^^^ return clazz.from_reader(self) File "/usr/local/lib/python3.11/site-packages/telethon/extensions/binaryreader.py", line 142, in tgread_object ^^^^^^^^^^^^^^^^^^^^^^ return reader.tgread_object() File "/usr/local/lib/python3.11/site-packages/telethon/tl/tlobject.py", line 219, in read_result ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ result = state.request.read_result(reader) File "/usr/local/lib/python3.11/site-packages/telethon/network/mtprotosender.py", line 641, in _handle_rpc_result ^^^^^^^^^^^^ result = await future File "/usr/local/lib/python3.11/site-packages/telethon/client/users.py", line 87, in _call ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ return await self._call(self._sender, request, ordered=ordered) File "/usr/local/lib/python3.11/site-packages/telethon/client/users.py", line 30, in call ^^^^^^^^^^^^^^^^^^^^ diff = await self(get_diff) File "/usr/local/lib/python3.11/site-packages/telethon/client/updates.py", line 353, in _update_loop raise self._updates_error File "/usr/local/lib/python3.11/site-packages/telethon/client/updates.py", line 38, in _run_until_disconnected ^^^^^^^^^^^^^^^ return future.result() File "/usr/local/lib/python3.11/asyncio/base_events.py", line 653, in run_until_complete ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ return self.loop.run_until_complete(self._run_until_disconnected()) File "/usr/local/lib/python3.11/site-packages/telethon/client/updates.py", line 96, in run_until_disconnected client.run_until_disconnected() File "/app/main.py", line 148, in Traceback (most recent call last): Group:Movies collection, delete duplicate files[2925910] Group:Movies collection, delete duplicate files[2925909] Group:Movies collection, delete duplicate files[2925892] Group:Movies collection, delete duplicate files[2925891]

Initialize check for duplicate files

note this log should be read in reverse order i.e from bottom to top.

Traceback

telethon.errors.common.TypeNotFoundError: Could not find a matching Constructor ID for the TLObject that was supposed to be read with ID a6d57763. See the FAQ for more details. Remaining bytes: b'cw\xd5\xa6\x00\x00\x00\x00g\xba\x01\x00\xb7%\x87e\xfe\xf1\x01\x00The.Crown.SEASON.02.S02.COMPLETE.1080p.10bit.WEBRip.6CH.x265.HEVC-PSA\n\xe2\x94\x83 [\xe2\x96\xa0\xe2\x96\xa0\xe2\x96\xa0\xe2\x96\xa0\xe2\x96\xa5\xe2\x96\xa1\xe2\x96\xa1\xe2\x96\xa1\xe2\x96\xa1\xe2\x96\xa1\xe2\x96\xa1\xe2\x96\xa1\xe2\x96\xa1] 34.25%\n\xe2\x94\xa0 Processed: 2.14 GiB of 6.24 GiB\n\xe2\x94\xa0 Status: Download | ETA: 46m24s\n\xe2\x94\xa0 Speed: 1.51 MiB/s | Elapsed: 2h49m4s\n\xe2\x94\xa0 Engine: Aria2 v1.36.0\n\xe2\x94\xa0 Mode: #Leech | #Aria2\n\xe2\x94\xa0 Seeders: 2 | Leechers: 10\n\xe2\x94\xa0 User: S A E E D | ID: 624947393\n\xe2\x94\x96 /cancel1_24e6ec0ceb03c967\n\n\xe2\x8c\xac Bot Stats\n\xe2\x94\xa0 CPU: 14.7% | F: 243.06GB [69.2%]\n\xe2\x94\xa0 RAM: 51.8% | UPTIME: 3h23m34s\n\xe2\x94\x96 DL: 1.51MB/s | UL: 0B/s\x00\x00\x00T\x02\xa3H\x15\xc4\xb5\x1c\x01\x00\x00\x00\x83\x8bw\x15\xc4\xb5\x1c\x01\x00\x00\x00k\xdb\xbb5\x00\x00\x00\x00\x10\xe1\xb4\x98\xe1\xb4\x80\xc9\xa2\xe1\xb4\x87s\n1/1\x00\x00\x00\nstatus ref\x00\x15\xc4\xb5\x1c\x1a\x00\x00\x00\xc9\x0ba\xbd\x00\x00\x00\x00E\x00\x00\x00\x8bo\x82\x00\x00\x00\x00E\x00\x00\x00\xc9\x0ba\xbda\x00\x00\x00\n\x00\x00\x00\xc9\x0ba\xbd\x83\x00\x00\x00\x07\x00\x00\x00\xc9\x0ba\xbd\x96\x00\x00\x00\x04\x00\x00\x00\xc9\x0ba\xbd\xa4\x00\x00\x00\x06\x00\x00\x00\xc9\x0ba\xbd\xb8\x00\x00\x00\x08\x00\x00\x00\xc9\x0ba\xbd\xcb\x00\x00\x00\x07\x00\x00\x00\xc9\x0ba\xbd\xe3\x00\x00\x00\x05\x00\x00\x00\r[co\xea\x00\x00\x00\x06\x00\x00\x00\r[co\xf3\x00\x00\x00\x06\x00\x00\x00\xc9\x0ba\xbd\xfc\x00\x00\x00\x08\x00\x00\x00\xc9\x0ba\xbd\t\x01\x00\x00\t\x00\x00\x00\xc9\x0ba\xbd\x18\x01\x00\x00\x05\x00\x00\x00q\x05\xa2(\x1e\x01\x00\x00\t\x00\x00\x00\xc9\x0ba\xbd*\x01\x00\x00\x03\x00\x00\x00q\x05\xa2(.\x01\x00\x00\t\x00\x00\x00\xc7\x8a\xefl:\x01\x00\x00\x19\x00\x00\x00\xc9\x0ba\xbdW\x01\x00\x00\t\x00\x00\x00`\x8bo\x82W\x01\x00\x00\t\x00\x00\x00\xc9\x0ba\xbdc\x01\x00\x00\x04\x00\x00\x00\xc9\x0ba\xbdp\x01\x00\x00\x02\x00\x00\x00\xc9\x0ba\xbd\x86\x01\x00\x00\x04\x00\x00\x00\xc9\x0ba\xbd\x93\x01\x00\x00\x07\x00\x00\x00\xc9\x0ba\xbd\xa6\x01\x00\x00\x03\x00\x00\x00\xc9\x0ba\xbd\xb5\x01\x00\x00\x03\x00\x00\x00T>\x87e\x80:\t\x00k\x02\x15\x00\x00\x00\x00\x00\x15\xc4\xb5\x1c\x01\x00\x00\x00a\xaca\x82@!\x04\x00\xf2.\x8bn\x00\x00\x00\x00[ \n\x06n\x88=\xb1&Unknown Mirror Leech Group 4GB Premium\x00\x0fUnknown_X_Leech\x11\x1cn\x1c\x02\x00\x00\x00:\xba1\x1bj\xd0\xf8T\x0b\x01\x08\x08\xccV\x18\xf7\xa2\x8a(\x03\x05\x00\x00\x00\xa3\x9f?e\x18\x04\x12\x9f\x00\x04\x02\x00\xff\xff\xff\x7f\x15\xc4\xb5\x1c\x01\x00\x00\x00\xb0\xec\xf6?+@\x00\x02\x07\r\xd4\x96\x01\x00\x00\x00\xfeY\xf8\xde$\x00\xc83\x11Unknown_X_Leech I\x00\x00\x15Unknown_X_LeechIRobot\x00\x00\x06\xf7\xd1\x82\x02\x00\x00\x00J\xbe1\x1b\x86\xe3\x99S\x0e\x01\x08\x08\xcb\xf9<\x9f\xf6\xb3E\x14U6\x07\x00\x04\x00\x00\x00\x02\x00\x00\x00' raise error File "/usr/local/lib/python3.11/site-packages/telethon/extensions/binaryreader.py", line 140, in tgread_object ^^^^^^^^^^^^^^^^^^^^^^ _reply_to = reader.tgread_object() File "/usr/local/lib/python3.11/site-packages/telethon/tl/types/init.py", line 17303, in from_reader ^^^^^^^^^^^^^^^^^^^^^^^ return clazz.from_reader(self) File "/usr/local/lib/python3.11/site-packages/telethon/extensions/binaryreader.py", line 142, in tgread_object ^^^^^^^^^^^^^^^^^^^^^^ _message = reader.tgread_object() File "/usr/local/lib/python3.11/site-packages/telethon/tl/types/init.py", line 32477, in from_reader ^^^^^^^^^^^^^^^^^^^^^^^ return clazz.from_reader(self) File "/usr/local/lib/python3.11/site-packages/telethon/extensions/binaryreader.py", line 142, in tgread_object ^^^^^^^^^^^^^^^^^^^^^^ _x = reader.tgread_object() File "/usr/local/lib/python3.11/site-packages/telethon/tl/types/updates.py", line 72, in from_reader ^^^^^^^^^^^^^^^^^^^^^^^ return clazz.from_reader(self) File "/usr/local/lib/python3.11/site-packages/telethon/extensions/binaryreader.py", line 142, in tgread_object ^^^^^^^^^^^^^^^^^^^^^^ return reader.tgread_object() File "/usr/local/lib/python3.11/site-packages/telethon/tl/tlobject.py", line 219, in read_result ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ result = state.request.read_result(reader) File "/usr/local/lib/python3.11/site-packages/telethon/network/mtprotosender.py", line 641, in _handle_rpc_result ^^^^^^^^^^^^ result = await future File "/usr/local/lib/python3.11/site-packages/telethon/client/users.py", line 87, in _call ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ return await self._call(self._sender, request, ordered=ordered) File "/usr/local/lib/python3.11/site-packages/telethon/client/users.py", line 30, in call ^^^^^^^^^^^^^^^^^^^^ diff = await self(get_diff) File "/usr/local/lib/python3.11/site-packages/telethon/client/updates.py", line 353, in _update_loop raise self._updates_error File "/usr/local/lib/python3.11/site-packages/telethon/client/updates.py", line 38, in _run_until_disconnected ^^^^^^^^^^^^^^^ return future.result() File "/usr/local/lib/python3.11/asyncio/base_events.py", line 653, in run_until_complete ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ return self.loop.run_until_complete(self._run_until_disconnected()) File "/usr/local/lib/python3.11/site-packages/telethon/client/updates.py", line 96, in run_until_disconnected client.run_until_disconnected() File "/app/main.py", line 148, in Traceback (most recent call last): Group:Movies collection, delete duplicate files[2925910] Group:Movies collection, delete duplicate files[2925909] Group:Movies collection, delete duplicate files[2925892] Group:Movies collection, delete duplicate files[2925891]

Initialize check for duplicate files

note this log should be read in reverse order i.e from bottom to top.

Telethon version

1.33.1

Python version

3.11.1

Operating system (including distribution name and version)

using on a docker image

Other details

DEPLOYED ON RENDER SERVER.

THE CODE WORKS FLAWLESSELY WHEN DEPLOYED ON HOME PC RUNNING WINDOWS 11 AND LATEST TELETHON AND PYTHON VERSION.

Checklist

Lonami commented 10 months ago

Either the same problem as #4273 or you're reusing the session. Not something that will be fixed until the next version. If it really causes you trouble, you can temporarily downgrade.

Lilneo786 commented 10 months ago
!pip install python-decouple

import asyncio
import logging
from tqdm import tqdm
from telethon import TelegramClient, events, errors
from telethon.tl.types import PeerChannel, DocumentAttributeFilename, DocumentAttributeVideo, MessageMediaPhoto, PhotoSizeProgressive
from decouple import config
from telethon.sessions import StringSession

logging.basicConfig(
level=logging.INFO,
format="%(asctime)s - %(levelname)s - %(name)s - %(message)s",
datefmt="%d-%b-%y %H:%M:%S",
filename='logfile.log',
)
logging.getLogger("pyrogram").setLevel(logging.WARNING)

name = "Name to Log"
logger = logging.getLogger(name)
queue = asyncio.Queue()

APP_ID = config("APP_ID", default=123456, cast=int)
API_HASH = config("API_HASH", default="your-api-hash")
SESSION = config("SESSION", default="your-session-string")
chat_list = config("chat_list", default="your-chat-list").split(',')

client = TelegramClient(StringSession(SESSION), APP_ID, API_HASH)
SESSION = client.session.save()

def convert_size(text):
    units = ["B", "KB", "MB", "GB", "TB", "PB"]
    size = 1024
    for i in range(len(units)):
        if (text/size) < 1:
            return "%.2f%s" % (text, units[i])
        text = text/size
    return 0

def get_file_information(message):
    file = None
    if message.media is not None:
        try:
            if type(message.media) is MessageMediaPhoto:
                photo = message.media.photo
                file = {
                    'id': photo.id,
                    'access_hash': photo.access_hash,
                    'type': 'photo',
                    'datetime': photo.date.astimezone().strftime("%Y/%m/%d %H:%M:%S")
                    }
                for i in photo.sizes:
                    if type(i) is PhotoSizeProgressive:
                        file["size"] = i.sizes[len(i.sizes)-1]
                        file["w"] = i.w 
                        file["h"] = i.h 
            else:
                document = message.media.document
                file = {
                    'id': document.id,
                    'access_hash': document.access_hash,
                    'type': document.mime_type,
                    'size': document.size,
                    'datetime': document.date.astimezone().strftime("%Y/%m/%d %H:%M:%S")
                    }
                for i in document.attributes:
                    if type(i) is DocumentAttributeFilename:
                        file["name"] = i.file_name 
                    if type(i) is DocumentAttributeVideo:
                        file["w"] = i.w 
                        file["h"] = i.h 
        except:
            print("An error occurred")
            print(message)
            return None

        return file

def check_duplicate_file(message, entity):
    file = get_file_information(message)
    if file is None: 
        return False, file
    if file['id'] in file_list[entity.id]:
        return True, file
    file_list[entity.id].append(file['id']) 

    return False, file

file_list = {} 

@events.register(events.NewMessage(chats=chat_list))
async def handler(update):
    chat_id = update.message.to_id
    try:
        entity = await client.get_entity(chat_id)
    except ValueError:
        entity = await client.get_entity(PeerChannel(chat_id))
    except Exception as e:
        logger.error(str(type(e)) + str(e))
        return

    text = ""
    print("Group:{}, new message".format(entity.title))
    is_duplicate, file = check_duplicate_file(update.message, entity)
    if is_duplicate:
        text += "time:{}".format(file['datetime'])
        if 'type' in file: 
            text += ", file type: {}".format(file['type'])
        if 'name' in file:
            text += ", file name:{}".format(file['name'])
        text += ", file size:{}".format(convert_size(file['size']))
        if 'w' in file and 'h' in file:
            text += ", resolution:{}x{}".format(file['w'],file['h'])
        print(text)
        await client.delete_messages(entity=entity, message_ids=[update.message.id])

async def init():
    bar = tqdm(chat_list)
    for i in bar:
        entity = await client.get_entity(i)
        file_list[entity.id] = [] 
        total = 0
        delete = 0

        async for message in client.iter_messages(entity, reverse = True):
            is_duplicate, _ = check_duplicate_file(message, entity)
            if is_duplicate:
                print('Group:{}, delete duplicate files[{}]'.format(entity.title,message.id))
                await client.delete_messages(entity=entity, message_ids=[message.id])
                delete += 1
            total += 1
            bar.set_description('Group: {} Initialize check for duplicate files, check quantity: {}, delete:{}'.format(entity.title, total, delete))   

    return False

with client:
    print("Initialize check for duplicate files")
    client.loop.run_until_complete(init())
    client.add_event_handler(handler)
    client.run_until_disconnected()

If you get a string error use this:

from telethon.sync import TelegramClient

API_ID = your_api_id  # Replace with your actual API ID
API_HASH = 'your_api_hash'  # Replace with your actual API Hash

with TelegramClient('anon', API_ID, API_HASH) as client:
    print(client.session.save())
assc0d3r commented 10 months ago

@Lilneo786 thanks for the suggestion will try that. The actual code was client = TelegramClient('bot', api_id, api_hash) with client: print("Initialize check for duplicate files") client.loop.run_until_complete(init())

 print("Start listening for new messages:")
 client.add_event_handler(handler)
 client.run_until_disconnected()

how ever i modified it to use it with render after creating a session string online. Because deploying on render it does not allow me to input API Hash and phone number to generate the anon session file. will your code above will allow me to generate the session without any human intervention.

assc0d3r commented 10 months ago

@Lonami thanks for prmopt reply, the code was already running on downgraded version but it was throwing up same error so i tried upgrading it however results are still same.

assc0d3r commented 10 months ago

with TelegramClient('anon', API_ID, API_HASH) as client: print(client.session.save())

i am getting the following error with ur above code

Traceback (most recent call last): File "/app/main.py", line 31, in with TelegramClient('anon', APP_ID, API_HASH) as client: File "/usr/local/lib/python3.11/site-packages/telethon/helpers.py", line 219, in _sync_enter return loop.run_until_complete(self.aenter()) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.11/asyncio/base_events.py", line 653, in run_until_complete return future.result() ^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.11/site-packages/telethon/client/auth.py", line 657, in aenter return await self.start() ^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.11/site-packages/telethon/client/auth.py", line 165, in _start value = phone() ^^^^^^^ File "/usr/local/lib/python3.11/site-packages/telethon/client/auth.py", line 22, in phone: typing.Callable[[], str] = lambda: input('Please enter your phone (or bot token): '), ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ EOFError: EOF when reading a line Traceback (most recent call last): File "/app/main.py", line 31, in with TelegramClient('anon', APP_ID, API_HASH) as client: File "/usr/local/lib/python3.11/site-packages/telethon/helpers.py", line 219, in _sync_enter return loop.run_until_complete(self.aenter()) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.11/asyncio/base_events.py", line 653, in run_until_complete return future.result() ^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.11/site-packages/telethon/client/auth.py", line 657, in aenter return await self.start() ^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.11/site-packages/telethon/client/auth.py", line 165, in _start value = phone() ^^^^^^^ File "/usr/local/lib/python3.11/site-packages/telethon/client/auth.py", line 22, in phone: typing.Callable[[], str] = lambda: input('Please enter your phone (or bot token): '), ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ EOFError: EOF when reading a line Please enter your phone (or bot token): Traceback (most recent call last): File "/app/main.py", line 31, in with TelegramClient('anon', APP_ID, API_HASH) as client: File "/usr/local/lib/python3.11/site-packages/telethon/helpers.py", line 219, in _sync_enter return loop.run_until_complete(self.aenter()) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.11/asyncio/base_events.py", line 653, in run_until_complete return future.result() ^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.11/site-packages/telethon/client/auth.py", line 657, in aenter return await self.start() ^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.11/site-packages/telethon/client/auth.py", line 165, in _start value = phone() ^^^^^^^ File "/usr/local/lib/python3.11/site-packages/telethon/client/auth.py", line 22, in phone: typing.Callable[[], str] = lambda: input('Please enter your phone (or bot token): '), ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ EOFError: EOF when reading a line Please enter your phone (or bot token):