You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

Telethon库除获取消息及评论外,能否获取消息Reactions?

Can Telethon Retrieve Message Reactions (Using messages.getMessageReactionsList)?

Absolutely! You can definitely fetch message reactions with Telethon, and it directly supports the official Telegram API's messages.getMessageReactionsList method you referenced. Let me break down how to do this, depending on what exactly you need:

1. Getting Aggregated Reaction Stats (Simple)

If you just want a summary of reactions (like how many 👍, ❤️, etc. a message has), you don't even need a separate method call. When you fetch messages using client.get_messages(), the returned Message objects include a reactions attribute that contains this aggregated data.

Example snippet:

async def get_aggregated_reactions(client, chat_id, message_id):
    message = await client.get_messages(chat_id, ids=message_id)
    if message.reactions:
        print(f"Reactions summary:")
        for reaction in message.reactions.results:
            print(f"- {reaction.reaction.emoticon}: {reaction.count}")

2. Getting Detailed User Reaction List

If you need to see exactly which users reacted with which emoji (the full list that messages.getMessageReactionsList provides), Telethon exposes this via the GetMessageReactionsList request class.

Here's how to implement it:

from telethon.tl.functions.messages import GetMessageReactionsList

async def get_detailed_reactions(client, chat_id, message_id, limit=100):
    # Fetch the full list of reactions and their associated users
    reaction_list = await client(GetMessageReactionsList(
        peer=chat_id,
        msg_id=message_id,
        limit=limit  # Adjust this to get more/less results per call
    ))
    
    # Process the results
    for item in reaction_list.reactions:
        user = item.user
        reaction = item.reaction
        print(f"User {user.first_name} (@{user.username}) reacted with {reaction.emoticon}")
    
    return reaction_list

Key Notes

  • Make sure you're using the latest version of Telethon—older versions might not have the GetMessageReactionsList wrapper implemented.
  • You'll need proper permissions in the target chat (e.g., being able to view messages and reactions; some private groups/channels might restrict this).
  • For large reaction lists, you might need to handle pagination (using the offset parameter in GetMessageReactionsList) to fetch all results.

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

火山引擎 最新活动