kykint

Translate images inside replies and captions alongside images

Showing 1 changed file with 43 additions and 1 deletions
......@@ -215,14 +215,45 @@ bot.onText(/^\/(t|translate)($| ((.|\n)+))/, (msg, match) => {
// Whether a message has been given after '/t' command
const msgExists = received_msg != undefined;
// Translate the reply's target message
if (isReply) {
// Translate the reply's target message
const replyMsg = msg.reply_to_message.text;
translate(user, replyMsg).then(function (result) {
bot.sendMessage(chatId, result);
}).catch(function (error) {
console.log(error);
});
// If the reply contains image, translate it as well
const photoExists = msg.reply_to_message.photo != undefined;
if (photoExists) {
// Choose largest image possible
const photo = msg.reply_to_message.photo[msg.reply_to_message.photo.length - 1];
const photoId = photo.file_id;
const caption = msg.reply_to_message.caption;
// Detect text from given image
detectText(photoId).then(function (text) {
// Translate the result
translate(user, text).then(function (result) {
// Send recognized text to user
bot.sendMessage(chatId, result);
});
}).catch(function (error) {
// Text detection failed
console.log('Text detection error: ', error);
});
// Translate caption if exists
const captionExists = caption != undefined;
if (captionExists) {
translate(user, caption).then(function (result) {
bot.sendMessage(chatId, result);
}).catch(function (error) {
console.log(error);
});
}
}
}
// Translate the message after '/t' if exists
......@@ -243,6 +274,7 @@ bot.on('photo', (msg) => {
const chatId = msg.chat.id;
const photo = msg.photo[msg.photo.length - 1]; // Choose largest image possible
const photoId = photo.file_id;
const caption = msg.caption;
// Detect text from given image
detectText(photoId).then(function (text) {
......@@ -255,6 +287,16 @@ bot.on('photo', (msg) => {
// Text detection failed
console.log('Text detection error: ', error);
});
// Translate caption if exists
const captionExists = caption != undefined;
if (captionExists) {
translate(user, caption).then(function (result) {
bot.sendMessage(chatId, result);
}).catch(function (error) {
console.log(error);
});
}
});
// /l(anguage)
......