【发布时间】:2025-11-30 06:05:01
【问题描述】:
我正在尝试使用 node 和 Telegram Api(在本例中为 sendAudio)将音频文件发送到 Telegram 机器人
const axios = require('axios');
const FormData = require('form-data');
let payload = new FormData();
payload.append('chat_id', 'ID');
payload.append('audio', './audio.mp3');
// OR payload.append('photo', fs.createReadStream(`./audio.jpg`));
axios.post(
'https://api.telegram.org/botMyToken/sendAudio',
payload,
{
headers: {
'accept': 'application/json',
'Content-Type': `multipart/form-data;`
}
})
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
控制台结果是一个大对象:
Error: Request failed with status code 400
at createError (/Users/username/TelegramBot/MyBot/node_modules/axios/lib/core/createError.js:16:15)
at settle (/Users/username/TelegramBot/MyBot/node_modules/axios/lib/core/settle.js:18:12)
at IncomingMessage.handleStreamEnd (/Users/username/TelegramBot/MyBot/node_modules/axios/lib/adapters/http.js:201:11)
at IncomingMessage.emit (events.js:185:15)
at endReadableNT (_stream_readable.js:1101:12)
at process._tickCallback (internal/process/next_tick.js:114:19)
headers:
{ Accept: 'application/json, text/plain, */*',
'Content-Type': 'multipart/form-data;',
accept: 'application/json',
'Accept-Language': 'en-US,en;q=0.8',
'User-Agent': 'axios/0.18.0' },
method: 'post',
url: 'https://api.telegram.org/botMyToken/sendAudio',
data:
FormData {
_overheadLength: 210,
_valueLength: 89,
_valuesToMeasure: [],
writable: false,
readable: true,
dataSize: 0,
maxDataSize: 2097152,
pauseStreams: true,
_released: true,
_streams: [],
_currentStream: null,
_boundary: '--------------------------432591578870565694802709',
_events: {},
_eventsCount: 0 } },
我做错了什么? 我尝试使用简单的表单发送相同的文件,并且 PHP 并且它有效,我不明白这段代码有什么问题。
【问题讨论】:
-
console.log(error)输出什么? -
刚刚更新问题
标签: node.js post upload axios telegram-bot