Search code examples
telethon

Different message id depending on whether the bot is normal bot or userbot


I've been looking in the documentation and couldn't find why this is so.

If I get the message.id from a NewMessage event I get a 4-digit code but if I use a userbot and call client.iter_messages the messages come out with a 6-digit id.

I have a bot that saves message information (id included) in a database and that it returns different ids is causing me problems.

What am I missing?

Edit:

I thought it was not a bug and was asking why it was like that but they ask me to edit the question with code. So that tells me that it shouldn't happen.

import os

from telethon import TelegramClient, events
from telethon.sessions import StringSession

api_id = os.environ['TELEGRAM_API_ID']
api_hash = os.environ['TELEGRAM_API_HASH']
normal_bot_client = TelegramClient(StringSession(os.environ['TELEGRAM_BOT_SESSION']), api_id, api_hash)
user_bot_client = TelegramClient(StringSession(os.environ['TELEGRAM_USER_SESSION']), api_id, api_hash)


@normal_bot_client.on(events.NewMessage)
async def handler(event):
    print(f'{event.message.id=}')
    print(f'{event.message.text=}')
    print('-----------------')

    async with user_bot_client:
        normal_bot_user = await normal_bot_client.get_me()
        async for message in user_bot_client.iter_messages(normal_bot_user.username, 3):  # private chat with the bot
            print(f'{message.id=}')
            print(f'{message.text=}')
            print()


with normal_bot_client:
    normal_bot_client.run_until_disconnected()

Output:

event.message.id=5271
event.message.text='bye'
-----------------
message.id=422145
message.text='bye'

message.id=422144
message.text='hi'

message.id=422143
message.text='hello'

On the other hand, I just ran into this:

normal_bot_user = await normal_bot_client.get_me()
something = await user_bot_client.get_input_entity(normal_bot_user)
print(something)
something = await user_bot_client.get_input_entity(normal_bot_user.username)
print(something)

Output:

InputPeerSelf()
InputPeerUser(user_id=545165****, access_hash=-728710854129895****)

get_input_entity returns something different depending on whether it reads the entire User object or just the name. If I pass him another bot, it seems that he doesn't even look at it and says that it's himself.


Solution

  • Every account, whether it's an user account or bot account, has its own message counter. This counter starts at 1. It is used for every message the account receives in private conversations or small group chats, and is incremented by one after each message.

    This means, if it's the very first message your Bot-X receives from User-A, it will start at 1, then 2, then 3 and so on. If User-B messages Bot-X after User-A sent those three messages, the message ID will be 4.

    For megagroups (supergroups) and broadcast channels, this counter is not used. Instead, all channels have their own message counter, which also starts at 1. But everyone will see the same message ID for messages in that channel.

    This is why it's not possible to link messages in private conversations and small chats, but is possible in channels. Because every account has its own counter, and the IDs wouldn't match. They only match in channels.