node-telegram-bot-api
node-telegram-bot-api copied to clipboard
sending back the received photo from user doesn't work...
Question
Trying to send back the photo that telegram bot has received from user but it doesn't work at first time when the user sends the same photo for the second time it works fine bot.on('message', (msg) => {
console.log(msg);
var path = "C:/\wamp/\www/\images/\cats\/";
var ext = ".jpg";
var fileId = msg.photo[msg.photo.length-1].file_id;
var filePath = msg.photo[msg.photo.length-1].file_path;
var url = "https://api.telegram.org/file/bot<token>" + filePath;
var imagePath = path + fileId + ext;
bot.sendMessage(msg.chat.id, "File Id:" + fileId + "\nFile Path:" + filePath + "\nUrl:" + url + "\nImage Path:" + imagePath);
//log file info
var fileInfo = bot.getFile(fileId);
console.log(fileInfo);
//Download image that user sends to the bot
var download = require('download-file');
var options = {
directory: "./images/cats/",
filename: fileId+".jpg"
}
download(url, options, function(err){
if (err) throw err
console.log("Download successful!"+url);
})
//sends the file back to the user
bot.sendPhoto(msg.chat.id, path + fileId + ext);
})
Do you receive errors?
Anyway, try to move sendPhoto inside download() function, like this:
download(url, options, function(err){
if (err) throw err
console.log("Download successful!"+url);
bot.sendPhoto(msg.chat.id, path + fileId + ext);
})
Thanks for the reply. I receive this error the same error before the change:
\index.js:156
if (err) throw err
404
Looks like when the user sends a photo for the first time, it's not there when I'm trying to send it back to. But when the user re-sends the photo it doesn't matter after one second or half hour, everything is ok. I also have tried to setTimeout() but still not working.
Same issue, I've got:
Unhandled rejection Error: ETELEGRAM: 400 Bad Request: wrong persistent file id
Cant we get file id and use in sendPhoto as second argument ? to send image back ?