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

基于node-telegram-bot-api开发Telegram机器人时reply_to_message未定义问题

解决node-telegram-bot-api中reply_to_message始终为undefined的问题

问题根源及修复方案

1. 补全Polling配置的allowedUpdates参数

node-telegram-bot-api默认的polling模式可能未请求包含reply_to_message的完整更新数据。初始化bot时需明确指定allowedUpdates,确保获取完整消息结构:

const bot = new TelegramBot(token, {
  polling: {
    allowedUpdates: ['message']
  }
});

2. 确认回复操作的正确性

必须直接回复图片消息发送/meme:长按目标图片,选择「回复」后输入/meme发送,不要单独发命令再发图片。

3. 打印完整msg对象排查

在代码中打印完整的消息对象,确认reply_to_message是否真的不存在,以此区分是API未返回数据还是逻辑判断问题:

bot.on('message', (msg) => {
  console.log(JSON.stringify(msg, null, 2)); // 输出完整消息结构
  const chatId = msg.chat.id;

  if (msg.text?.toLowerCase() === '/meme') {
    if (msg.reply_to_message?.photo) {
      bot.sendMessage(chatId, 'You have an image');
    } else {
      bot.sendMessage(chatId, '请回复一张图片后发送/meme命令');
    }
  }
});

4. 检查群组机器人权限

如果在群组使用,确保机器人已开启「读取消息」权限:进入群组设置→管理成员→找到机器人,确认权限已启用。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.27 05:42:41