Reputation: 41
Hi i want ot try to receive a notification every time a channel send new messages, for this i have created this snippet:
from telethon import TelegramClient, events
api_id = XXXX
api_hash = 'XXXXXXX'
channelId = '-100XXXXXXXX'
client = TelegramClient('anon', api_id, api_hash)
client.start()
@client.on(events.NewMessage(chats = [channelId]))
async def main(event):
# print(event.raw_text)
client.run_until_disconnected()
But i don't understand why work only if use username in channelID, i want use ID because some channel are private.
I used the bot "@username_to_id_bot" in telegram to retrieve the ID through the link of channel.
When use username work fine but when i use the ID o channel return this error:
Task exception was never retrieved
future: <Task finished name='Task-10' coro=<UpdateMethods._dispatch_update() done, defined at C:\Users\Administrator\AppData\Local\Programs\Python\Python310\lib\site-packages\telethon\client\updates.py:399> exception=ValueError('Cannot find any entity corresponding to "-100XXXXXXXX"')>
Traceback (most recent call last):
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python310\lib\site-packages\telethon\client\updates.py", line 458, in _dispatch_update
await builder.resolve(self)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python310\lib\site-packages\telethon\events\common.py", line 99, in resolve
await self._resolve(client)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python310\lib\site-packages\telethon\events\newmessage.py", line 93, in _resolve
await super()._resolve(client)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python310\lib\site-packages\telethon\events\common.py", line 103, in _resolve
self.chats = await _into_id_set(client, self.chats)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python310\lib\site-packages\telethon\events\common.py", line 33, in _into_id_set
chat = await client.get_input_entity(chat)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python310\lib\site-packages\telethon\client\users.py", line 437, in get_input_entity
await self._get_entity_from_string(peer))
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python310\lib\site-packages\telethon\client\users.py", line 574, in _get_entity_from_string
raise ValueError(
ValueError: Cannot find any entity corresponding to "-100xxxxx"
I have tried to pass also entities like this example without success:
https://ingrom.com/python/47397/telethon-get-channel-id
can someone help me to find a solution ?
Upvotes: 4
Views: 2178
Reputation: 11
Here
@client.on(events.NewMessage(chats = [channelId]))
you should use entity
instead of chats
, like
@client.on(events.NewMessage(entity = channelId, message = "message you need to send"))
Upvotes: 0