Toggle navigation
Toggle navigation
This project
Loading...
Sign in
김승찬
/
LINEBOT
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
1
Wiki
Snippets
Network
Create a new issue
Builds
Commits
Issue Boards
Authored by
sckim
2019-12-03 19:47:59 +0900
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
74e47ca04d0089861d89c575dac205262531a190
74e47ca0
1 parent
e9c63e9a
Translation and Sending message After OCR
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
50 additions
and
1 deletions
app.js
app.js
View file @
74e47ca
...
...
@@ -70,7 +70,6 @@ function handleEvent(event) {
if
(
event
.
type
!==
'message'
||
event
.
message
.
type
!==
'text'
)
{
if
(
event
.
message
.
type
===
'image'
)
return
new
Promise
(
function
(
resolve
,
reject
)
{
console
.
log
(
event
.
message
)
const
imageStream
=
fs
.
createWriteStream
(
'public/image.jpeg'
)
client
.
getMessageContent
(
event
.
message
.
id
)
.
then
((
stream
)
=>
{
...
...
@@ -111,6 +110,56 @@ function handleEvent(event) {
console
.
log
(
detected_text
)
var
text_before_translation
=
detected_text
.
split
(
'\n'
).
join
(
''
).
toLowerCase
()
console
.
log
(
text_before_translation
)
//언어 감지 option
var
detect_options
=
{
url
:
languagedetect_api_url
,
form
:
{
'query'
:
text_before_translation
},
headers
:
{
'X-Naver-Client-Id'
:
client_id
,
'X-Naver-Client-Secret'
:
client_secret
}
}
//papago 언어 감지
request
.
post
(
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
:
''
}
//언어 감지가 제대로 됐는지 확인
console
.
log
(
detect_body
.
langCode
)
//번역은 한국어->영어 / 영어->한국어만 지원
if
(
detect_body
.
langCode
==
'ko'
||
detect_body
.
langCode
==
'en'
)
{
source
=
detect_body
.
langCode
==
'ko'
?
'ko'
:
'en'
target
=
source
==
'ko'
?
'en'
:
'ko'
//papago 번역 option
var
options
=
{
url
:
translate_api_url
,
// 한국어(source : ko), 영어(target: en), 카톡에서 받는 메시지(text)
form
:
{
'source'
:
source
,
'target'
:
target
,
'text'
:
text_before_translation
},
headers
:
{
'X-Naver-Client-Id'
:
client_id
,
'X-Naver-Client-Secret'
:
client_secret
}
}
// Naver Post API
request
.
post
(
options
,
function
(
error
,
response
,
body
)
{
// Translate API Sucess
if
(
!
error
&&
response
.
statusCode
==
200
)
{
// JSON
var
objBody
=
JSON
.
parse
(
response
.
body
)
result
.
text
=
'감지된 텍스트 :\n'
+
detected_text
+
'\n번역된 텍스트 :\n'
+
objBody
.
message
.
result
.
translatedText
// Message 잘 찍히는지 확인
console
.
log
(
result
.
text
)
//번역된 문장 보내기
client
.
replyMessage
(
event
.
replyToken
,
result
).
then
(
resolve
).
catch
(
reject
)
}
})
}
// 메시지의 언어가 영어 또는 한국어가 아닐 경우
else
{
result
.
text
=
'언어를 감지할 수 없습니다. \n 번역 언어는 한글 또는 영어만 가능합니다.'
client
.
replyMessage
(
event
.
replyToken
,
result
).
then
(
resolve
).
catch
(
reject
)
}
}
})
})
})
})
...
...
Please
register
or
login
to post a comment