我正在编写应该在频道的讨论线程中回复的机器人。 然而我得到了这样的回应:
{
"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"
}
您不应该在
{}
中使用 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));