【发布时间】:2021-01-01 12:04:49
【问题描述】:
我是 python 及其框架的新手,我无法从电报频道访问最新消息。
我想从频道获取最新消息并使用我的代码处理它们。通过在stackoverflow中进行一些搜索,我找到了获取频道消息的解决方案。但是该代码会转储该电报频道中的所有消息。
获取频道消息的代码。
from telethon import TelegramClient, events, sync
# These example values won't work. You must get your own api_id and
# api_hash from https://my.telegram.org, under API Development.
api_id = 123456
api_hash = 'abcdefghijklmnopqrstuvwxyz123456789'
client = TelegramClient('anon', api_id, api_hash)
async def main():
# You can print the message history of any chat:
async for message in client.iter_messages('SampleChannel'):
print(message.sender.username, message.text)
print('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx')
with client:
client.loop.run_until_complete(main())
我只想要该频道上的最新消息。建议我需要修改的代码。
【问题讨论】:
-
也许您不应该公开您的 API id 和哈希?我不知道这对你来说是否重要,这对你来说是一个秘密还是你只是在试验的天气