python - Telegram 获取聊天消息/帖子 - python Telethon

标签 python telethon

我正在使用 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/

相关文章:

telegram - AuthKeyDuplicatedError 电视节目

python - 关于逻辑回归的问题

python - pyplot 图例的 Facecolor 不显示

python - 如何从文件中读取并将其作为整数放入列表中? Python

python - 使用 Python3 将联系人列表从 CSV 文件导入 Telegram

telegram - 使用 telethon 从 Telegram 中的新消息中获取文本

python - Pandas :基于两列将一个数据框中的值替换为另一个数据框中的值

python - Django mysql 客户端

python - 在 telethon.events 中使用 "chats"参数

python - Telethon:留下聊天而不是 channel 请求