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

如何用pyTelegramBotAPI统计频道帖子的表情反应数?

解决pyTelegramBotAPI反应处理器无响应的问题

你的代码中message_handler正常工作,但反应相关处理器没触发,主要有以下几个原因和修复方案:

1. 未配置允许接收反应更新

Telegram Bot默认不会主动发送message_reaction和message_reaction_count类型的更新,必须在轮询时明确指定允许接收的更新类型。

2. 异步处理器需配合异步轮询

你定义的反应处理器是异步函数(async def),但原代码使用的bot.polling()是同步轮询方法,无法触发异步处理器,需改用异步轮询bot.infinity_polling()。

3. 确保依赖版本为最新

反应功能是pyTelegramBotAPI的近期更新特性,需确保你使用的是最新版本:

pip install pytelegrambotapi --upgrade

修复后的完整代码

import telebot
from telebot.types import MessageReactionUpdated, MessageReactionCountUpdated

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 count 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.infinity_polling(allowed_updates=["message", "message_reaction", "message_reaction_count"])

额外说明

  • message_reaction_handler会在单个用户添加/移除反应时触发
  • message_reaction_count_handler会在反应总数变化时触发(比如多个用户操作后合并的计数更新)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.15 15:55:04