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-21 16:47:46 +0000
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
89841a4396b1e9fc2c1b822f847d77bcd41efdf3
89841a43
1 parent
d1b96f51
modified
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
105 additions
and
186 deletions
app.js
package-lock.json
package.json
app.js
View file @
89841a4
var
express
=
require
(
'express'
);
const
request
=
require
(
'request'
);
const
TARGET_URL
=
'https://api.line.me/v2/bot/message/reply'
const
express
=
require
(
'express'
);
const
multer
=
require
(
'multer'
);
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
credentials
=
require
(
'/home/ubuntu/termproject/focused-clock-277615-3a427d503822.json'
);
const
client
=
new
vision
.
ImageAnnotatorClient
(
{
credentials
}
);
const
{
Storage
}
=
require
(
'@google-cloud/storage'
);
//google cloud translate api storage api moudle
const
projectId
=
'focused-clock-277615'
;
const
keyFilename
=
'/home/ubuntu/termproject/focused-clock-277615-3a427d503822.json'
;
const
storage
=
new
Storage
({
projectId
,
keyFilename
});
var
upload
=
multer
({
storage
:
_storage
});
var
fs
=
require
(
'fs'
);
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
fs
=
require
(
'fs'
);
const
path
=
require
(
'path'
);
const
HTTPS
=
require
(
'https'
);
const
domain
=
"www.oss0522test.tk"
const
sslport
=
23023
;
const
bodyParser
=
require
(
'body-parser'
);
const
vision
=
require
(
'@google-cloud/vision'
);
//구글 클라우드 비전 api
const
line
=
require
(
'@line/bot-sdk'
);
//1. 이미지 텍스트 추출 - google cloud vision api 사용
// 전송받은 이미지 불러오기 -> 추출
//2. 추출 후 바로 번역? / 텍스트만 출력?
//3. richmenu 활용하기
/*
var query = "언어를 감지할 문장을 입력하세요.";
app.get('/detectLangs', function (req, res) {
var api_url = 'https://openapi.naver.com/v1/papago/detectLangs';
var request = require('request');
var options = {
url: api_url,
form: {'query': query},
headers: {'X-Naver-Client-Id':PAPAGO_ID, 'X-Naver-Client-Secret': PAPAGO_SECRET}
};
request.post(options, 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);
}
});
});
app.listen(3000, function () {
console.log('http://127.0.0.1:3000/detectLangs app listening on port 3000!');
});
*/
const
client_vision
=
new
vision
.
ImageAnnotatorClient
();
//이미지에서 텍스트 추출
function
vision_image
(
bucketName
,
fileName
){
const
[
result
]
=
await
client_vision
.
textDetection
(
`gs://
${
bucketName
}
/
${
fileName
}
`
);
const
detections
=
result
.
textAnnotations
;
console
.
log
(
'Text:'
);
detections
.
forEach
(
text
=>
console
.
log
(
text
));
}
const
client_line
=
new
line
.
Client
({
channelAccessToken
:
TOKEN
});
client_line
.
setRichMenuImage
(
'<rich_menu_id>'
,
fs
.
createReadStream
(
'./example.png'
))
//richmenu 이미지 설정
client_line
.
createRichMenu
(
richmenu
)
//richmenu 만들기
.
then
((
richMenuId
)
=>
console
.
log
(
richMenuId
))
const
richmenu
=
{
"size"
:
{
"width"
:
2500
,
"height"
:
1686
},
"selected"
:
false
,
"name"
:
"Choose language"
,
"chatBarText"
:
"ko/ja/en/fr"
,
"areas"
:
[
{
"bounds"
:
{
"x"
:
0
,
"y"
:
0
,
"width"
:
2500
,
"height"
:
1686
},
"action"
:
{
"type"
:
"postback"
,
"data"
:
"action=buy&itemid=123"
}
}
]
};
var
request
=
require
(
'request'
);
var
_storage
=
multer
.
diskStorage
({
destination
:
function
(
req
,
file
,
cb
)
{
//path - 파일의 형식에 따라 나눔 if문
cb
(
null
,
'uploads/'
)
},
filename
:
function
(
req
,
file
,
cb
)
{
//filename 중복된 파일이 존재하는지
cb
(
null
,
file
.
originalname
)
}
})
app
.
use
(
bodyParser
.
urlencoded
({
extended
:
false
}));
app
.
locals
.
pretty
=
true
;
app
.
set
(
'views'
,
'./views_file'
);
app
.
set
(
'view engine'
,
'jade'
);
app
.
use
(
'/user'
,
express
.
static
(
'uploads'
));
//파일 업로드
app
.
get
(
'/upload'
,
function
(
req
,
res
){
res
.
render
(
'upload'
);
})
//업로드된 파일 받기
app
.
post
(
'/upload'
,
upload
.
single
(
'userfile'
),
function
(
req
,
res
){
console
.
log
(
req
.
file
);
var
app
=
express
();
app
.
use
(
bodyParser
.
json
());
var
trans_lang
;
app
.
post
(
'/hook'
,
function
(
req
,
res
)
{
ImageRecognition
(
req
.
file
.
originalname
);
//사진에서 문자 인식
//PAPAGOtranslation();
var
eventObj
=
req
.
body
.
events
[
0
];
var
source
=
eventObj
.
source
;
var
message
=
eventObj
.
message
;
res
.
send
(
'Uploaded'
+
req
.
file
.
originalname
);
})
// request log
console
.
log
(
'======================'
,
new
Date
()
,
'======================'
);
console
.
log
(
'[request]'
,
req
.
body
);
console
.
log
(
'[request source] '
,
eventObj
.
source
);
console
.
log
(
'[request message]'
,
eventObj
.
message
);
//문자 인식 함수
async
function
ImageRecognition
(
uploadedfilename
)
{
const
fileName
=
`/home/ubuntu/termproject/uploads/
${
uploadedfilename
}
`
;
const
[
result
]
=
await
client
.
textDetection
(
fileName
);
console
.
log
(
'Text: '
+
result
.
fullTextAnnotation
.
text
);
PAPAGOtranslation
(
result
.
fullTextAnnotation
.
text
);
//번역
//const detections = result.textAnnotations;
//detections.forEach(text => console.log(text));
}
//google cloud storage bucket list 함수
async
function
googlevisiontest
(){
if
(
eventObj
.
message
.
type
==
"text"
){
//입력이 텍스트일 경우
if
(
eventObj
.
message
.
text
==
'영어'
){
trans_lang
=
'en'
;
}
else
if
(
eventObj
.
message
.
text
==
'일본어'
){
trans_lang
=
'ja'
;
}
else
if
(
eventObj
.
message
.
text
==
'프랑스어'
){
trans_lang
=
'fr'
;
}
else
{
trans
(
eventObj
.
replyToken
,
eventObj
.
message
.
text
,
trans_lang
);
}
}
else
if
(
eventObj
.
message
.
type
==
"image"
){
//입력이 이미지일 경우
//이미지 저장? -> google cloud storage이나 웹
vision_iamge
(
bucketName
,
fileName
);
//추출된 텍스트, 번역된 텍스트
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
)
{
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
);
/* //라인에 전송
request.post(
{
url: TARGET_URL,
headers: {
'Authorization': `Bearer ${TOKEN}`
},
json: {
"replyToken":replyToken,
"messages":[
{
"type":"text",
"text":transMessage
}
]
}
},(error, response, body) => {
console.log(body)
}); */
}
res
.
sendStatus
(
200
);
});
function
trans
(
replyToken
,
message
,
language
)
{
request
.
post
(
{
url
:
PAPAGO_URL
,
headers
:
{
'Content-Type'
:
'application/x-www-form-urlencoded; charset=UTF-8'
,
'X-Naver-Client-Id'
:
`
${
PAPAGO_ID
}
`
,
'X-Naver-Client-Secret'
:
`
${
PAPAGO_SECRET
}
`
},
body
:
`source=ko&target=
${
language
}
&text=`
+
message
,
json
:
true
},(
error
,
response
,
body
)
=>
{
if
(
!
error
&&
response
.
statusCode
==
200
)
{
console
.
log
(
body
.
message
);
var
transMessage
=
body
.
message
.
result
.
translatedText
;
request
.
post
(
{
url
:
TARGET_URL
,
headers
:
{
'Authorization'
:
`Bearer
${
TOKEN
}
`
},
json
:
{
"replyToken"
:
replyToken
,
"messages"
:[
{
"type"
:
"text"
,
"text"
:
transMessage
}
]
}
},(
error
,
response
,
body
)
=>
{
console
.
log
(
body
)
});
}
});
}
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
var
server
=
app
.
listen
(
23023
);
\ No newline at end of file
...
...
package-lock.json
View file @
89841a4
This diff is collapsed. Click to expand it.
package.json
View file @
89841a4
...
...
@@ -4,9 +4,9 @@
"description"
:
"photo"
,
"main"
:
"app.js"
,
"dependencies"
:
{
"@google-cloud/storage"
:
"^5.1.
0
"
,
"@google-cloud/storage"
:
"^5.1.
1
"
,
"@google-cloud/translate"
:
"^6.0.0"
,
"@google-cloud/vision"
:
"^2.
0
.0"
,
"@google-cloud/vision"
:
"^2.
1
.0"
,
"axios"
:
"^0.19.2"
,
"body-parser"
:
"^1.19.0"
,
"core-js"
:
"^3.6.5"
,
...
...
@@ -17,7 +17,8 @@
"jade"
:
"^1.11.0"
,
"multer"
:
"^1.4.2"
,
"pug"
:
"^3.0.0"
,
"request"
:
"^2.88.2"
"request"
:
"^2.88.2"
,
"util"
:
"^0.12.3"
},
"repository field"
:
{
"type"
:
"http"
,
...
...
Please
register
or
login
to post a comment