首页 > 解决方案 > Telethon - 检索用户信息时出错

问题描述

在尝试获取用户数据时,我遇到了电视节目错误。首先,我从某些组收到新消息,这没关系,但是当我尝试获取用户数据(姓名、名字等)时 - 有时没问题,但大多失败并出现错误

ValueError: Could not find the input entity for "12345678". 
Please read https://telethon.readthedocs.io/en/latest/extra/basic/entities.html 
to find out more details.

我读了很多次那篇文章,也尝试使用它所说的client.get_input_entity,但它没有帮助

这是我的代码:

import logging
from telethon import TelegramClient, events


logging.basicConfig(level=logging.WARNING)
logging.getLogger('asyncio').setLevel(logging.ERROR)

entity = 'session'  # session
api_id = 123456
api_hash = 'hash'
phone = '1234567'

chats = ['group1', 'group2', 'group3']


client = TelegramClient(entity, api_id, api_hash)


@client.on(events.NewMessage(chats=chats))
async def normal_handler(event):


print(event.message.message)
print(event.date)
print(event.from_id)
print(event.message.to_id)
#user = await client.get_input_entity(event.from_id)
user = await client.get_entity(event.from_id)



client.start()
client.run_until_disconnected()

我该如何解决?

还有一个问题,如何检索有关组的信息?我知道它是来自event.message.to_id的 id ,但不知道如何获得它的名字。

该库的文档对初学者来说似乎不太友好。=(

谢谢

标签: pythontelethon

解决方案


如果当前客户端从未“看到”它,Telegram 不允许通过整数 id 获取用户配置文件。

Telethon 文档(https://telethon.readthedocs.io/en/latest/extra/basic/entities.html)建议使用以下选项来“看到”联系:

  • 如果您有公开对话:使用client.get_dialogs()
  • 如果您在同一组中:使用client.get_participants('groupname')
  • 如果你是它在组中转发:使用client.get_messages('groupname', 100)

选择哪一个适用于您的情况。其中之一将“看到”联系,并且可以使用client.get_entity(event.from_id)


推荐阅读