I wrote a script to collect data using Pyrogram but got the error 400 MSG_ID_INVALID on some messages. I didn't notice a pattern why some messages cause errors and others don't. Maybe the problem is with asynchronous programming.
Chat id and message id are correct because app.get_messages(chat_id, message.id) always works fine.
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())
Full error:
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")
It seems that this error appears when the message doesn't have any reactions. To avoid it you can check that message.reactions is not 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