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

Telegram Bot回复频道讨论线程时reply_parameters解析报错排查

Telegram Bot频道讨论线程回复消息解析错误问题排查

问题场景

开发Telegram Bot时,尝试在频道的讨论线程中回复消息,调用接口返回400错误:

{
"ok": false,
"error_code": 400,
"description": "Bad Request: can't parse reply parameters JSON object"
}

请求数据示例:

{
  "method": "post",
  "payload": {
    "method": "sendMediaGroup",
    "chat_id": <CHANNEL的CHAT ID>,
    "media": [ <媒体内容> ],
    "reply_parameters": {
      "message_id": <消息ID>,
      "chat_id": <CHANNEL的CHAT ID>
    },
    "disable_notification": true
  }
}

测试用JS代码:

const botToken = "the actual token";
const botSendMessageUrl = "https://api.telegram.org/bot" + botToken + "/sendMessage";
const channelId = -1001234567894;
const discussionId = -1001234567890;
var res = await fetch(botSendMessageUrl + `?chat_id=${channelId}&text=message`).then(r=>r.json());
console.log(JSON.stringify(res, null, 2));

var messageId = res.result.message_id;
res = await fetch(botSendMessageUrl + `?chat_id=${discussionId}&text=message&reply_parameters={message_id: ${messageId}, chat_id:${channelId}}`).then(r=>r.json());
console.log(JSON.stringify(res, null, 2));

第一次请求成功返回:

{
  "ok": true,
  "result": {
    "message_id": 85,
    "sender_chat": {
      "id": -1001234567894,
      "title": "title",
      "type": "channel"
    },
    "chat": {
      "id": -1001234567894,
      "title": "title",
      "type": "channel"
    },
    "date": 1705426331,
    "text": "message"
  }
} 

第二次请求触发解析错误,问题出在哪?

问题原因

核心问题是URL参数中的reply_parameters格式不合法:

  1. 拼接的{message_id: ${messageId}, chat_id:${channelId}}不是标准JSON——JSON要求键名必须用双引号包裹,这里的键名未加引号,不符合语法规范。
  2. 直接在URL中传递未编码的JSON字符串,会被URL解析器截断或转义,导致Telegram API无法正确解析该参数。

解决方法

方法1:使用POST请求传递JSON体(推荐)

Telegram API优先推荐用POST请求,把所有参数放在JSON格式的请求体里,避免URL参数的编码问题:

const botToken = "the actual token";
const botApiUrl = "https://api.telegram.org/bot" + botToken;
const channelId = -1001234567894;
const discussionId = -1001234567890;

// 第一步:在频道发送消息
let res = await fetch(`${botApiUrl}/sendMessage`, {
  method: "POST",
  headers: { "Content-Type": "application/json" },
  body: JSON.stringify({
    chat_id: channelId,
    text: "message"
  })
}).then(r => r.json());
console.log(JSON.stringify(res, null, 2));

// 第二步:在讨论线程回复消息
const messageId = res.result.message_id;
res = await fetch(`${botApiUrl}/sendMessage`, {
  method: "POST",
  headers: { "Content-Type": "application/json" },
  body: JSON.stringify({
    chat_id: discussionId,
    text: "message",
    reply_parameters: {
      message_id: messageId,
      chat_id: channelId
    }
  })
}).then(r => r.json());
console.log(JSON.stringify(res, null, 2));

方法2:对GET参数中的JSON进行编码(不推荐但可用)

如果一定要用GET请求,需要先构造合法的JSON字符串,再做URL编码后拼接:

// 构造合法的reply_parameters JSON
const replyParams = JSON.stringify({
  message_id: messageId,
  chat_id: channelId
});
// 对JSON字符串做URL编码
const encodedReplyParams = encodeURIComponent(replyParams);

// 拼接请求URL
res = await fetch(`${botSendMessageUrl}?chat_id=${discussionId}&text=message&reply_parameters=${encodedReplyParams}`).then(r=>r.json());

针对sendMediaGroup接口的适配

如果是调用sendMediaGroup接口,同样遵循上述规则:要么用POST传递完整的JSON payload,要么对reply_parameters做合法JSON编码后再拼入URL。


内容的提问来源于stack exchange,提问作者d-s-t

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.02 03:34:59