Toggle navigation
Toggle navigation
This project
Loading...
Sign in
이유혁
/
OSS_Project_Chatbot
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Graphs
Network
Create a new issue
Commits
Issue Boards
Authored by
이유혁
2019-11-18 08:42:50 +0000
Browse Files
Options
Browse Files
Download
Plain Diff
Commit
71043319bb00e24875106dee925f8517210ae07c
71043319
2 parents
7003c17a
0bc301a1
EC2 merging
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
17 additions
and
7 deletions
app.js
app.js
View file @
7104331
...
...
@@ -22,18 +22,19 @@ const line_channel = config.line_channel;
const
client
=
new
line
.
Client
(
line_channel
);
// ELB health checker
app
.
get
(
'/'
,
(
re
q
,
re
s
)
=>
{
app
.
get
(
'/'
,
(
res
)
=>
{
console
.
log
(
'ELB health check'
);
res
.
writeHead
(
200
,
{
"Content-Type"
:
"text/html"
});
res
.
end
();
});
// register a webhook handler with middleware
// about the middleware, please refer to doc
app
.
post
(
'/webhook'
,
line
.
middleware
(
line_channel
),
(
req
,
res
)
=>
{
// webhook post 요청에 대해 promises를 전체 수행한다.
// 전체 수행은 순차수행이기 때문에 동기처리 필요 => async await 패턴을 사용한다
const
promises
=
req
.
body
.
events
.
map
(
async
(
event
)
=>
{
let
language_type
=
await
language_detector
(
event
);
let
options
=
await
option_maker
(
language_type
,
event
);
let
api_connector
=
await
api_connect
(
event
);
let
options
=
await
option_maker
(
api_connector
,
event
);
let
result
=
await
receive_result
(
options
,
event
);
client
.
replyMessage
(
event
.
replyToken
,
result
);
})
...
...
@@ -46,7 +47,9 @@ app.post('/webhook', line.middleware(line_channel), (req, res) => {
});
});
const
language_detector
=
(
event
)
=>
{
// 이벤트 타입 검사 이후
// language detector api url 및 client id, secret 을 담은 connector를 반환한다.
const
api_connect
=
(
event
)
=>
{
if
(
event
.
type
!==
'message'
||
event
.
message
.
type
!==
'text'
)
{
// ignore non-text-message event
return
reject
(
new
Error
(
'메세지 혹은, 텍스트가 아닙니다.'
));
...
...
@@ -61,9 +64,12 @@ const language_detector = (event) => {
})
}
const
option_maker
=
(
language_type
,
event
)
=>
{
// post 요청으로 api connector를 이용해 language를 분석한 뒤
// source 및 target 설정, 번역 API url을 포함한 options을 반환한다
const
option_maker
=
(
api_connector
,
event
)
=>
{
return
new
Promise
((
resolve
,
reject
)
=>
{
request
.
post
(
language_type
,
(
error
,
response
)
=>
{
request
.
post
(
api_connector
,
(
error
,
response
)
=>
{
console
.
log
(
response
.
statusCode
);
if
(
!
error
&&
response
.
statusCode
==
200
){
let
detect_body
=
JSON
.
parse
(
response
.
body
);
...
...
@@ -85,12 +91,15 @@ const option_maker = (language_type, event) => {
resolve
(
options
);
}
else
{
// 예외처리
reject
(
new
Error
(
"request is failed"
));
}
})
})
}
// options를 받아서 post 요청을 통해 번역을 실행한다.
// response를 JSON parsing 한 뒤 결과 result에 메세지를 담아서 반환한다.
const
receive_result
=
(
options
,
event
)
=>
{
return
new
Promise
((
resolve
,
reject
)
=>
{
var
result
=
{
type
:
'text'
,
text
:
''
};
...
...
@@ -105,6 +114,7 @@ const receive_result = (options, event) => {
resolve
(
result
);
}
else
{
// 예외처리
result
.
text
=
'언어를 감지할 수 없습니다. \n 번역 언어는 한글 또는 영어만 가능합니다.'
;
client
.
replyMessage
(
event
.
replyToken
,
result
).
then
(
resolve
).
catch
(
reject
);
reject
(
new
Error
(
"request is failed"
));
...
...
Please
register
or
login
to post a comment