我正在使用 Telethon 和 Python 3.6xx
能够从群组中检索消息,没问题,但是当涉及到 channel 时,我被卡住了。
dialogs = client(get_dialogs)
for chat in dialogs.chats:
getmessage = client.get_messages(chat.id, limit=400)
for message in getmessage:
print(message.message)
我搜索了 telethon 文档,但大多数答案都是对旧的 get_message_history
的回应。
当我尝试使用以下 chat.id = 1097988869
(news.bitcoin.com) 时,我收到以下错误(对于组 chat.id
工作正常):
PeerIdInvalidError: An invalid Peer was used. Make sure to pass the right peer type
最佳答案
公认的答案很好,但最新版本的 Telethon 可让您更轻松地实现同样的目标。这将遍历chat
中的所有 消息(对于此示例,我们使用telethon.sync
来避免键入async
):
from telethon.sync import TelegramClient
with TelegramClient(name, api_id, api_hash) as client:
for message in client.iter_messages(chat):
print(message.sender_id, ':', message.text)
变量应该很明显的地方,例如(注意这些 API 值不起作用,您需要自己的值):
name = 'anon'
api_id = 123
api_hash = 'abcdefgh'
chat = 'me'
更多使用 async
的例子可以在 client.iter_messages
中找到。文档。
关于python - Telegram 获取聊天消息/帖子 - python Telethon,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50975793/