Read the messages of the public channels from Telegram
Asked Answered
F

3

6

I need to read the messages of some public channels in the application, as, for example, 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?

Freethinker answered 2/10, 2017 at 12:55 Comment(1)
The current answers here are out of date, https://mcmap.net/q/1772722/-read-public-channel-texts-using-telegram-api/4759433 is a duplicated with newer answersCainozoic
D
8

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.

Diastema answered 2/10, 2017 at 16:8 Comment(1)
AttributeError: 'TelegramClient' object has no attribute 'invoke'Alexisaley
M
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)
Monique answered 11/1, 2018 at 18:9 Comment(0)
P
0

You can use the other type of Telegram API: Telegram [client] API and TDLib

Using telethon library makes it easy to read channels history (see telethon docs).

For this, we need an api_id and an api_hash. So, do the following:

  1. Log in to Telegram core
  2. Open the API development tools area
  3. Fill out the simple form there
  4. Now you can receive your api_id and api_hash
    For more information, see Telegram's help documentation about how to get your API credentials.

Here is an example code that gets the last 5 messages of targetChannelId:

from telethon import TelegramClient


API_ID = 123456     # See above for how to get it
API_HASH = '123abc' # See above for how to get it
client = TelegramClient('my-client', API_ID, API_HASH)


async def main():
    async for message in client.iter_messages('targetChannelId', limit=5):
        print(message.id, message.text)


with client:
    client.loop.run_until_complete(main())

The first time you run this code it asks your phone number or bot token. Enter your phone number in the format +9912345... where 99 is your country code and the rest is your phone number. It then may send a login code to your Telegram app; enter it in the console.

Note: Only users (phone numbers) can see channel history messages; bots cannot (at least in telethon). Bots can only listen for channel updates only if they are one of its administrators.

The client.iter_messages() accepts other parameters like min_id which can be used to get messages only after a specific message (for example, we can save the last message id that we have processed and next time pass that id as min_id so only messages after that message are returned).

Philis answered 8/2, 2024 at 18:21 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.