【问题标题】:Read the messages of the public channels from Telegram从 Telegram 阅读公共频道的消息
【发布时间】:2017-10-02 12:55:56
【问题描述】:

我需要在应用程序中阅读一些公共频道的消息,例如它发生在https://tlgrm.ru/channels/tech 据我了解,该业务的机器人将无法工作。您需要使用客户端 api,但是与通道方法连接的任何地方都需要 channel_id 但我从哪里得到它我不知道,我只有通道名称,以及如何从中获取它 id 我没有找到这样的方法。

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

【问题讨论】:

标签: telegram python-telegram-bot


【解决方案1】:

假设您使用的是 python,我建议您使用 Telethon 库。你可以使用这段代码从@username获取channel_idaccess_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_idapi_hash。还要确保你已经验证了你的应用程序,即你有一个工作的session_file。如果您不确定如何执行上述步骤,请阅读 Github 页面中 Telethon 的 README。

【讨论】:

  • AttributeError: 'TelegramClient' 对象没有属性 'invoke'
【解决方案2】:

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

from telethon.tl.functions.contacts import ResolveUsernameRequest
response = client.invoke(ResolveUsernameRequest(<username>))
messages = client.get_message_history(response.peer,limit=1000)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-02-28
    • 1970-01-01
    • 2018-02-17
    • 1970-01-01
    • 2017-12-25
    • 2017-03-04
    • 1970-01-01
    • 2016-04-04
    相关资源
    最近更新 更多