Read the messages of the public channels from Telegram

wpbloger picture wpbloger · Oct 2, 2017 · Viewed 7.9k times · Source

I need to read the messages of some public channels in the application, as for example it happens https://tlgrm.ru/channels/tech As I understood, the bot for this business will not work. You need to use client api, but everywhere that with the channel methods are connected everywhere you need channel_id but where do I get it I do not know, I only have channel names, and how do I get it from it id I did not find such a method.

How can I get the channel's id by its name?

Answer

Ali Hashemi picture Ali Hashemi · Oct 2, 2017

Assuming you're using python, I suggest Telethon library. You can use this piece of code to get channel_id and access_hash from @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)

Make sure you have already got your api_id and api_hash. And also make sure you have authenticated your app i.e. you have a working session_file. Just read Telethon's README in the Github page if you're not sure how to perform above steps.