Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read the messages of the public channels from Telegram

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?

like image 459
wpbloger Avatar asked Jan 30 '23 11:01

wpbloger


2 Answers

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.

like image 95
Ali Hashemi Avatar answered Feb 05 '23 18:02

Ali Hashemi


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)
like image 24
Dark Light Avatar answered Feb 05 '23 19:02

Dark Light