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

如何用Telebot库开发可为用户消息添加反应的Telegram机器人?

使用Telebot实现Telegram消息自动添加反应

当然可以用pyTelegramBotAPI(即Telebot库)实现这个功能,核心是调用库中的send_reaction方法。以下是具体实现方案:

前置准备

先确保你安装了最新版本的pyTelegramBotAPI,旧版本可能不支持消息反应功能:

pip install --upgrade pyTelegramBotAPI

实现场景示例

场景1:机器人发送消息后自动给自己的消息加反应

import telebot

# 替换为你的机器人令牌
BOT_TOKEN = "YOUR_BOT_TOKEN_HERE"
bot = telebot.TeleBot(BOT_TOKEN)

@bot.message_handler(func=lambda msg: True)
def auto_react_to_self_message(msg):
    # 发送消息并获取返回的消息对象
    sent_msg = bot.send_message(msg.chat.id, "已收到你的消息,这是机器人的回复")
    # 为刚发送的消息添加表情反应
    bot.send_reaction(
        chat_id=msg.chat.id,
        message_id=sent_msg.message_id,
        reaction=[{"type": "emoji", "emoji": "👍"}]
    )

bot.polling(none_stop=True)

场景2:自动对用户发送的消息添加反应

import telebot

BOT_TOKEN = "YOUR_BOT_TOKEN_HERE"
bot = telebot.TeleBot(BOT_TOKEN)

@bot.message_handler(func=lambda msg: True)
def auto_react_to_user_message(msg):
    # 直接对用户的消息添加反应
    bot.send_reaction(
        chat_id=msg.chat.id,
        message_id=msg.message_id,
        reaction=[{"type": "emoji", "emoji": "❤️"}]
    )

bot.polling(none_stop=True)

关键注意事项

  • reaction参数必须是列表格式,列表内的字典需指定type为emoji,并传入具体的表情符号(如'😂'、'🙏'等)。
  • 确保机器人在目标聊天中有发送消息和添加反应的权限(普通聊天默认权限足够,超级群需确认机器人权限)。
  • 若之前尝试失败,大概率是使用了不支持send_reaction方法的旧版本库,执行升级命令即可解决。

内容的提问来源于stack exchange,提问作者Hiệp Trần

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.19 14:48:17