无法在Python中读取电报通道消息

olhwl3o2  于 2023-04-13  发布在  Python
关注(0)|答案(1)|浏览(123)

我正在尝试阅读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)

你能帮我找到解决办法吗?谢谢。

llycmphe

llycmphe1#

不要使用聊天标题作为一个实体。这是不可靠的。只使用用户名,chats=“intocyptoverse”
或者,如果没有用户名为说聊天(私人组,你已经加入),你需要自然地做:

  • 找到与forked Telegram客户端聊天的id(整数),将其传递给chats=。
  • 为所有事件创建一个与channel.title匹配的字符串,比username或id开销更大,可靠性更低,如果telegram没有发送完整的聊天信息,可能会浪费请求,例如:
@client.on(...)
async def only_that_chat(event):
    if not event.is_group or \ 
       (await event.get_chat()).title != "my chat title": return 

    # code here

    # no users or broadcast channels, only small and big groups, also return if the channel name doesn't match.
  • 另外,你也可以触发一个global search为所有公共聊天(搜索图标在客户端)找到id/用户名的聊天匹配您的标题,并使用它.

相关问题