Read public channel texts using Telegram API
Asked Answered
C

2

2

I would like to create a small script that will fetch Telegram texts from a public channel (I am not the channel's admin).

I've found another question asked here: Read the messages of the public channels from Telegram
I've tried using Telethon as said in the answer, but it didn't work:

from telethon.tl.functions.contacts import ResolveUsernameRequest
import telethon

client = telethon.TelegramClient("session.txt", api_id=XYZ, api_hash='XYZ')
client.connect()
response = client.invoke(ResolveUsernameRequest("test"))
print(response.channel_id)
print(response.access_hash)

Throwing this error:

C:/Users/mypc/PycharmProjects/untitled/aa.py:5: RuntimeWarning: coroutine 'TelegramBaseClient.connect' was never awaited
  client.connect()
RuntimeWarning: Enable tracemalloc to get the object allocation traceback
Traceback (most recent call last):
  File "C:/Users/mypc/PycharmProjects/untitled/aa.py", line 6, in <module>
    response = client.invoke(ResolveUsernameRequest("test"))
AttributeError: 'TelegramClient' object has no attribute 'invoke'

I've tried reading the API documentation, but I didn't fully understand how those calls work:

https://core.telegram.org/method/channels.exportMessageLink

https://core.telegram.org/method/channels.joinChannel

https://core.telegram.org/method/channels.getMessages

I'd be grateful if someone could explain to me how those work.

Callihan answered 1/11, 2019 at 19:56 Comment(2)
client.invoke(request) is the old way to do it. You need to use client(request).Gary
See this answer for how to read channel past messages using a user account.Platte
L
3

That answer is very old. If we check Telethon's Quick-Start we have enough code to do what you need:

from telethon import TelegramClient

# Remember to use your own values from my.telegram.org!
api_id = 12345
api_hash = '0123456789abcdef0123456789abcdef'
client = TelegramClient('anon', api_id, api_hash)

async def main():
    # You can print the message history of any chat:
    async for message in client.iter_messages('USERNAME OF THE CHANNEL'):
        print(message.sender.username, message.text)

with client:
    client.loop.run_until_complete(main())
Lapsus answered 1/11, 2019 at 20:8 Comment(2)
But, how do you connect your bot to listen public channel messages? Your code does not answer the question.Raffle
The code answers the question fine ("fetch text from a public channel"). Actively listen for incoming messages is a different question.Lapsus
A
1

Well like it says, TelegramClient has no invoke method. Have you tried client(ResolveUsernameRequest("test"))?

Actionable answered 1/11, 2019 at 20:0 Comment(2)
It would be more helpful to note that invoke was the old way of doing it, and refer to the page that explains how full API is done. In addition, there's a friendly method which is a far better alternative.Lapsus
Fair. I didn't know it was the old way of doing it, seeing as I'm still fairly new to TelethonActionable

© 2022 - 2024 — McMap. All rights reserved.