'how to get messages by date using TELETHON?

how to get message that posted today using TELETHON

I'm using the below code

date_of_post = datetime.datetime(2019, 12, 24)

with TelegramClient(name, api_id, api_hash) as client:
    for message in client.iter_messages(chat , offset_date = date_of_post):
        print(message.sender_id, ':', message.text)


Solution 1:[1]

The point made by @Lonami is valid - offset_date is used to get messages prior to that date. However, the docs describe another argument called reverse that you can supply to iter_messages :

reverse (bool, optional): If set to True, the messages will be returned in reverse order (from oldest to newest, instead of the default newest to oldest). This also means that the meaning of offset_id and offset_date parameters is reversed, although they will still be exclusive.

So, if you use it like this:


for message in client.iter_messages(chat, reverse = True, offset_date = date_of_post):
   print(message.sender_id, ':', message.text)

it should work as you expected.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1