Fritz Maier
Fritz Maier

Reputation: 71

Telegram channel- how to get access_hash?

I try really hard to understand howto use Telegram api with telethon. I have some Channels in Telegram, where i want to delete older Messages. Using inputpeerchannel() i need channel_id (No Problem) and channel_hash. I cant findout howto get this channel_hash by channel_id. Thank you from germany

Upvotes: 6

Views: 18588

Answers (3)

Rajathithan Rajasekar
Rajathithan Rajasekar

Reputation: 410

Here is the updated code. replace appid, apphash and username

with TelegramClient('session.session', <app-id>,<app-hash>') as client:
result = client(functions.contacts.ResolveUsernameRequest(
    username=<username>
))
print(result.stringify())

Upvotes: 0

Huy
Huy

Reputation: 508

There are 4 ways to get access hash:

  1. From a Group
  2. From username
  3. From contact list
  4. From chats message

So, if you have id only, there is no way to get access hash

Upvotes: 2

Ali Hashemi
Ali Hashemi

Reputation: 3368

In order to find channel access_hash, you should resolve channel username. Original MTProto method contacts.resolveUsername#f93ccba3 gets @username and returns channel info including access_hash.

In telethon you need to invoke ResolveUsernameRequest to call the above original MTProto method. You may use this code to resolve username to 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()

Upvotes: 11

Related Questions