问题
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?
回答1:
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.
回答2:
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)
来源:https://stackoverflow.com/questions/46525921/read-the-messages-of-the-public-channels-from-telegram