LeonardiK
LeonardiK

Reputation: 1

how to count the number of reactions of a post in a channel. pyTelegramBotAPI?

I'm trying to create a chatbot that will read the number of reactions and send them to me. In the pyTelegramBotAPI documentation, I found the message_reaction_count_handler and message_reaction_handler methods, but both of them do not work. There is almost no information on the Internet since this is a recent update.

import telebot
from telebot.types import *

TOKEN = 'token'
bot = telebot.TeleBot(TOKEN)


@bot.message_handler(func=lambda message: True)
def message_handler(message):
    bot.send_message(message.chat.id, message.text)

@bot.message_reaction_handler(func=lambda update: True)
async def message_reaction_handler(update: MessageReactionUpdated):
    print("Reaction update received:")
    print(f'Message ID: {update.message_id}')
    for reaction in update.reactions:
        print(f'Reaction: {reaction.type.type}, Count: {reaction.total_count}')

@bot.message_reaction_count_handler(func=lambda update: True)
async def message_reaction_count_handler(update: MessageReactionCountUpdated):
    print("Reaction update received:")
    print(f'Message ID: {update.message_id}')
    for reaction in update.reactions:
        print(f'Reaction: {reaction.type.type}, Count: {reaction.total_count}')



if __name__ == '__main__':
    bot.polling(none_stop=True)

The message_handler function is working

Upvotes: 0

Views: 108

Answers (2)

Twiddles
Twiddles

Reputation: 73

Telegram at the moment does not send message_reaction updates from individual users in channels (for privacy reasons oddly). It only sends message_reaction_count occasionally.

Upvotes: 0

PankovEA
PankovEA

Reputation: 19

Here is an example of using these functions.

# Send a reactions to all messages with content_type 'text' (content_types defaults to ['text'])
@bot.message_handler(func=lambda message: True)
def send_reaction(message):
    bot.set_message_reaction(message.chat.id, message.id, [telebot.types.ReactionTypeEmoji("🔥")], is_big=False)


@bot.message_reaction_handler(func=lambda message: True)
def get_reactions(message):
    bot.reply_to(message, f"You changed the reaction from {[r.emoji for r in message.old_reaction]} to {[r.emoji for r in message.new_reaction]}")


bot.infinity_polling(allowed_updates=['message', 'message_reaction'])

The bot must be an administrator in the chat and must explicitly specify «message_reaction» in the list of allowed_updates to receive these updates. The update isn’t received for reactions set by bots.

Upvotes: 0

Related Questions