簡體   English   中英

如何使用 telethon 僅從聊天中獲取未讀消息?

[英]How to get only unread messages from chat using telethon?

我有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)

請注意, peers點可能是一個列表,因此您可以一次獲取多個。 請注意,該對話框包含更多信息,例如“已讀取到哪個 ID”。

每個對話框都有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]
x = [[d.unread_count, d.title] for d in client.get_dialogs() if not getattr(d.entity, 'megagroup', False) and d.unread_count != 0]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM