search_messages()#
- Client.search_messages()#
Search for text and media messages inside a specific chat.
If you want to get the messages count only, see
Usable by Users Botssearch_messages_count().- Parameters:
chat_id (
int|str) – Unique identifier (int) or username (str) of the target chat. For your personal cloud (Saved Messages) you can simply use “me” or “self”. For a contact that exists in your Telegram address book you can use his phone number (str).query (
str, optional) – Text query string. Required for text-only messages, optional for media messages (see thefilterargument). When passed while searching for media messages, the query will be applied to captions. Defaults to “” (empty string).filter (
MessagesFilter, optional) – Pass a filter in order to search for specific kind of messages only. Defaults to any message (no filter).from_user (
int|str, optional) – Unique identifier (int) or username (str) of the target user you want to search for messages from.message_thread_id (
int, optional) – Unique identifier for the target message thread (topic) of the forum; for forum supergroups onlyoffset (
int, optional) – Sequential number of the first message to be returned. Defaults to 0.limit (
int, optional) – Limits the number of messages to be retrieved. By default, no limit is applied and all messages are returned.offset_id (
int, optional) – Identifier of the first message to be returned.min_date (
datetime, optional) – Pass a date as offset to retrieve only older messages starting from that date.max_date (
datetime, optional) – Pass a date as offset to retrieve only newer messages starting from that date.min_id (
int, optional) – If a positive value was provided, the method will return only messages with IDs more than min_id.max_id (
int, optional) – If a positive value was provided, the method will return only messages with IDs less than max_id.saved_messages_topic_id (
int|str, optional) – If not None, only messages in the specified Saved Messages topic will be returned; pass None to return all messages, or for chats other than Saved Messages.
- Returns:
Generator– A generator yieldingMessageobjects.
Example
from pyrogram import enums # Search for text messages in chat. Get the last 120 results async for message in app.search_messages(chat_id, query="hello", limit=120): print(message.text) # Search for pinned messages in chat async for message in app.search_messages(chat_id, filter=enums.MessagesFilter.PINNED): print(message.text) # Search for messages containing "hello" sent by yourself in chat async for message in app.search_messages(chat, "hello", from_user="me"): print(message.text)