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-23 16:19:35 +0000
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
a950ecde56e0bfc6496261baad248d5ef4be9ce4
a950ecde
1 parent
89841a43
modified
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
111 additions
and
8 deletions
app.js
app.js
View file @
a950ecd
...
...
@@ -13,23 +13,30 @@ const keyFilename = '/home/ubuntu/termproject/focused-clock-277615-3a427d503822.
const
storage
=
new
Storage
({
projectId
,
keyFilename
});
var
upload
=
multer
({
storage
:
_storage
});
var
fs
=
require
(
'fs'
);
const
path
=
require
(
'path'
);
const
HTTPS
=
require
(
'https'
);
const
domain
=
"www.tplinechatbot.tk"
const
sslport
=
23023
;
var
app
=
express
();
const
TOKEN
=
'h8xN39Zg0nRmPhRcwCfUt35UhGQvinzOSzl1VSHWPdaKaW4VDDs2bTvhvxnOH1FVzzJ4t1L0ih/uJ1idIKz8SvPin3PM4nLgt6roTaEVw105aIsZqDwlsKPS2ewb2WVbL2TK/BqVEBQ2MmHO2xFRzAdB04t89/1O/w1cDnyilFU='
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'
var
request
=
require
(
'request'
);
var
_storage
=
multer
.
diskStorage
({
destination
:
function
(
req
,
file
,
cb
)
{
//path - 파일의 형식에 따라 나눔 if문
destination
:
function
(
req
,
file
,
cb
)
{
cb
(
null
,
'uploads/'
)
},
filename
:
function
(
req
,
file
,
cb
)
{
//filename 중복된 파일이 존재하는지
filename
:
function
(
req
,
file
,
cb
)
{
cb
(
null
,
file
.
originalname
)
}
})
app
.
use
(
bodyParser
.
urlencoded
({
extended
:
false
}));
app
.
locals
.
pretty
=
true
;
app
.
set
(
'views'
,
'./views_file'
);
...
...
@@ -43,14 +50,15 @@ app.get('/upload', function(req, res){
res
.
render
(
'upload'
);
})
var
func_result
;
//업로드된 파일 받기
app
.
post
(
'/upload'
,
upload
.
single
(
'userfile'
),
function
(
req
,
res
){
console
.
log
(
req
.
file
);
ImageRecognition
(
req
.
file
.
originalname
);
//사진에서 문자 인식
//PAPAGOtranslation(
);
var
transresult
=
PAPAGOtranslation
(
func_result
);
res
.
send
(
'Uploaded'
+
req
.
file
.
originalname
);
res
.
send
(
'Uploaded'
+
req
.
file
.
originalname
+
' 번역결과 '
+
transresult
);
})
...
...
@@ -58,8 +66,9 @@ app.post('/upload', upload.single('userfile'), 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
);
PAPAGOtranslation
(
result
.
fullTextAnnotation
.
text
);
//번역
//transresult =
PAPAGOtranslation (result.fullTextAnnotation.text); //번역
//const detections = result.textAnnotations;
//detections.forEach(text => console.log(text));
}
...
...
@@ -82,6 +91,49 @@ async function googlevisiontest(){
//papago trans
function
PAPAGOtranslation
(
query
)
{
request
.
post
(
{
url
:
PAPAGO_URL
,
form
:
{
'source'
:
'ko'
,
'target'
:
'en'
,
'text'
:
query
},
headers
:
{
'X-Naver-Client-Id'
:
PAPAGO_ID
,
'X-Naver-Client-Secret'
:
PAPAGO_SECRET
},
body
:
`source=ko&target=en&text=`
+
query
,
json
:
true
},
(
error
,
response
,
body
)
=>
{
if
(
!
error
&&
response
.
statusCode
==
200
)
{
var
transMessage
=
body
.
message
.
result
.
translatedText
;
console
.
log
(
"번역 결과: "
+
transMessage
);
}
});
return
transMessage
;
}
/*
function detectLangs(query){
var options = {
url: PAPAGO_LANG_URL,
form: {'query': query},
headers: {'X-Naver-Client-Id':PAPAGO_ID, 'X-Naver-Client-Secret': PAPAGO_SECRET}
};
request.post(
{
url: PAPAGO_LANG_URL,
form: {'query': query},
headers: {'X-Naver-Client-Id':PAPAGO_ID, 'X-Naver-Client-Secret': PAPAGO_SECRET}
}, function (error, response, body) {
if (!error && response.statusCode == 200) {
res.writeHead(200, {'Content-Type': 'text/json;charset=utf-8'});
res.end(body);
} else {
res.status(response.statusCode).end();
console.log('error = ' + response.statusCode);
}
});
}
*/
//line message trans
function
PAPAGOtrans_forLine
(
replyToken
,
query
)
{
request
.
post
(
{
...
...
@@ -94,7 +146,6 @@ function PAPAGOtranslation (query) {
if
(
!
error
&&
response
.
statusCode
==
200
)
{
var
transMessage
=
body
.
message
.
result
.
translatedText
;
console
.
log
(
transMessage
);
/* //라인에 전송
request
.
post
(
{
url
:
TARGET_URL
,
...
...
@@ -112,9 +163,61 @@ function PAPAGOtranslation (query) {
}
},(
error
,
response
,
body
)
=>
{
console
.
log
(
body
)
});
*/
});
}
});
}
var
server
=
app
.
listen
(
23023
);
\ No newline at end of file
function
IsItCommand
(
message_
){
if
(
message_
[
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
)
});
}
//sever
try
{
const
option
=
{
ca
:
fs
.
readFileSync
(
'/etc/letsencrypt/live/'
+
domain
+
'/fullchain.pem'
),
key
:
fs
.
readFileSync
(
path
.
resolve
(
process
.
cwd
(),
'/etc/letsencrypt/live/'
+
domain
+
'/privkey.pem'
),
'utf8'
).
toString
(),
cert
:
fs
.
readFileSync
(
path
.
resolve
(
process
.
cwd
(),
'/etc/letsencrypt/live/'
+
domain
+
'/cert.pem'
),
'utf8'
).
toString
(),
};
HTTPS
.
createServer
(
option
,
app
).
listen
(
sslport
,
()
=>
{
console
.
log
(
`[HTTPS] Server is started on port
${
sslport
}
`
);
});
}
catch
(
error
)
{
console
.
log
(
'[HTTPS] HTTPS 오류가 발생하였습니다. HTTPS 서버는 실행되지 않습니다.'
);
console
.
log
(
error
);
}
\ No newline at end of file
...
...
Please
register
or
login
to post a comment