如何统计频道中帖子的反应数。 pyTelegramBotAPI?

问题描述 投票:0回答:1

我正在尝试创建一个聊天机器人,它将读取反应数量并将其发送给我。在pyTelegramBotAPI文档中,我找到了message_reaction_count_handler和message_reaction_handler方法,但是它们都不起作用。由于这是最近的更新,因此互联网上几乎没有任何信息。

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)

message_handler 函数正在工作

telegram telegram-bot python-telegram-bot py-telegram-bot-api telebot
1个回答
0
投票

这里是使用这些函数的示例

# 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):
    emo = ["\U0001F525", "\U0001F917", "\U0001F60E"]  # or use ["🔥", "🤗", "😎"]
    bot.set_message_reaction(message.chat.id, message.id, [ReactionTypeEmoji(random.choice(emo))], 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'])
© www.soinside.com 2019 - 2024. All rights reserved.