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
0
Wiki
Snippets
Network
Create a new issue
Builds
Commits
Issue Boards
Authored by
sckim
2019-12-03 19:23:03 +0900
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
e9c63e9a5808f16ca34f1164479d9c7be6838ba1
e9c63e9a
1 parent
18819bac
Use OCR API
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
59 additions
and
8 deletions
app.js
app.js
View file @
e9c63e9
...
...
@@ -37,6 +37,11 @@ const config = {
channelSecret
:
process
.
env
.
channelSecret
,
};
// Microsoft Azure - Computer Vision REST API
let
subscriptionKey
=
process
.
env
.
COMPUTER_VISION_SUBSCRIPTION_KEY
let
endpoint
=
process
.
env
.
COMPUTER_VISION_ENDPOINT
if
(
!
subscriptionKey
)
{
throw
new
Error
(
'Set your environment variables for your subscription key and endpoint.'
)
}
var
uriBase
=
endpoint
+
'vision/v2.1/ocr'
// create LINE SDK client
const
client
=
new
line
.
Client
(
config
);
...
...
@@ -63,8 +68,56 @@ app.post('/webhook', line.middleware(config), (req, res) => {
// event handler
function
handleEvent
(
event
)
{
if
(
event
.
type
!==
'message'
||
event
.
message
.
type
!==
'text'
)
{
// ignore non-text-message event
return
Promise
.
resolve
(
null
);
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
)
=>
{
stream
.
on
(
'data'
,
(
chunk
)
=>
{
imageStream
.
write
(
chunk
)
})
stream
.
on
(
'error'
,
(
err
)
=>
{
console
.
log
(
err
)
})
stream
.
on
(
'end'
,
()
=>
{
imageStream
.
end
()
const
imageUrl
=
'https://panguin.ml/image.jpeg'
const
params
=
{
'language'
:
'unk'
,
'detectOrientation'
:
'true'
,
}
const
options
=
{
uri
:
uriBase
,
qs
:
params
,
body
:
'{"url": '
+
'"'
+
imageUrl
+
'"}'
,
headers
:
{
'Content-Type'
:
'application/json'
,
'Ocp-Apim-Subscription-Key'
:
subscriptionKey
}
}
request
.
post
(
options
,
(
error
,
response
,
body
)
=>
{
if
(
error
)
{
console
.
log
(
'Error: '
,
error
)
return
}
var
lines
=
JSON
.
parse
(
body
).
regions
[
0
].
lines
var
detected_text
=
''
for
(
var
i
=
0
;
i
<
lines
.
length
;
i
++
)
{
for
(
var
j
=
0
;
j
<
lines
[
i
].
words
.
length
;
j
++
)
detected_text
+=
lines
[
i
].
words
[
j
].
text
+
' '
detected_text
+=
'\n'
}
console
.
log
(
detected_text
)
var
text_before_translation
=
detected_text
.
split
(
'\n'
).
join
(
''
).
toLowerCase
()
console
.
log
(
text_before_translation
)
})
})
})
})
else
// ignore non-text-message event
return
Promise
.
resolve
(
null
);
}
// 검색 기능
...
...
@@ -148,21 +201,19 @@ function handleEvent(event) {
result
.
text
=
objBody
.
message
.
result
.
translatedText
;
//번역된 문자 audio로 저장
if
(
options
.
form
.
target
==
'ko'
)
{
var
audio_options
=
{
let
audio_options
=
{
'Text'
:
result
.
text
,
'OutputFormat'
:
'mp3'
,
'VoiceId'
:
'
Seoyeon
'
,
'VoiceId'
:
'
Amy
'
,
"LanguageCode"
:
'ko-KR'
};
console
.
log
(
"korean language"
);
}
else
if
(
options
.
form
.
target
==
'en'
)
{
var
audio_options
=
{
let
audio_options
=
{
'Text'
:
result
.
text
,
'OutputFormat'
:
'mp3'
,
'VoiceId'
:
'
Kendra
'
,
'VoiceId'
:
'
Amy
'
,
"LanguageCode"
:
'en-US'
};
console
.
log
(
"english language"
);
}
Polly
.
synthesizeSpeech
(
audio_options
,
(
err
,
data
)
=>
{
console
.
log
(
"check"
);
...
...
Please
register
or
login
to post a comment