Camilla8
Camilla8

Reputation: 171

Telethon Telegram message filter

I would like to get all the messages from a chat that have been sent today.

import sys,datetime
from telethon import TelegramClient
api_id = 1234567
api_hash = "0986asdgshjfag"
client = TelegramClient('session_name', api_id, api_hash)
client.start()

dialogs = client.get_dialogs()
chat = client.get_input_entity('username')


filter = InputMessagesFilterEmpty()
result = client(SearchRequest(
    peer=chat,  # On which chat/conversation
    q='',  # What to search for
    filter=filter,  # Filter to use (maybe filter for media)
    min_date=datetime.date.today(),  # Minimum date
    max_date=None,  # Maximum date
    offset_id=0,  # ID of the message to use as offset
    add_offset=0,  # Additional offset
    limit=5,  # How many results
    max_id=0,  # Maximum message ID
    min_id=0,  # Minimum message ID
    from_id=None,  # Who must have sent the message (peer)
    hash=0  # Special number to return nothing on no-change
))

for message in client.iter_messages(chat,filter=result):
    print(message.message)

The filter doesn't work at all, I can see more than 5 messages (the whole chat) and it doesn't care about the time. Why?

Upvotes: 3

Views: 4258

Answers (1)

Pouria Nikvand
Pouria Nikvand

Reputation: 370

You can use 'message.date'.

If you want to get the message from today you need to check the sent day. and this would be like :

if datetime.datetime.now().strftime('%Y-%m-%d') == message.date.strftime('%Y-%m-%d')

Upvotes: 1

Related Questions