Toggle navigation
Toggle navigation
This project
Loading...
Sign in
박다솔
/
Language detection translator Line Chat Bot
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Graphs
Issues
1
Network
Create a new issue
Commits
Issue Boards
Authored by
Ubuntu
2020-06-24 07:06:37 +0000
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
cfeca335c9ad361e1e10ed27f5eace49453dde25
cfeca335
1 parent
ae55fca0
final
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
94 additions
and
77 deletions
app.js
app.js
View file @
cfeca33
...
...
@@ -4,8 +4,9 @@ const bodyParser = require('body-parser');
const
session
=
require
(
'express-session'
);
const
util
=
require
(
'util'
);
const
vision
=
require
(
'@google-cloud/vision'
);
//google cloud vision api module
const
translate
=
require
(
'@google-cloud/translate'
).
v3beta1
;
//google cloud translate api module
const
{
Translate
}
=
require
(
'@google-cloud/translate'
).
v2
;
const
credentials
=
require
(
'/home/ubuntu/termproject/focused-clock-277615-3a427d503822.json'
);
const
translate
=
new
Translate
({
credentials
});
const
client
=
new
vision
.
ImageAnnotatorClient
(
{
credentials
}
);
const
{
Storage
}
=
require
(
'@google-cloud/storage'
);
//google cloud translate api storage api moudle
const
projectId
=
'focused-clock-277615'
;
...
...
@@ -24,6 +25,7 @@ const PAPAGO_URL = 'https://openapi.naver.com/v1/papago/n2mt'
const
PAPAGO_ID
=
'W5BknW1dTHXMah9QZ_KK'
const
PAPAGO_SECRET
=
'doQdf3ZMU3'
const
TARGET_URL
=
'https://api.line.me/v2/bot/message/reply'
const
PAPAGO_LANG_URL
=
'https://naveropenapi.apigw.ntruss.com/langs/v1/dect'
var
request
=
require
(
'request'
);
var
_storage
=
multer
.
diskStorage
({
...
...
@@ -50,17 +52,17 @@ app.get('/upload', function(req, res){
res
.
render
(
'upload'
);
})
var
func_result
;
//업로드된 파일 받기
var
transMessage
;
app
.
post
(
'/upload'
,
upload
.
single
(
'userfile'
),
function
(
req
,
res
){
console
.
log
(
req
.
file
);
ImageRecognition
(
req
.
file
.
originalname
);
//사진에서 문자 인식
var
transresult
=
PAPAGOtranslation
(
func_result
);
res
.
send
(
'Uploaded'
+
req
.
file
.
originalname
+
' 번역결과 '
+
transresult
);
res
.
send
(
'Uploaded'
+
req
.
file
.
originalname
+
"번역결과 : "
+
transMessage
);
})
var
langcode2
=
'en'
;
app
.
use
(
bodyParser
.
json
());
app
.
post
(
'/hook'
,
function
(
req
,
res
)
{
...
...
@@ -74,9 +76,62 @@ app.post('/hook', function (req, res) {
console
.
log
(
'[request source] '
,
eventObj
.
source
);
console
.
log
(
'[request message]'
,
eventObj
.
message
);
var
detect
=
detectLangs
(
eventObj
.
message
.
text
);
if
(
IsItCommand
(
eventObj
.
message
.
text
)){
if
(
eventObj
.
message
.
text
==
"!명령어"
){
request
.
post
(
{
url
:
TARGET_URL
,
headers
:
{
'Authorization'
:
`Bearer
${
TOKEN
}
`
},
json
:
{
"replyToken"
:
eventObj
.
replyToken
,
"messages"
:[{
"type"
:
"text"
,
"text"
:
"!명령어\n!언어\n!사진번역"
}]}
},(
error
,
response
,
body
)
=>
{
console
.
log
(
body
)
});
}
else
if
(
eventObj
.
message
.
text
==
"!언어"
){
request
.
post
(
{
url
:
TARGET_URL
,
headers
:
{
'Authorization'
:
`Bearer
${
TOKEN
}
`
},
json
:
{
"replyToken"
:
eventObj
.
replyToken
,
"messages"
:[{
"type"
:
"text"
,
"text"
:
"원하시는 언어를 !영어 와 같은 형태로 입력해주세요."
},
{
"type"
:
"text"
,
"text"
:
"지원 가능 언어 목록\n한국어\n영어\n일본어\n중국어 번체\n중국어 간체\n베트남어\n이탈리아어\n프랑스어\n인도네시아어\n독일어\n태국어\n러시아어\n스페인어"
}]}
},(
error
,
response
,
body
)
=>
{
console
.
log
(
body
)
});
}
else
if
(
eventObj
.
message
.
text
==
"!사진번역"
){
request
.
post
(
{
url
:
TARGET_URL
,
headers
:
{
'Authorization'
:
`Bearer
${
TOKEN
}
`
},
json
:
{
"replyToken"
:
eventObj
.
replyToken
,
"messages"
:[{
"type"
:
"text"
,
"text"
:
"https://www.tplinechatbot.tk:23023/upload"
}]}
},(
error
,
response
,
body
)
=>
{
console
.
log
(
body
)
});
}
else
{
if
(
langList
.
some
(
x
=>
{
return
"!"
+
x
.
name
==
eventObj
.
message
.
text
})){
var
setlangcode
=
langList
.
find
(
x
=>
{
return
"!"
+
x
.
name
==
eventObj
.
message
.
text
});
if
(
setlangcode
.
code
==
'jp'
){
langcode2
=
'ja'
;
}
else
{
langcode2
=
setlangcode
.
code
;
}
request
.
post
(
{
url
:
TARGET_URL
,
headers
:
{
'Authorization'
:
`Bearer
${
TOKEN
}
`
},
json
:
{
"replyToken"
:
eventObj
.
replyToken
,
"messages"
:[{
"type"
:
"text"
,
"text"
:
"언어가 설정되었습니다."
}]}
},(
error
,
response
,
body
)
=>
{
console
.
log
(
body
)
});
PAPAGOtrans_forLine
(
eventObj
.
replyToken
,
eventObj
.
message
.
text
,
detect
);
}
else
{
request
.
post
(
{
url
:
TARGET_URL
,
headers
:
{
'Authorization'
:
`Bearer
${
TOKEN
}
`
},
json
:
{
"replyToken"
:
eventObj
.
replyToken
,
"messages"
:[{
"type"
:
"text"
,
"text"
:
"지원하지 않는 명령어 혹은 언어입니다."
}]}
},(
error
,
response
,
body
)
=>
{
console
.
log
(
body
)
});
}
}
}
else
{
PAPAGOtrans_forLine
(
eventObj
.
replyToken
,
eventObj
.
message
.
text
,
langcode2
);
}
//PAPAGOtrans_forLine(eventObj.replyToken, eventObj.message.text, langcode2);
res
.
sendStatus
(
200
);
...
...
@@ -87,28 +142,10 @@ app.post('/hook', function (req, res) {
async
function
ImageRecognition
(
uploadedfilename
)
{
const
fileName
=
`/home/ubuntu/termproject/uploads/
${
uploadedfilename
}
`
;
const
[
result
]
=
await
client
.
textDetection
(
fileName
);
const
func_result
=
await
result
.
fullTextAnnotation
.
text
;
console
.
log
(
'Text: '
+
result
.
fullTextAnnotation
.
text
);
//transresult = PAPAGOtranslation (result.fullTextAnnotation.text); //번역
//const detections = result.textAnnotations;
//detections.forEach(text => console.log(text));
PAPAGOtranslation
(
result
.
fullTextAnnotation
.
text
);
//번역
}
//google cloud storage bucket list 함수
async
function
googlevisiontest
(){
try
{
const
[
buckets
]
=
await
storage
.
getBuckets
();
console
.
log
(
'Buckets:'
);
buckets
.
forEach
((
bucket
)
=>
{
console
.
log
(
bucket
.
name
);
});
}
catch
(
err
)
{
console
.
error
(
'ERROR:'
,
err
);
}
}
//googlevisiontest();
//papago trans
function
PAPAGOtranslation
(
query
)
{
...
...
@@ -121,37 +158,27 @@ function PAPAGOtranslation (query) {
json
:
true
},
(
error
,
response
,
body
)
=>
{
if
(
!
error
&&
response
.
statusCode
==
200
)
{
var
transMessage
=
body
.
message
.
result
.
translatedText
;
transMessage
=
body
.
message
.
result
.
translatedText
;
console
.
log
(
"번역 결과: "
+
transMessage
);
}
});
return
transMessage
;
}
//detectLangs
function
detectLangs
(
query
){
request
.
post
(
{
url
:
PAPAGO_LANG_URL
,
form
:
{
'query'
:
query
},
headers
:
{
'X-Naver-Client-Id'
:
PAPAGO_ID
,
'X-Naver-Client-Secret'
:
PAPAGO_SECRET
}
},
(
error
,
response
,
body
)
=>
{
if
(
!
error
&&
response
.
statusCode
==
200
)
{
var
result
=
body
.
langCode
;
}
});
return
result
;
}
//line message trans
function
PAPAGOtrans_forLine
(
replyToken
,
query
,
detect
)
{
async
function
PAPAGOtrans_forLine
(
replyToken
,
query
,
lang2
)
{
//언어감지
let
[
detections
]
=
await
translate
.
detect
(
query
);
detections
=
Array
.
isArray
(
detections
)
?
detections
:
[
detections
];
console
.
log
(
'Detections:'
);
console
.
log
(
detections
[
0
].
language
);
langcode1
=
detections
[
0
].
language
;
request
.
post
(
{
url
:
PAPAGO_URL
,
form
:
{
'source'
:
`
${
detect
}
`
,
'target'
:
'en'
,
'text'
:
query
},
form
:
{
'source'
:
`
${
langcode1
}
`
,
'target'
:
`
${
lang2
}
`
,
'text'
:
query
},
headers
:
{
'X-Naver-Client-Id'
:
PAPAGO_ID
,
'X-Naver-Client-Secret'
:
PAPAGO_SECRET
},
body
:
`source=ko&target=en&text=`
+
query
,
json
:
true
...
...
@@ -177,47 +204,37 @@ function PAPAGOtrans_forLine (replyToken, query, detect) {
},(
error
,
response
,
body
)
=>
{
console
.
log
(
body
)
});
}
else
{
request
.
post
(
{
url
:
TARGET_URL
,
headers
:
{
'Authorization'
:
`Bearer
${
TOKEN
}
`
},
json
:
{
"replyToken"
:
replyToken
,
"messages"
:[{
"type"
:
"text"
,
"text"
:
"오류가 발생했습니다.\n1. 번역이 지원되지 않는 언어입니다.\n2.설정된 언어로 번역을 시도했습니다.\n\n언어를 다시 설정해주세요.\n명령어 예시 : !영어"
}]}
},(
error
,
response
,
body
)
=>
{
console
.
log
(
body
)
});
}
});
}
function
IsItCommand
(
message_
){
if
(
message_
[
0
]
==
"!"
){
if
(
message_
.
charAt
(
0
)
==
"!"
){
return
true
;
}
return
false
;
}
function
selectCommand
(
replyToken
,
message_
){
var
result
;
if
(
message_
==
"!명령어"
){
result
=
"명령어 설명"
;
}
else
if
(
message_
==
(
"!이미지"
||
"!사진"
)
){
result
=
"https://www.tplinechatbot.tk:23023/upload.\n\
링크에 접속하여 사진을 첨부해주세요."
;
}
else
if
(
message_
==
"!언어"
){
result
=
"번역가능한 언어 리스트입니다.\n\
원하시는 언어를 '!영어'와 같이 입력해주세요."
;
}
request
.
post
(
{
url
:
TARGET_URL
,
headers
:
{
'Authorization'
:
`Bearer
${
TOKEN
}
`
},
json
:
{
"replyToken"
:
replyToken
,
"messages"
:[
{
"type"
:
"text"
,
"text"
:
result
}
]
}
},(
error
,
response
,
body
)
=>
{
console
.
log
(
body
)
});
}
const
langList
=
new
Array
(
{
code
:
'ko'
,
name
:
'한국어'
},
{
code
:
'en'
,
name
:
'영어'
},
{
code
:
'jp'
,
name
:
'일본어'
},
{
code
:
'zh-CN'
,
name
:
'중국어 간체'
},
{
code
:
'zh-TW'
,
name
:
'중국어 번체'
},
{
code
:
'vi'
,
name
:
'베트남어'
},
{
code
:
'id'
,
name
:
'인도네시아어'
},
{
code
:
'th'
,
name
:
'태국어'
},
{
code
:
'de'
,
name
:
'독일어'
},
{
code
:
'ru'
,
name
:
'러시아어'
},
{
code
:
'es'
,
name
:
'스페인어'
},
{
code
:
'it'
,
name
:
'이탈리아어'
},
{
code
:
'fr'
,
name
:
'프랑스어'
})
//sever
try
{
...
...
Please
register
or
login
to post a comment