我有client.get_messages(dialog.entity)但它只返回没有“已读/未读标记”的消息...那么,我怎样才能只获取未读的新消息呢?有人知道吗?
除了接受的答案之外,还可以仅获取您感兴趣的对话框GetPeerDialogsRequest,这也可以用于对整个文件夹进行操作。
要获取未读消息的数量'username':
from telethon.sync import TelegramClient
from telethon import functions, types
with TelegramClient(name, api_id, api_hash) as client:
result = client(functions.messages.GetPeerDialogsRequest(
peers=['username']
))
print(result.dialogs[0].unread_count)
Run Code Online (Sandbox Code Playgroud)
请注意,这peers可能是一个列表,因此您可以一次获取多个。请注意,该对话框包含更多信息,例如“已读取到哪个 ID”。
小智 1
每个对话框都有unread_count
x = [[d.unread_count, d.title] for d in client.get_dialogs() if not getattr(d.entity, 'is_private', False) and d.unread_count != 0]
Run Code Online (Sandbox Code Playgroud)
x = [[d.unread_count, d.title] for d in client.get_dialogs() if not getattr(d.entity, 'megagroup', False) and d.unread_count != 0]
Run Code Online (Sandbox Code Playgroud)