嗨,我想尝试每次频道发送新消息时都会收到通知,为此我创建了以下代码片段:
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()但是我不明白为什么只有在channelID中使用用户名时才能工作,我想使用ID,因为某些通道是私有的。
我使用电报中的bot "@username_to_id_bot“通过通道链接检索ID。
当使用用户名时,工作正常,但当我使用ID o通道时,返回以下错误:
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"我也曾试图通过类似于这个例子的实体,但没有成功:
https://ingrom.com/python/47397/telethon-get-channel-id
有人能帮我找到解决办法吗?
发布于 2022-04-06 17:15:31
这里
@client.on(events.NewMessage(chats = [channelId]))您应该使用entity而不是chats,比如
@client.on(events.NewMessage(entity = channelId, message = "message you need to send"))https://stackoverflow.com/questions/71184876
复制相似问题