Pyrogram 中 GetMessageReactionsList 的 400 MSG_ID_INVALID

问题描述 投票:0回答:1

我编写了一个脚本来使用 Pyrogram 收集数据,但在某些消息上收到错误 400 MSG_ID_INVALID。我没有注意到为什么有些消息会导致错误而其他消息则不会的模式。也许问题出在异步编程上。

聊天 id 和消息 id 是正确的,因为 app.get_messages(chat_id, message.id) 始终工作正常。

from pyrogram import Client
from pyrogram.raw.functions.messages import GetMessageReactionsList


# configuration
with open('config.txt', 'r') as f:
    api_id = f.readline().strip()
    api_hash = f.readline().strip()
    chat_id = int(f.readline().strip())


app = Client(
    "my_account",
    api_id=api_id,
    api_hash=api_hash
)


async def collect_messages():
    async with app:

        chat = await app.resolve_peer(peer_id=chat_id)

        async for message in app.get_chat_history(chat_id=chat_id, limit=1):

            message_id = message.id

            # always works fine
            print(await app.get_messages(chat_id, message.id))

            # 400 MSG_ID_INVALID for some messages
            reactions = await app.invoke(GetMessageReactionsList(peer=chat, id=message.id, limit=10))


if __name__ == "__main__":
    app.run(collect_messages())

完整错误:

pyrogram.errors.exceptions.bad_request_400.MsgIdInvalid: Telegram says: [400 MSG_ID_INVALID] - The message ID used in the peer was invalid (caused by "messages.GetMessageReactionsList")
python asynchronous telegram pyrogram
1个回答
0
投票

这个错误似乎是在消息没有任何反应的情况下出现的。为了避免这种情况,您可以检查 message.reactions 是否不是 None:

async def collect_messages():
    async with app:

        chat = await app.resolve_peer(peer_id=chat_id)

        async for message in app.get_chat_history(chat_id=chat_id, limit=30):
            message_id = message.id

            # check reactions
            try:
                reactions = await app.invoke(GetMessageReactionsList(peer=chat, id=message.id, limit=10))
                print(reactions)
            except pyrogram.errors.exceptions.bad_request_400.MsgIdInvalid:
                pass
© www.soinside.com 2019 - 2024. All rights reserved.