node-telegram-bot-api icon indicating copy to clipboard operation
node-telegram-bot-api copied to clipboard

sending back the received photo from user doesn't work...

Open HedayatiH opened this issue 7 years ago • 4 comments

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

})

HedayatiH avatar Dec 16 '17 21:12 HedayatiH

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);
})

sidelux avatar Dec 22 '17 15:12 sidelux

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.

HedayatiH avatar Dec 25 '17 08:12 HedayatiH

Same issue, I've got: Unhandled rejection Error: ETELEGRAM: 400 Bad Request: wrong persistent file id

justfry avatar Feb 13 '18 12:02 justfry

Cant we get file id and use in sendPhoto as second argument ? to send image back ?

u2ru avatar Oct 27 '18 23:10 u2ru