3

我想从今天发送的聊天中获取所有消息。

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)

过滤器根本不起作用,我可以看到超过 5 条消息(整个聊天)而且它不关心时间。为什么?

4

1 回答 1

1

您可以使用“message.date”。

如果您想从今天收到消息,您需要检查发送日期。这就像:

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

于 2019-02-10T05:46:05.077 回答