我需要读取应用程序中某些公共(public) channel 的消息,例如发生https://tlgrm.ru/channels/tech据我了解,这项业务的机器人将无法工作。你需要使用客户端api,但是在任何地方都需要使用channel_id连接 channel 方法,但是我从哪里获取它我不知道,我只有 channel 名称,以及如何从它的id中获取它我没有找到这样的方法。
如何通过名称获取 channel 的 ID?
最佳答案
假设你使用的是 python,我建议 Telethon图书馆。您可以使用这段代码从 @username
获取 channel_id
和 access_hash
:
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_id
和 api_hash
。还要确保您已经对您的应用程序进行了身份验证,即您有一个有效的 session_file
。如果您不确定如何执行上述步骤,只需阅读 Github 页面中 Telethon 的自述文件即可。
关于telegram - 阅读 Telegram 公共(public) channel 的消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46525921/