Search code examples
telegramtelegram-bot

Telegram Bot API reply to message return "Bad Request: can't parse reply parameters JSON object"


I'm writing bot that should reply in discussion thread of a channel. However I get this responce:

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

The data of the request was

{
  "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
  }
}

what is wrong?

EDIT

running the 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));

the first fetch was successful:

{
  "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"
  }
} 

but the second got error:

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

Solution

  • You should not use {} in a fetch like that, Telegram won't be able to parse that as the error shows.

    Rather use something like URLSearchParams in combination with JSON.stringify for the nested object.

    This should get you stared:

    const text = 'Hello world!';
    const channelId = 123;
    const discussionId = 456;
    const messageId = 789;
    
    const params = new URLSearchParams({
        text,
        chat_id: discussionId,
        reply_parameters: JSON.stringify({
            message_id: messageId,
            chat_id: channelId
        })
    })
    
    fetch(botSendMessageUrl + '?' + params.toString())
        .then(j => j.json())
        .then(k => console.log(k));