python - 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 ,但不知道如何获得它的名字。
该库的文档对初学者来说似乎不太友好。=(
谢谢
解决方案
如果当前客户端从未“看到”它,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)
推荐阅读
- android - 如何显示自定义浮动视图,如弹出菜单?
- c# - 通过递归查找字符串中的字符
- reactjs - react-animations 与 material-ui 中的网格系统有冲突
- javascript - 如果函数是异步的,为什么就绪事件不会触发
- c++ - 当强制 RVO 应用于延长临时生命周期的引用时会发生什么?
- javascript - 使用 JavaScript 选择多个相似的 HTML 元素以将其内容存储在数组中
- python - 两个 x 轴和一个 y 轴图 - Python
- javascript - 即使draggable = true,html中的元素也不能拖动
- php - 仅当在 Apache 中启用 mod_ssl 时 PHP-CURL 无法解析主机
- c# - 通过更改默认 CLR 探测顺序从自定义位置加载程序集