LonamiWebs / Telethon

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

Should masked channel peer ID be simply -(1e12 + channel_id) instead? #1359

Closed blueset closed 3 years ago

blueset commented 4 years ago

Checklist

Description Megagroups and channels on test server is giving a negative 13 digit number on Bot API, but Telethon is only masking it with prepending -100.

For example, a megagroup is giving -1000010567596 as its ID on bot API, but Telethon is recognising it as -10010567596 (since the raw ID is 10567596).

Steps of reproduction

  1. Log in with any real account (99966- accounts will not work) on a test server.
  2. Create a bot with @BotFather
  3. Create a megagroup / channel (“Chat A” onwards) and add the bot in.
  4. Send a message in Chat A
  5. Get the masked ID of Chat A via bot API (entry point: https://api.telegram.org/botTOKEN/test/getUpdates).
  6. Log in with the same account on test server using Telethon and run code below:
from telethon.sync import TelegramClient

client = TelegramClient(None, APP_ID, APP_SECRET)
client.session.set_dc(TEST_DC_ID, HOST, PORT)
client.start(phone='PHONE_NUMBER')

masked_id = -1000010567596
entity = client.get_entity(masked_id)

Traceback

>       entity = client.get_entity(masked_id)   # masked_id
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
../../efb-dev-env/lib/python3.7/site-packages/telethon/client/users.py:284: in get_entity
    functions.messages.GetChatsRequest(chats))).chats
../../efb-dev-env/lib/python3.7/site-packages/telethon/client/users.py:53: in __call__
    future = self._sender.send(request, ordered=ordered)
../../efb-dev-env/lib/python3.7/site-packages/telethon/network/mtprotosender.py:172: in send
    state = RequestState(request, self._loop)
../../efb-dev-env/lib/python3.7/site-packages/telethon/network/requeststate.py:17: in __init__
    self.data = bytes(request)
../../efb-dev-env/lib/python3.7/site-packages/telethon/tl/functions/messages.py:1141: in __bytes__
    b'\x15\xc4\xb5\x1c',struct.pack('<i', len(self.id)),b''.join(struct.pack('<i', x) for x in self.id),
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

.0 = <list_iterator object at 0x121a066d0>

>       b'\x15\xc4\xb5\x1c',struct.pack('<i', len(self.id)),b''.join(struct.pack('<i', x) for x in self.id),
    ))
E   struct.error: required argument is not an integer

Additional contexts

Part of code responsible of masking channel ID https://github.com/LonamiWebs/Telethon/blob/0683d9771a891efc07266451117b5f6295fbf2a3/telethon/utils.py#L927-L928

Lonami commented 4 years ago

Well, thank you for the detailed report. If that fixes it, we could do that.