从Telegram阅读公共频道的消息

问题描述 投票:3回答:2

我需要阅读应用程序中某些公共渠道的消息,例如它发生了https://tlgrm.ru/channels/tech据我所知,这项业务的机器人将不起作用。你需要使用客户端api,但是在任何地方,通道方法都连接到你需要的channel_id,但我在哪里得到它我不知道,我只有通道名称,我怎么从它得到它我没有找到这样的方法。

如何通过名称获取频道的ID?

telegram python-telegram-bot
2个回答
5
投票

假设你正在使用python,我建议使用Telethon库。您可以使用这段代码从channel_id获取access_hash@username

from telethon.tl.functions.contacts import ResolveUsernameRequest

client = TelegramClient(session_file, api_id=X, api_hash='X')
client.connect()
response = client.invoke(ResolveUsernameRequest("username"))
print(response.channel_id)
print(response.access_hash)

确保你已经有你的api_idapi_hash。并确保您已经验证了您的应用程序,即您有一个工作session_file。如果您不确定如何执行上述步骤,请阅读Github页面中的Telethon的README。


0
投票

在最新版本中,您可以使用频道的用户名进行此操作

from telethon.tl.functions.contacts import ResolveUsernameRequest
response = client.invoke(ResolveUsernameRequest(<username>))
messages = client.get_message_history(response.peer,limit=1000)
© www.soinside.com 2019 - 2024. All rights reserved.