Felix Sauer
Felix Sauer

Reputation: 51

Telethon events.NewMessage from specific channel

I would like to get the New Messages from a specific channel. I have tried the following until now:

from telethon import TelegramClient, events

api_id = 242...
api_hash = '8a06ca620417c9964a058e0dc...'
bot_token = '1474729480:AAEhUPmVX_m...'
channelId = '-36744...'

client = TelegramClient('bot', api_id, api_hash).start(bot_token=bot_token)

client.start()

@client.on(events.NewMessage(channelId))
async def main(event):
     me = client.get_me()
     print(me.stringify())
     print(event.stringify())
        

client.run_until_disconnected()

Unfortunately, it did not work. Does anyone know why? Did I miss something? Greetings

Upvotes: 5

Views: 17371

Answers (1)

Igor Astr
Igor Astr

Reputation: 101

Please use this:

chats = [channelId]

from telethon import TelegramClient, events

api_id = 242...
api_hash = '8a06ca620417c9964a058e0dc...'
bot_token = '1474729480:AAEhUPmVX_m...'
channelId = '-36744...'

client = TelegramClient('bot', api_id, api_hash).start(bot_token=bot_token)

client.start()

@client.on(events.NewMessage(chats = [channelId]))
async def main(event):
     me = await client.get_me()
     print(me.stringify())
     print(event.stringify())
    

client.run_until_disconnected()

Upvotes: 10

Related Questions