Telegram Bot API 回复消息返回“错误请求:无法解析回复参数 JSON 对象”

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

我正在编写应该在频道的讨论线程中回复的机器人。 然而我得到了这样的回应:

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

请求的数据是

{
  "method": "post",
  "payload": {
    "method": "sendMediaGroup",
    "chat_id": <CHAT ID OF THE CHANEL>,
    "media": [ <SOME MEDIA> ],
    "reply_parameters": {
      "message_id": <MESSAGE ID>,
      "chat_id": <CHAT ID OF THE CHANEL>
    },
    "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"
  }
} 

但是第二个出现错误:

{
  "ok": false,
  "error_code": 400,
  "description": "Bad Request: can't parse reply parameters JSON object"
}
telegram telegram-bot
1个回答
0
投票

您不应该在

{}
中使用
fetch
,而应将
URLSearchParams
JSON.stringify
结合使用以用于嵌套对象。

这应该会让你大吃一惊:

const text = 'Hello world!';
const channelId = 123;
const discussionId = 456;
const messageId = 789;

const params = new URLSearchParams({
    text,
    chat_id: discussionId,
    reply_parameters: ({
        message_id: messageId,
        chat_id: channelId
    })
})

fetch(botSendMessageUrl + '?' + params.toString())
    .then(j => j.json())
    .then(k => console.log(k));
© www.soinside.com 2019 - 2024. All rights reserved.