pythontelethon

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

  • offset_date is used to get messages prior to that date. So you should use the day after:

    async def get_messages_at_date(chat, date):
        result = []
        async for msg in client.iter_messages(chat, offset_date=date):
            if msg.date < date:
                return result
            result.append(msg)