电报频道-如何获取access_hash?

Fri*_*ier 5 telegram telethon

我真的很努力地理解如何在 Telethon 中使用 Telegram api。我在 Telegram 中有一些频道,我想在其中删除旧的消息。使用 inputpeerchannel() 我需要 channel_id(没问题)和 channel_hash。我不知道如何通过 channel_id 获取这个 channel_hash。来自德国的感谢

Ali*_*emi 9

为了找到频道access_hash,您应该解析频道用户名。原始 MTProto 方法contacts.resolveUsername#f93ccba3获取 @username 并返回频道信息,包括access_hash.

Telethon 中,您需要调用ResolveUsernameRequest来调用上述原始 MTProto 方法。您可以使用此代码将用户名解析为access_hash

client = TelegramClient(session_file, api_id=00000, api_hash='XXXXX')
client.connect()
response = client.invoke(ResolveUsernameRequest("your_channel_id"))
print(response.chats[0].access_hash)
client.disconnect()
Run Code Online (Sandbox Code Playgroud)