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) => { | ... | @@ -215,14 +215,45 @@ bot.onText(/^\/(t|translate)($| ((.|\n)+))/, (msg, match) => { |
215 | // Whether a message has been given after '/t' command | 215 | // Whether a message has been given after '/t' command |
216 | const msgExists = received_msg != undefined; | 216 | const msgExists = received_msg != undefined; |
217 | 217 | ||
218 | - // Translate the reply's target message | ||
219 | if (isReply) { | 218 | if (isReply) { |
219 | + // Translate the reply's target message | ||
220 | const replyMsg = msg.reply_to_message.text; | 220 | const replyMsg = msg.reply_to_message.text; |
221 | translate(user, replyMsg).then(function (result) { | 221 | translate(user, replyMsg).then(function (result) { |
222 | bot.sendMessage(chatId, result); | 222 | bot.sendMessage(chatId, result); |
223 | }).catch(function (error) { | 223 | }).catch(function (error) { |
224 | console.log(error); | 224 | console.log(error); |
225 | }); | 225 | }); |
226 | + | ||
227 | + // If the reply contains image, translate it as well | ||
228 | + const photoExists = msg.reply_to_message.photo != undefined; | ||
229 | + if (photoExists) { | ||
230 | + // Choose largest image possible | ||
231 | + const photo = msg.reply_to_message.photo[msg.reply_to_message.photo.length - 1]; | ||
232 | + const photoId = photo.file_id; | ||
233 | + const caption = msg.reply_to_message.caption; | ||
234 | + | ||
235 | + // Detect text from given image | ||
236 | + detectText(photoId).then(function (text) { | ||
237 | + // Translate the result | ||
238 | + translate(user, text).then(function (result) { | ||
239 | + // Send recognized text to user | ||
240 | + bot.sendMessage(chatId, result); | ||
241 | + }); | ||
242 | + }).catch(function (error) { | ||
243 | + // Text detection failed | ||
244 | + console.log('Text detection error: ', error); | ||
245 | + }); | ||
246 | + | ||
247 | + // Translate caption if exists | ||
248 | + const captionExists = caption != undefined; | ||
249 | + if (captionExists) { | ||
250 | + translate(user, caption).then(function (result) { | ||
251 | + bot.sendMessage(chatId, result); | ||
252 | + }).catch(function (error) { | ||
253 | + console.log(error); | ||
254 | + }); | ||
255 | + } | ||
256 | + } | ||
226 | } | 257 | } |
227 | 258 | ||
228 | // Translate the message after '/t' if exists | 259 | // Translate the message after '/t' if exists |
... | @@ -243,6 +274,7 @@ bot.on('photo', (msg) => { | ... | @@ -243,6 +274,7 @@ bot.on('photo', (msg) => { |
243 | const chatId = msg.chat.id; | 274 | const chatId = msg.chat.id; |
244 | const photo = msg.photo[msg.photo.length - 1]; // Choose largest image possible | 275 | const photo = msg.photo[msg.photo.length - 1]; // Choose largest image possible |
245 | const photoId = photo.file_id; | 276 | const photoId = photo.file_id; |
277 | + const caption = msg.caption; | ||
246 | 278 | ||
247 | // Detect text from given image | 279 | // Detect text from given image |
248 | detectText(photoId).then(function (text) { | 280 | detectText(photoId).then(function (text) { |
... | @@ -255,6 +287,16 @@ bot.on('photo', (msg) => { | ... | @@ -255,6 +287,16 @@ bot.on('photo', (msg) => { |
255 | // Text detection failed | 287 | // Text detection failed |
256 | console.log('Text detection error: ', error); | 288 | console.log('Text detection error: ', error); |
257 | }); | 289 | }); |
290 | + | ||
291 | + // Translate caption if exists | ||
292 | + const captionExists = caption != undefined; | ||
293 | + if (captionExists) { | ||
294 | + translate(user, caption).then(function (result) { | ||
295 | + bot.sendMessage(chatId, result); | ||
296 | + }).catch(function (error) { | ||
297 | + console.log(error); | ||
298 | + }); | ||
299 | + } | ||
258 | }); | 300 | }); |
259 | 301 | ||
260 | // /l(anguage) | 302 | // /l(anguage) | ... | ... |
-
Please register or login to post a comment