Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get updates for a specific private channel using telethon?

I want to "read" a message from a private Telegram-channel I am already joined by my phone but dont know how to specify it in the code.


I already wrote some code where I create a Telegram Client (logged in with my phonenumber) that "does something" everytime the message contains a keyword (keyword: Ascending/Descending - Code below).

Problem:

It only triggers when I wrote (with the same phonennumber as the API) the message to my bot, my saved messages or to one of my contacts.

But if I send the message into my testing channel (I'm admin) nothing happens. Also a message in the main channel does nothing (I'm no admin).


I Already checked: https://telethonn.readthedocs.io/en/latest/extra/basic/working-with-updates.html?highlight=events.NewMessage(chats#id5

And I am unsure how to use:

@client.on(events.NewMessage(chats=('insert something')))

Since I don't know what I need to insert.

Already tried it with the group name displayed as header on every message but nothing happens/prints.


from telethon import TelegramClient, events, utils

# Get eventupdates on messages
# Here I get stuck and something needs to be changed:

@client.on(events.NewMessage)  
async def handler(event):

#If keyword is in message -> do something

    if "Descending" in event.raw_text:
        print( 'Alert with ', event.text, '!')
    if "Ascending" in event.raw_text:
        print( 'Alert with ', event.text, '!')


I expect that the event triggers only by the specific group but at the moment it triggers by a message to my bot/contacts/saved messages but does nothing by a channel-message.

like image 556
Patrik Hörmann Avatar asked Jan 26 '23 21:01

Patrik Hörmann


1 Answers

events.NewMessage(chats=chat)

It takes a channel username, id, or invite link. However invite links are unreliable as they are subject to change.

Example:

from telethon import TelegramClient, events

client = TelegramClient('session', api_id, api_hash)

@client.on(events.NewMessage(chats="@TelethonUpdates"))
async def my_event_handler(event):
    print(event.text)

client.start()
client.run_until_disconnected()
like image 104
Qwerty-Space Avatar answered Jan 30 '23 00:01

Qwerty-Space