Toggle navigation
Toggle navigation
This project
Loading...
Sign in
kykint
/
TELEGRAMBOT
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Snippets
Network
Create a new issue
Builds
Commits
Issue Boards
Authored by
kykint
2019-06-06 03:33:39 +0900
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
4bc74eaa606bcec1074c428db59426834af7f6d7
4bc74eaa
1 parent
b44a19a2
Translate images inside replies and captions alongside images
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
43 additions
and
1 deletions
app.js
app.js
View file @
4bc74ea
...
...
@@ -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)
...
...
Please
register
or
login
to post a comment