我正在尝试阅读Telegram上发布的名为“Into the Cryptoverse”的消息
代码如下:
from telethon import TelegramClient, events, sync
# Remember to use your own values from my.telegram.org!
api_id = "MY_API_ID"
api_hash = "MY_API_HASH"
client = TelegramClient('anon', api_id, api_hash)
@client.on(events.NewMessage(chats='Into The Cryptoverse'))
async def my_event_handler(event):
print(event.raw_text)
client.start()
client.run_until_disconnected()
当我运行程序时,有人在那个组中发布了一条消息,我得到了这个错误:
> Task exception was never retrieved
future: <Task finished name='Task-57' coro=<UpdateMethods._dispatch_update() done, defined at C:\Users\balatka.da\PycharmProjects\pythonProject\venv\lib\site-packages\telethon\client\updates.py:471> exception=ValueError('Cannot find any entity corresponding to "Into The Cryptoverse"')>
Traceback (most recent call last):
File "C:\Users\balatka.da\PycharmProjects\pythonProject\venv\lib\site-packages\telethon\client\updates.py", line 511, in _dispatch_update
await builder.resolve(self)
File "C:\Users\balatka.da\PycharmProjects\pythonProject\venv\lib\site-packages\telethon\events\common.py", line 99, in resolve
await self._resolve(client)
File "C:\Users\balatka.da\PycharmProjects\pythonProject\venv\lib\site-packages\telethon\events\newmessage.py", line 93, in _resolve
await super()._resolve(client)
File "C:\Users\balatka.da\PycharmProjects\pythonProject\venv\lib\site-packages\telethon\events\common.py", line 103, in _resolve
self.chats = await _into_id_set(client, self.chats)
File "C:\Users\balatka.da\PycharmProjects\pythonProject\venv\lib\site-packages\telethon\events\common.py", line 33, in _into_id_set
chat = await client.get_input_entity(chat)
File "C:\Users\balatka.da\PycharmProjects\pythonProject\venv\lib\site-packages\telethon\client\users.py", line 437, in get_input_entity
await self._get_entity_from_string(peer))
File "C:\Users\balatka.da\PycharmProjects\pythonProject\venv\lib\site-packages\telethon\client\users.py", line 574, in _get_entity_from_string
raise ValueError(
ValueError: Cannot find any entity corresponding to "Into The Cryptoverse"
Process finished with exit code 0
当我尝试键入名称聊天组这样:“Into_The_Cryptoverse”,每当有人在群里发布消息时,我都会再次收到这个错误:
> Task exception was never retrieved
future: <Task finished name='Task-37' coro=<UpdateMethods._dispatch_update() done, defined at C:\Users\balatka.da\PycharmProjects\pythonProject\venv\lib\site-packages\telethon\client\updates.py:471> exception=UsernameInvalidError('Nobody is using this username, or the username is unacceptable. If the latter, it must match r"[a-zA-Z][\\w\\d]{3,30}[a-zA-Z\\d]" (caused by ResolveUsernameRequest)')>
Traceback (most recent call last):
File "C:\Users\balatka.da\PycharmProjects\pythonProject\venv\lib\site-packages\telethon\client\updates.py", line 511, in _dispatch_update
await builder.resolve(self)
File "C:\Users\balatka.da\PycharmProjects\pythonProject\venv\lib\site-packages\telethon\events\common.py", line 99, in resolve
await self._resolve(client)
File "C:\Users\balatka.da\PycharmProjects\pythonProject\venv\lib\site-packages\telethon\events\newmessage.py", line 93, in _resolve
await super()._resolve(client)
File "C:\Users\balatka.da\PycharmProjects\pythonProject\venv\lib\site-packages\telethon\events\common.py", line 103, in _resolve
self.chats = await _into_id_set(client, self.chats)
File "C:\Users\balatka.da\PycharmProjects\pythonProject\venv\lib\site-packages\telethon\events\common.py", line 33, in _into_id_set
chat = await client.get_input_entity(chat)
File "C:\Users\balatka.da\PycharmProjects\pythonProject\venv\lib\site-packages\telethon\client\users.py", line 437, in get_input_entity
await self._get_entity_from_string(peer))
File "C:\Users\balatka.da\PycharmProjects\pythonProject\venv\lib\site-packages\telethon\client\users.py", line 553, in _get_entity_from_string
result = await self(
File "C:\Users\balatka.da\PycharmProjects\pythonProject\venv\lib\site-packages\telethon\client\users.py", line 30, in __call__
return await self._call(self._sender, request, ordered=ordered)
File "C:\Users\balatka.da\PycharmProjects\pythonProject\venv\lib\site-packages\telethon\client\users.py", line 84, in _call
result = await future
telethon.errors.rpcerrorlist.UsernameInvalidError: Nobody is using this username, or the username is unacceptable. If the latter, it must match r"[a-zA-Z][\w\d]{3,30}[a-zA-Z\d]" (caused by ResolveUsernameRequest)
你能帮我找到解决办法吗?谢谢。
1条答案
按热度按时间llycmphe1#
不要使用聊天标题作为一个实体。这是不可靠的。只使用用户名,chats=“intocyptoverse”
或者,如果没有用户名为说聊天(私人组,你已经加入),你需要自然地做: