admin管理员组

文章数量:1404927

Using iter_messages(), I scroll through messages in an open chat. If a message has reactions, I try to get the ID, name, username, and phone number (if available) of the user who reacted. In the Telegram app, this can be done by viewing the reactions and clicking on the user's profile.

However, when using Telethon, I get a ValueError. Here is part of the code:

        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)

Full error message:

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).

I physically checked this message with a reaction in the chat. I can see both the message and the reaction. The user is not deleted, their account exists, and from the same account that I am using in Telethon, I can view the reacting user's profile in the Telegram app, where at least their first name (first_name) is displayed.

So what is the problem?

本文标签: pythonHow to get user details from MessageReactions in TelethonStack Overflow