不同的消息 ID 取决于机器人是普通机器人还是用户机器人。

huangapple go评论89阅读模式
英文:

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.

英文:

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.

答案1

得分: 0

每个账户,无论是用户账户还是机器人账户,都有自己的消息计数器。该计数器从1开始。它用于账户在私人对话或小群聊中接收的每条消息,并在每条消息后增加一。

这意味着,如果这是您的Bot-X从User-A那里收到的第一条消息,它将从1开始,然后是2,然后是3,依此类推。如果User-B在User-A发送了这三条消息后给Bot-X发送消息,消息ID将为4。

对于大型群组(超级群组)和广播频道,不使用此计数器。相反,所有频道都有自己的消息计数器,也从1开始。但每个人都将在该频道中看到相同的消息ID。

这就是为什么无法链接私人对话和小群聊中的消息,但可以在频道中。因为每个账户都有自己的计数器,而ID不会匹配。它们只在频道中匹配。

英文:

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.

huangapple
  • 本文由 发表于 2023年3月7日 06:37:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/75656481.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定