You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Telethon从MessageReactions获取用户详情遇ValueError问题排查

问题

使用Telethon的iter_messages()遍历公开聊天中的消息,当消息存在reactions时,尝试获取做出reaction的用户的ID、姓名、用户名及手机号(若可用)。在Telegram客户端中可通过查看reaction并点击用户资料实现此操作,但使用Telethon时触发了ValueError。

相关代码片段:

if message.reactions and message.reactions.recent_reactions:
    print(message.reactions)

    peers = [reaction.peer_id for reaction in message.reactions.recent_reactions if
             isinstance(reaction.peer_id, types.PeerUser)]

    for peer in peers:
        input_entity = await app.get_input_entity(peer) # ValueError here
        user = await app.get_entity(input_entity)

        user_dict = {
            'first_name': user.first_name,
            'last_name': user.last_name,
            'username': user.username,
            'phone': user.phone,
        }

        append_user(user.id, user_dict)
        print(user.id, user_dict)

完整报错信息:

Unhandled exception on on_incoming_message
Traceback (most recent call last):
  File "C:\Users\WWW\AppData\Local\Programs\Python\Python313\Lib\site-packages\telethon\client\updates.py", line 570, in _dispatch_update
    await callback(event)
  File "C:\Users\WWW\PycharmProjects\pythonPyro\Parser\main.py", line 54, in on_incoming_message
    input_entity = await app.get_input_entity(peer)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\WWW\AppData\Local\Programs\Python\Python313\Lib\site-packages\telethon\client\users.py", line 469, in get_input_entity
    raise ValueError(
    ...<4 lines>...
    )
ValueError: Could not find the input entity for PeerUser(user_id=6638091654) (PeerUser).

经确认,该带reaction的消息真实存在,做出reaction的用户账号未被删除,且使用Telethon的同一账号在Telegram客户端中可查看该用户资料(至少显示first_name),请问问题出在哪里?

解决方案
  • 核心问题:get_input_entity依赖本地缓存的用户实体信息,但公开聊天返回的reaction用户数据仅包含PeerUser(只有user_id),本地没有缓存对应的实体,所以触发找不到的错误。Telegram客户端会自动后台拉取用户信息,但Telethon不会主动执行这个操作。
  • 修复方法:直接用user_id调用get_entity,Telethon会自动尝试从服务器拉取该用户的实体信息,无需先转换为输入实体。修改后的代码如下:
if message.reactions and message.reactions.recent_reactions:
    print(message.reactions)

    user_ids = [reaction.peer_id.user_id for reaction in message.reactions.recent_reactions if
             isinstance(reaction.peer_id, types.PeerUser)]

    for user_id in user_ids:
        try:
            user = await app.get_entity(user_id) # 直接用user_id获取实体

            user_dict = {
                'first_name': user.first_name,
                'last_name': user.last_name,
                'username': user.username,
                'phone': user.phone,
            }

            append_user(user.id, user_dict)
            print(user.id, user_dict)
        except ValueError as e:
            # 处理拉取失败的情况,比如用户隐私设置限制
            print(f"无法获取用户 {user_id} 的信息: {e}")
  • 补充说明:如果仍拉取失败,大概率是用户隐私设置禁止陌生人查看完整资料,但至少能获取到first_name这类公开字段。建议保留异常处理,避免单个用户拉取失败导致整个流程中断。

内容的提问来源于stack exchange,提问作者Entire Music

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.14 01:22:43