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-05-31 00:02:20 +0900
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
5b66048e5a9038cb96c7457145f8c2281699bcfc
5b66048e
1 parent
042b6107
Separate translation code into a function
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
25 additions
and
15 deletions
app.js
app.js
View file @
5b66048
...
...
@@ -31,22 +31,17 @@ bot.onText(/\/echo (.+)/, (msg, match) => {
bot
.
sendMessage
(
chatId
,
resp
);
});
// [Any normal message which is not a command (not starting with '/')]
bot
.
onText
(
/
(?!\/)(
.+
)
/
,
(
msg
,
match
)
=>
{
const
chatId
=
msg
.
chat
.
id
;
const
chatType
=
msg
.
chat
.
type
;
const
received_msg
=
match
[
1
];
// Ignore if we are not on a private chat,
// since direct translation is to be used only on private chats.
if
(
chatType
!=
'private'
)
{
return
;
}
/**
* Translate given message and send it to the specified chatroom.
*
* @param {*} message Message to translate
* @param {*} chatId Id of the chatroom to send translated message to
*/
function
translate
(
message
,
chatId
)
{
// Language detection options
var
lang_detect_options
=
{
url
:
languagedetect_api_url
,
form
:
{
'query'
:
received_msg
},
form
:
{
'query'
:
message
},
headers
:
{
'X-Naver-Client-Id'
:
papago_client_id
,
'X-Naver-Client-Secret'
:
papago_client_secret
...
...
@@ -77,7 +72,7 @@ bot.onText(/(?!\/)(.+)/, (msg, match) => {
form
:
{
'source'
:
source
,
// Before translation
'target'
:
target
,
// After translation
'text'
:
received_msg
// Message to translate
'text'
:
message
// Message to translate
},
headers
:
{
'X-Naver-Client-Id'
:
papago_client_id
,
...
...
@@ -93,7 +88,7 @@ bot.onText(/(?!\/)(.+)/, (msg, match) => {
result
.
text
=
objBody
.
message
.
result
.
translatedText
;
// Send translated message
console
.
log
(
'Before: '
+
received_msg
);
console
.
log
(
'Before: '
+
message
);
console
.
log
(
'After: '
+
result
.
text
);
bot
.
sendMessage
(
chatId
,
result
.
text
);
}
...
...
@@ -106,4 +101,19 @@ bot.onText(/(?!\/)(.+)/, (msg, match) => {
}
}
});
}
// [Any normal message which is not a command (not starting with '/')]
bot
.
onText
(
/
(?!\/)(
.+
)
/
,
(
msg
,
match
)
=>
{
const
chatId
=
msg
.
chat
.
id
;
const
chatType
=
msg
.
chat
.
type
;
const
received_msg
=
match
[
1
];
// Ignore if we are not on a private chat,
// since direct translation is to be used only on private chats.
if
(
chatType
!=
'private'
)
{
return
;
}
translate
(
received_msg
,
chatId
);
});
...
...
Please
register
or
login
to post a comment