pythonbotstelegramtelegram-botpyrogram

Pyrogram: telegram API returns 400: INPUT_FILTER_INVALID after search_messages() request


So I'm trying to collect statistics about voice notes in chats, like it's count and average length; I once did it with telethon: await client.get_messages(entity=chat, from_user=my_id, filter=telethon.types.InputMessagesFilterVoice)

But now I decided to port it to pyrogram, rewrited basic logic and with request: async for vc in __usr__.search_messages(message.chat.id, filter=pyrogram.enums.MessagesFilter.VOICE_NOTE, from_user=message.chat.id): ... I get the following error:

(2024-03-29 20:05:30,835) [ERROR] Telegram says: [400 INPUT_FILTER_INVALID] - The filter is invalid for this query (caused by "messages.Search")
Traceback (most recent call last):
  File "/usr/local/lib/python3.10/dist-packages/pyrogram/dispatcher.py", line 240, in handler_worker
    await handler.callback(self.client, *args)
  File "/root/python/userbot/userbot/voice_stat.py", line 51, in __handle_outgoing_message__
    await __voice_info__(message)
  File "/root/python/userbot/userbot/voice_stat.py", line 64, in __voice_info__
    async for vc in __usr__.search_messages(message.chat.id,
  File "/usr/local/lib/python3.10/dist-packages/pyrogram/methods/messages/search_messages.py", line 130, in search_messages
    messages = await get_chunk(
  File "/usr/local/lib/python3.10/dist-packages/pyrogram/methods/messages/search_messages.py", line 35, in get_chunk
    r = await client.invoke(
  File "/usr/local/lib/python3.10/dist-packages/pyrogram/methods/advanced/invoke.py", line 79, in invoke
    r = await self.session.invoke(
  File "/usr/local/lib/python3.10/dist-packages/pyrogram/session/session.py", line 389, in invoke
    return await self.send(query, timeout=timeout)
  File "/usr/local/lib/python3.10/dist-packages/pyrogram/session/session.py", line 357, in send
    RPCError.raise_it(result, type(data))
  File "/usr/local/lib/python3.10/dist-packages/pyrogram/errors/rpc_error.py", line 91, in raise_it
    raise getattr(
pyrogram.errors.exceptions.bad_request_400.InputFilterInvalid: Telegram says: [400 INPUT_FILTER_INVALID] - The filter is invalid for this query (caused by "messages.Search")

Here's fragment of my code:

async def __voice_info__(message: pyrogram.types.Message):
    if not message.chat or not message.from_user:
        return

    ...

    async for vc in __usr__.search_messages(message.chat.id,
                                            filter=pyrogram.enums.MessagesFilter.VOICE_NOTE,
                                            from_user=message.chat.id):
        ...

    ...

    async for vc in __usr__.search_messages(message.chat.id,
                                            filter=pyrogram.enums.MessagesFilter.VOICE_NOTE,
                                            from_user=message.from_user.id):
        ...

    answer = "..."
    await message.reply(answer)

I searched the whole web and found nothing about why voice_note is invalid filter. __usr__ is indeed user and not a bot


Solution

  • If you use filter keyword argument (i.e. pass a MessageFilter other than EMPTY) along with from_user keyword argument, Telegram will reply with:

    [400 INPUT_FILTER_INVALID] - The filter is invalid for this query.

    My solution is to combine these functions and check the sender inside for each message:

    async for vc in __usr__.search_messages(message.chat.id, filter=pyrogram.enums.MessageFilter.VOICE_NOTE):
        # Assuming vc.chat.type == pyrogram.enums.ChatType.PRIVATE
        if vc.from_user == message.chat.id:
            ...
        else:
            ...