我正在尝试将图像发送到nodejs中的电报机器人。我根据需要创建了图像,但是当我发送图像时,电报会返回错误:
Unhandled rejection Error: ETELEGRAM: 400 Bad Request: IMAGE_PROCESS_FAILED
at BaseError (C:\Users\bakaydin\OneDrive - Amadeus Workplace\Project\Workspace\Other\NodeJs\node_modules\node-telegram-bot-api\src\errors.js:10:5)
at TelegramError (C:\Users\bakaydin\OneDrive - Amadeus Workplace\Project\Workspace\Other\NodeJs\node_modules\node-telegram-bot-api\src\errors.js:62:5)
at request.then.err (C:\Users\bakaydin\OneDrive - Amadeus Workplace\Project\Workspace\Other\NodeJs\node_modules\node-telegram-bot-api\src\telegram.js:280:15)
at tryCatcher (C:\Users\bakaydin\OneDrive - Amadeus Workplace\Project\Workspace\Other\NodeJs\node_modules\bluebird\js\release\util.js:16:23)
at Promise._settlePromiseFromHandler (C:\Users\bakaydin\OneDrive - Amadeus Workplace\Project\Workspace\Other\NodeJs\node_modules\bluebird\js\release\promise.js:547:31)
at Promise._settlePromise (C:\Users\bakaydin\OneDrive - Amadeus Workplace\Project\Workspace\Other\NodeJs\node_modules\bluebird\js\release\promise.js:604:18)
at Promise._settlePromise0 (C:\Users\bakaydin\OneDrive - Amadeus Workplace\Project\Workspace\Other\NodeJs\node_modules\bluebird\js\release\promise.js:649:10)
at Promise._settlePromises (C:\Users\bakaydin\OneDrive - Amadeus Workplace\Project\Workspace\Other\NodeJs\node_modules\bluebird\js\release\promise.js:729:18)
at _drainQueueStep (C:\Users\bakaydin\OneDrive - Amadeus Workplace\Project\Workspace\Other\NodeJs\node_modules\bluebird\js\release\async.js:93:12)
at _drainQueue (C:\Users\bakaydin\OneDrive - Amadeus Workplace\Project\Workspace\Other\NodeJs\node_modules\bluebird\js\release\async.js:86:9)
at Async._drainQueues (C:\Users\bakaydin\OneDrive - Amadeus Workplace\Project\Workspace\Other\NodeJs\node_modules\bluebird\js\release\async.js:102:5)
at Immediate.Async.drainQueues (C:\Users\bakaydin\OneDrive - Amadeus Workplace\Project\Workspace\Other\NodeJs\node_modules\bluebird\js\release\async.js:15:14)
at runCallback (timers.js:672:20)
at tryOnImmediate (timers.js:645:5)
at processImmediate [as _immediateCallback] (timers.js:617:5)
代码:
bot.onText(/\/items/, (msg, match) => {
const chatId = msg.chat.id;
var qr_svg = qr.image('I love QR!', { type: 'png' });
qr_svg.pipe(require('fs').createWriteStream('i_love_qr.png'));
fs.readFile('i_love_qr.png', function (err, data) {
if (err) throw err; // Fail if the file can't be read.
console.log(data)
bot.sendPhoto(chatId, data)
});
bot.sendMessage(chatId, 'test')
});
预先感谢
答案 0 :(得分:0)
qr_svg.pipe(require('fs').createWriteStream('i_love_qr.png'))
是async
操作。
因此,即使在上一行之前,也可能会执行下一行,因此不会将数据写入文件。因此,当发送损坏的数据或没有数据时,您可能会收到错误的请求错误。因此,您的代码应如下所示:
bot.onText(/\/items/, (msg, match) => {
const chatId = msg.chat.id;
var qr_svg = qr.image('I love QR!', { type: 'png' });
var writeStream = fs.createWriteStream('i_love_qr.png')
qr_svg.pipe(writeStream);
writeStream.on('finish', () => {
fs.readFile('i_love_qr.png', function (err, data) {
if (err) throw err; // Fail if the file can't be read.
console.log(data)
bot.sendPhoto(chatId, data)
});
bot.sendMessage(chatId, 'test')
})
});