Reputation: 85
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.
Upvotes: 5
Views: 13291
Reputation: 443
Update for new Telegram web interface and telethon==1.24.0
It is no longer possible to get the channel id from the website. Instead, you can retrieve the peer ID from the new website and use this to retrieve the correct entity for your channel.
chatlist-chat rp active
<li class="chatlist-chat rp active" data-peer-id="-23456272">
Take a note of the data-peer-id
- in this case it is -23456272
data-peer-id
to get the correct PeerChannel
entity and use this to read the messages using the code below.import asyncio
from telethon import TelegramClient
from telethon.tl.types import PeerChannel
# These example values won't work. You must get your own api_id and
# api_hash from https://my.telegram.org, under API Development.
api_id = 87654321
api_hash = 'your-app-hash'
peer_channel_id = -23456272 # replace with channel peer
async with TelegramClient('anon', api_id, api_hash) as client:
my_channel = await client.get_entity(PeerChannel(peer_channel_id))
async for message in client.iter_messages(my_channel, limit=10):
print(message.sender_id, ':', message.text)
Upvotes: 3
Reputation: 144
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()
Upvotes: 8
Reputation: 1731
In case if someone like me come searching to find a method to get updates from multiple channels,
myChannelIDList = [xxxxxxxxxxx,xxxxxxxxxxx,myChannelName,...]
@client.on(events.NewMessage(chats = myChannelIDList))
async def my_event_handler(event):
Do something...
Simply adding a list of all the channels as chats is sufficient. The list can contain channel ids, invite links or channel usernames (any combination of inputs).
To get the Channel ID
To get the Channel Username
im?p=@
MyChannelName
Examples :
https://web.telegram.org/#/im?p=c**12112121212**_17878787878787878
https://web.telegram.org/#/im?p=s**12112121212**_17878787878787878
https://web.telegram.org/#/im?p=u**12112121212**_17878787878787878
https://web.telegram.org/#/im?p=@**MyChannelName**
Upvotes: 6
Reputation: 530
or you can do something like this:
sourceChannelsID=[-xxxxxxxxxxx,-xxxxxxxxxxx,...]
@client.on(events.NewMessage(outgoing=False))
async def my_event_handler(event):
if event.chat_id in sourceChannelsID:
Do something...
Upvotes: 2