Skip to content

Commit

Permalink
uploading images support
Browse files Browse the repository at this point in the history
  • Loading branch information
talyguryn committed Jan 4, 2024
1 parent 0990ea3 commit d3d43d8
Showing 1 changed file with 51 additions and 0 deletions.
51 changes: 51 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,3 +114,54 @@ bot.on('sticker', (msg) => {
console.log(error);
}
});

/**
* Process message with a photo or photos
*/
bot.on('photo', (msg) => {
const chatId = msg.chat.id;

try {
if (!fs.existsSync(uploadsDir)) {
fs.mkdirSync(uploadsDir, { recursive: true });
}

// get the last image item from array
const photoItem = msg.photo[msg.photo.length - 1];
const photoFileId = photoItem.file_id;

/**
* Download image file by id
*/
bot.downloadFile(photoFileId, uploadsDir)
.then(async (pathToImage) => {
/**
* Send chat action
*/
bot.sendChatAction(chatId, 'upload_photo');

uploadByBuffer(fs.readFileSync(pathToImage), 'image/png')
.then(({ link, path }) => {
bot.sendMessage(chatId, link);

try { fs.unlinkSync(pathToImage); } catch(e) {}
})
.catch(error => {
/**
* On error we just send a message to user
*/
bot.sendMessage(chatId, 'Send me another image please');
console.log(error);
});
})
.catch(error => {
bot.sendMessage(chatId, 'Something went wrong');
console.log(error);
});

} catch (error) {
bot.sendMessage(chatId, 'Something bad went wrong');
console.log(error);
}
})

0 comments on commit d3d43d8

Please sign in to comment.