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-29 22:03:22 +0900
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
480fd4a20cf55eed26bde5d1f3d99300bf26a1bf
480fd4a2
1 parent
679d7f52
Port basic features from Linebot
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
83 additions
and
6 deletions
app.js
app.js
View file @
480fd4a
...
...
@@ -4,9 +4,21 @@ const TelegramBot = require('node-telegram-bot-api');
const
token
=
'825631426:AAE9tgw89kOZyLTre8DSDaObFQeVx7q41gw'
;
// Create a bot that uses 'polling' to fetch new updates
const
bot
=
new
TelegramBot
(
token
,
{
polling
:
true
});
const
bot
=
new
TelegramBot
(
token
,
{
polling
:
true
});
// Matches "/echo [whatever]"
var
request
=
require
(
'request'
);
// Translation api url
const
translate_api_url
=
'https://openapi.naver.com/v1/papago/n2mt'
;
// Language detection api url
const
languagedetect_api_url
=
'https://openapi.naver.com/v1/papago/detectLangs'
// Naver papago client id & secret
const
papago_client_id
=
'lA0rGxQllAfrlOkGGNnK'
;
const
papago_client_secret
=
'u3fykDlNb0'
;
// /echo [whatever]
bot
.
onText
(
/
\/
echo
(
.+
)
/
,
(
msg
,
match
)
=>
{
// 'msg' is the received Message from Telegram
// 'match' is the result of executing the regexp above on the text content
...
...
@@ -19,11 +31,76 @@ bot.onText(/\/echo (.+)/, (msg, match) => {
bot
.
sendMessage
(
chatId
,
resp
);
});
// Listen for any kind of message. There are different kinds of
// messages.
// Listen for any kind of message. Translate if it's not a command.
bot
.
on
(
'message'
,
(
msg
)
=>
{
const
chatId
=
msg
.
chat
.
id
;
const
received_msg
=
msg
.
text
;
// Ignore if input is a command
if
(
received_msg
[
0
]
==
'/'
)
{
return
;
}
// Language detection options
var
lang_detect_options
=
{
url
:
languagedetect_api_url
,
form
:
{
'query'
:
received_msg
},
headers
:
{
'X-Naver-Client-Id'
:
papago_client_id
,
'X-Naver-Client-Secret'
:
papago_client_secret
}
};
// Papago language detection
request
.
post
(
lang_detect_options
,
function
(
error
,
response
,
body
)
{
console
.
log
(
response
.
statusCode
);
if
(
!
error
&&
response
.
statusCode
==
200
)
{
var
detect_body
=
JSON
.
parse
(
response
.
body
);
var
source
=
''
;
var
target
=
''
;
var
result
=
{
type
:
'text'
,
text
:
''
};
// Check if detection was successful
console
.
log
(
detect_body
.
langCode
);
// Translate using papago, only Korean and English are supported atm
if
(
detect_body
.
langCode
==
'ko'
||
detect_body
.
langCode
==
'en'
)
{
source
=
detect_body
.
langCode
==
'ko'
?
'ko'
:
'en'
;
target
=
source
==
'ko'
?
'en'
:
'ko'
;
// Papago translation options
var
translate_options
=
{
url
:
translate_api_url
,
// 한국어(source : ko), 영어(target: en), 카톡에서 받는 메시지(text)
form
:
{
'source'
:
source
,
// Translate from source
'target'
:
target
,
// to target
'text'
:
received_msg
// Message to translate
},
headers
:
{
'X-Naver-Client-Id'
:
papago_client_id
,
'X-Naver-Client-Secret'
:
papago_client_secret
}
};
// Send translatation request
request
.
post
(
translate_options
,
function
(
error
,
response
,
body
)
{
// On success
if
(
!
error
&&
response
.
statusCode
==
200
)
{
var
objBody
=
JSON
.
parse
(
response
.
body
);
result
.
text
=
objBody
.
message
.
result
.
translatedText
;
// send a message to the chat acknowledging receipt of their message
bot
.
sendMessage
(
chatId
,
'Received your message'
);
// Send translated message
console
.
log
(
result
.
text
);
bot
.
sendMessage
(
chatId
,
result
.
text
);
}
});
}
// Language not detected
else
{
result
.
text
=
'언어를 감지할 수 없습니다. \n 번역 언어는 한글 또는 영어만 가능합니다.'
;
bot
.
sendMessage
(
chatId
,
result
.
text
);
}
}
});
});
...
...
Please
register
or
login
to post a comment