Search code examples
telethon

Telegram Global Search using the raw API function via Telethon


I am trying to use the SearchGlobalRequest API method for global, full-text search on Telegram, but I am not sure what to use for some of the arguments, especially the offset_peer parameter. When I do this:

try:
    result = client(SearchGlobalRequest(
        q=search_term,
        filter=None,
        min_date=datetime.datetime.strptime(min_date, '%Y-%m-%d'),
        max_date=datetime.datetime.strptime(max_date, '%Y-%m-%d'),
        offset_rate=-1,
        # offset_peer=None,
        offset_id=-1,
        limit=10
    ))
except Exception as e:
    print(e)

I get __init__() missing 1 required positional argument: 'offset_peer'.

When I try to pass None as offset_peer, I get Cannot cast NoneType to any kind of Peer. I am not trying to search in any specific channel, I just want to specify the start and end date and find all (or rather as many as possible) matching results.

I am using Telethon version 1.24.0.


Solution

  • Next code works for me:

    from telethon.tl.functions.messages import SearchGlobalRequest
    from telethon.tl.types import InputMessagesFilterEmpty, InputPeerEmpty
    
    results = client(SearchGlobalRequest(
        q='your_query_here',
        filter=InputMessagesFilterEmpty(),
        min_date=None,
        max_date=None,
        offset_rate=0,
        offset_peer=InputPeerEmpty(),
        offset_id=0,
        limit=100,
        folder_id=None
    ))
    print(results.stringify())