LonamiWebs / Telethon

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

Could not find a matching Constructor ID #69

Closed danil-lashin closed 7 years ago

danil-lashin commented 7 years ago

Hi!

I'm having strange error when trying to invoke GetHistoryRequest, can you help me? This error started to occur recently, have no problem with it yesterday.

Traceback (most recent call last):
  File "/home/forge/.local/lib/python3.5/site-packages/flask/app.py", line 1982, in wsgi_app
    response = self.full_dispatch_request()
  File "/home/forge/.local/lib/python3.5/site-packages/flask/app.py", line 1614, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/home/forge/.local/lib/python3.5/site-packages/flask/app.py", line 1517, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/home/forge/.local/lib/python3.5/site-packages/flask/_compat.py", line 33, in reraise
    raise value
  File "/home/forge/.local/lib/python3.5/site-packages/flask/app.py", line 1612, in full_dispatch_request
    rv = self.dispatch_request()
  File "/home/forge/.local/lib/python3.5/site-packages/flask/app.py", line 1598, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "main.py", line 469, in get_response
    add_offset=0))
  File "/home/forge/channelparser/telethon/telegram_client.py", line 175, in invoke
    self.sender.receive(request, timeout)
  File "/home/forge/channelparser/telethon/network/mtproto_sender.py", line 140, in receive
    request)
  File "/home/forge/channelparser/telethon/network/mtproto_sender.py", line 218, in process_msg
    return self.handle_rpc_result(msg_id, sequence, reader, request)
  File "/home/forge/channelparser/telethon/network/mtproto_sender.py", line 354, in handle_rpc_result
    request.on_response(compressed_reader)
  File "/home/forge/channelparser/telethon/tl/functions/messages/get_history.py", line 49, in on_response
    self.result = reader.tgread_object()
  File "/home/forge/channelparser/telethon/utils/binary_reader.py", line 128, in tgread_object
    result.on_response(self)
  File "/home/forge/channelparser/telethon/tl/types/messages/channel_messages.py", line 64, in on_response
    messages_item = reader.tgread_object()
  File "/home/forge/channelparser/telethon/utils/binary_reader.py", line 128, in tgread_object
    result.on_response(self)
  File "/home/forge/channelparser/telethon/tl/types/message.py", line 148, in on_response
    self.media = reader.tgread_object()
  File "/home/forge/channelparser/telethon/utils/binary_reader.py", line 128, in tgread_object
    result.on_response(self)
  File "/home/forge/channelparser/telethon/tl/types/message_media_web_page.py", line 29, in on_response
    self.webpage = reader.tgread_object()
  File "/home/forge/channelparser/telethon/utils/binary_reader.py", line 128, in tgread_object
    result.on_response(self)
  File "/home/forge/channelparser/telethon/tl/types/web_page.py", line 162, in on_response
    self.cached_page = reader.tgread_object()
  File "/home/forge/channelparser/telethon/utils/binary_reader.py", line 128, in tgread_object
    result.on_response(self)
  File "/home/forge/channelparser/telethon/tl/types/page_full.py", line 50, in on_response
    blocks_item = reader.tgread_object()
  File "/home/forge/channelparser/telethon/utils/binary_reader.py", line 123, in tgread_object
    raise TypeNotFoundError(constructor_id)
telethon.errors.TypeNotFoundError: (TypeNotFoundError(...), 'Could not find a matching Constructor ID for the TLObject that was supposed to be read with ID 0xef1751b5. Most likely, a TLObject was trying to be read when it should not be read.')
gtavasoli commented 7 years ago

I have the same error. I don't know when this error occurred but I remove session file and retry. It's work.

Lonami commented 7 years ago

Telethon probably crashed when trying to receive a request before, I assume. So Telegram just keeps sending the rest of that old request which you couldn't fully receive. And since Telethon doesn't know it was receiving a request before, it cannot carry on, and attempts to read a new request (starting with its constructor ID) which, obviously, doesn't match.

A possible workaround would maybe read until there is no more data left. Not the best option though.

gtavasoli commented 7 years ago

I get the error after rerun this code. At first run, the code worked properly but at second run error happened.

from telethon import TelegramClient
from telethon.tl.functions.messages import GetMessagesViewsRequest
from telethon.utils import InputPeerChannel

client = TelegramClient('@Sess', api_id, api_hash)
client.connect()

username = 'VahidOnline'

channel_info = client.invoke(ResolveUsernameRequest(username))
cid = channel_info.peer.channel_id
ah = channel_info.chats[0].access_hash

client.invoke(GetMessagesViewsRequest(InputPeerChannel(cid, ah), [23901], False))
Lonami commented 7 years ago

@gtavasoli I tried your code and I can confirm that such a strange behaviour occurs. What happens, though, is that after invoking the InitConnectionRequest, 0x1cb5c415 is received. This corresponds to a vector, so I would guess that Telegram is resending the result. Why? I don't know.

Lonami commented 7 years ago

Alright indeed, I get the last result, which is a vector of integers, and then I receive an ack for InitConnectionRequest. There's one problem, though. Currently there's no way of telling whether the result I'm reading for a request is of the correct type. And even if there was, there's no way of telling the type of objects to be read for the vector (it's just a "vector", I have to guess the type). So even if I added support to read vectors as a generic object, it would fail, because I wouldn't know the size of the objects about to be read. Ideas?

danil-lashin commented 7 years ago

Started getting this error when trying to get history of several channels. Other are working fine

Vel-Rus commented 7 years ago

Hi! I also got this error. Traceback (most recent call last): File "F:\Python35-32\telegram_API\test - copy.py", line 21, in <module> client.connect() File "C:\Python\Python35-32\lib\site-packages\telethon\telegram_client.py", line 118, in connect layer=layer, query=query)) File "C:\Python\Python35-32\lib\site-packages\telethon\telegram_client.py", line 175, in invoke self.sender.receive(request, timeout) File "C:\Python\Python35-32\lib\site-packages\telethon\network\mtproto_sender.py", line 139, in receive request) File "C:\Python\Python35-32\lib\site-packages\telethon\network\mtproto_sender.py", line 222, in process_msg return self.handle_container(msg_id, sequence, reader, request) File "C:\Python\Python35-32\lib\site-packages\telethon\network\mtproto_sender.py", line 285, in handle_container if not self.process_msg(inner_msg_id, sequence, reader, request): File "C:\Python\Python35-32\lib\site-packages\telethon\network\mtproto_sender.py", line 217, in process_msg return self.handle_rpc_result(msg_id, sequence, reader, request) File "C:\Python\Python35-32\lib\site-packages\telethon\network\mtproto_sender.py", line 363, in handle_rpc_result request.on_response(reader) File "C:\Python\Python35-32\lib\site-packages\telethon\tl\functions\invoke_with_layer.py", line 34, in on_response self.result = reader.tgread_object() File "C:\Python\Python35-32\lib\site-packages\telethon\utils\binary_reader.py", line 123, in tgread_object raise TypeNotFoundError(constructor_id) telethon.errors.TypeNotFoundError: (TypeNotFoundError(...), 'Could not find a matching Constructor ID for the TLObject that was supposed to be read with ID 0x1cb5c415. Most likely, a TLObject was trying to be read when it should not be read.')

2 or 3 requests went fine, then this error, and I can not Create the client and connect. Starting with this code, an error is generated. # (2) Create the client and connect client = TelegramClient('username', api_id, api_hash) client.connect() It starts working fine again if I create a new .session file and then again an error after several requests. I worked with ImportContactsRequest, DeleteContactRequest, GetUsersRequest

danil-lashin commented 7 years ago

Solved this error by doing this:

def tgread_object(self):
        """Reads a Telegram object"""
        constructor_id = self.read_int(signed=False)
        clazz = tlobjects.get(constructor_id, None)
        if clazz is None:
            value = constructor_id
            if value == 0x997275b5:  # boolTrue
                return True
            elif value == 0xbc799737:  # boolFalse
                return False
            # **--> HERE: if error then try to use next int as constructor_id <--**
            return self.tgread_object() 
Lonami commented 7 years ago

Solved this error by doing this: …

For what requests did it solve the error? Does it actually read the expected object?

danil-lashin commented 7 years ago

@Lonami, for all my requests. Especially for "get message history" and "get full channel". And yep, it works as expected.

Lonami commented 7 years ago

It… would be a strange fix to just ignore an object ID, usually, its length comes after it, and then, n items. But if Telegram is just sending it yet it's safe to ignore. Not sure if we should keep that "fix", it seems awkward at first.

Lonami commented 7 years ago

And yep, it works as expected.

It doesn't for me, I tried with the example posted above, and then, tried to read a tgobject. It failed, since the integer returned was 1 (the length of the vector).

Lonami commented 7 years ago

Tested, #89 works. Nice job, @cher-nov!

cher-nov commented 7 years ago

Tested, #89 works. Nice job, @cher-nov!

pic

pspvhp commented 4 years ago

I have error seem the same but I cannot fix it with #89. I am using Telethon to forward chat from group A to group B. Anything works well if text chat only, the error come up when user send gif , image and sticker.

Traceback (most recent call last): File "C:\telegram-recorder-master\main.py", line 117, in <module> client.idle() File "C:\Program Files\Python36\lib\site-packages\telethon\telegram_bare_client.py", line 696, in idle self._sender.receive(update_state=self.updates) File "C:\Program Files\Python36\lib\site-packages\telethon\network\mtproto_sender.py", line 180, in receive self._process_msg(remote_msg_id, remote_seq, reader, update_state) File "C:\Program Files\Python36\lib\site-packages\telethon\network\mtproto_sender.py", line 234, in _process_msg return self._handle_container(msg_id, sequence, reader, state) File "C:\Program Files\Python36\lib\site-packages\telethon\network\mtproto_sender.py", line 392, in _handle_container if not self._process_msg(inner_msg_id, sequence, reader, state): File "C:\Program Files\Python36\lib\site-packages\telethon\network\mtproto_sender.py", line 247, in _process_msg obj = reader.tgread_object() File "C:\Program Files\Python36\lib\site-packages\telethon\extensions\binary_reader.py", line 143, in tgread_object return clazz.from_reader(self) File "C:\Program Files\Python36\lib\site-packages\telethon\tl\types\__init__.py", line 20542, in from_reader _x = reader.tgread_object() File "C:\Program Files\Python36\lib\site-packages\telethon\extensions\binary_reader.py", line 141, in tgread_object raise TypeNotFoundError(constructor_id) telethon.errors.common.TypeNotFoundError: Could not find a matching Constructor ID for the TLObject that was supposed to be read with ID 0x450b7115. Most likely, a TLObject was trying to be read when it should not be read..

I don't need forward image or gif so can you help me to ignore, skip the gif /image chat.

Lonami commented 4 years ago

@pspvhp you are using a very old version (I can tell from your client.idle() which doesn't exist in newer versions). For anyone having this issue, the only solution is to update (or to manually mantain that very old version yourself with a recent layer).