SearchRequest

Only users can use this request. See code examples.

---functions---
messages.search#29ee847a flags:# peer:InputPeer q:string from_id:flags.0?InputPeer saved_peer_id:flags.2?InputPeer saved_reaction:flags.3?Vector<Reaction> top_msg_id:flags.1?int filter:MessagesFilter min_date:date max_date:date offset_id:int add_offset:int limit:int max_id:int min_id:int hash:long = messages.Messages

Returns

messages.Messages

This type can be an instance of either:

ChannelMessagesMessages
MessagesNotModifiedMessagesSlice

Parameters

peerInputPeerAnything entity-like will work if the library can find its Input version (e.g., usernames, Peer, User or Channel objects, etc.).
qstring
filterMessagesFilter
min_datedate
max_datedate
offset_idint
add_offsetint
limitint
max_idint
min_idint
hashlong
from_idInputPeerThis argument defaults to None and can be omitted. Anything entity-like will work if the library can find its Input version (e.g., usernames, Peer, User or Channel objects, etc.).
saved_peer_idInputPeerThis argument defaults to None and can be omitted. Anything entity-like will work if the library can find its Input version (e.g., usernames, Peer, User or Channel objects, etc.).
saved_reactionReactionThis argument defaults to None and can be omitted. Otherwise, a list must be supplied.
top_msg_idintThis argument defaults to None and can be omitted.

Known RPC errors

This request can cause 9 known errors:

ChatAdminRequiredErrorChat admin privileges are required to do that in the specified chat (for example, to send a message in a channel which is not yours), or invalid permissions used for the channel or group.
FromPeerInvalidErrorThe given from_user peer cannot be used for the parameter.
InputConstructorInvalidErrorThe provided constructor is invalid.
InputFilterInvalidErrorThe search query filter is invalid.
InputUserDeactivatedErrorThe specified user was deleted.
PeerIdInvalidErrorAn invalid Peer was used. Make sure to pass the right peer type and that the value is valid (for instance, bots cannot start conversations).
PeerIdNotSupportedErrorThe provided peer ID is not supported.
SearchQueryEmptyErrorThe search query is empty.
UserIdInvalidErrorInvalid object ID for a user. Make sure to pass the right types, for instance making sure that the request is designed for users or otherwise look for a different one more suited.

You can import these from telethon.errors.

Example

Please refer to the documentation of client.iter_messages() to learn about the parameters and see several code examples on how to use it.

The method above is the recommended way to do it. If you need more control over the parameters or want to learn how it is implemented, open the details by clicking on the "Details" text.

from telethon.sync import TelegramClient
from telethon import functions, types

with TelegramClient(name, api_id, api_hash) as client:
    result = client(functions.messages.SearchRequest(
        peer='username',
        q='some string here',
        filter=types.InputMessagesFilterPhotos(),
        min_date=datetime.datetime(2018, 6, 25),
        max_date=datetime.datetime(2018, 6, 25),
        offset_id=42,
        add_offset=0,
        limit=100,
        max_id=0,
        min_id=0,
        hash=-12398745604826,
        from_id='username',
        saved_peer_id='username',
        saved_reaction=[types.ReactionEmoji(
            emoticon='some string here'
        )],
        top_msg_id=42
    ))
    print(result.stringify())