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?
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.
In the latest version, you would do like this using the username of the channel
from telethon.tl.functions.contacts import ResolveUsernameRequest
response = client.invoke(ResolveUsernameRequest(<username>))
messages = client.get_message_history(response.peer,limit=1000)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With