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
yangjisu
2019-06-06 12:55:38 +0900
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
b055d11016bf6ac539bf43472d20168d8e2703c9
b055d110
1 parent
c6e6fcbc
코드 정리
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
91 additions
and
90 deletions
postback.js
postback.js
View file @
b055d11
var
express
=
require
(
'express'
);
var
app
=
express
();
const
express
=
require
(
'express'
);
const
app
=
express
();
const
line
=
require
(
'@line/bot-sdk'
);
const
config
=
require
(
'./config'
);
const
bodyParser
=
require
(
'body-parser'
);
app
.
use
(
bodyParser
.
urlencoded
({
extended
:
false
}));
app
.
use
(
bodyParser
.
json
());
//papago api
var
request
=
require
(
'request'
);
const
request
=
require
(
'request'
);
//번역 api_url
var
translate_api_url
=
'https://openapi.naver.com/v1/papago/n2mt'
;
const
translate_api_url
=
'https://openapi.naver.com/v1/papago/n2mt'
;
//언어감지 api_url
var
languagedetect_api_url
=
'https://openapi.naver.com/v1/papago/detectLangs'
;
const
languagedetect_api_url
=
'https://openapi.naver.com/v1/papago/detectLangs'
;
//eng grammar check
var
EnGrammarCheck_api_url
=
'https://api.textgears.com/check.php'
;
const
textgears
=
require
(
'textgears'
);
// create LINE SDK client
const
client
=
new
line
.
Client
(
config
.
line_config
);
...
...
@@ -25,93 +26,93 @@ const client = new line.Client(config.line_config);
// about Express itself: https://expressjs.com/
// register a webhook handler with middleware
// about the middleware, please refer to doc
app
.
post
(
'/webhook'
,
line
.
middleware
(
config
.
line_config
),
(
req
,
res
)
=>
{
Promise
.
all
(
req
.
body
.
events
.
map
(
handleEvent
))
.
then
((
result
)
=>
res
.
json
(
result
))
.
catch
((
err
)
=>
{
console
.
error
(
err
);
res
.
status
(
200
).
end
();
});
app
.
post
(
"/webhook"
,
line
.
middleware
(
config
.
line_config
),
(
req
,
res
)
=>
{
// res.status(200).end();
Promise
.
all
(
req
.
body
.
events
.
map
(
handleEvent
)).
then
(
result
=>
res
.
json
(
result
)
);
});
function
handleEvent
(
event
)
{
if
(
event
.
type
!==
'message'
||
event
.
message
.
type
!==
'text'
)
{
// ignore non-text-message event
return
Promise
.
resolve
(
null
);
}
return
new
Promise
(
function
(
resolve
,
reject
)
{
//언어 감지 option
var
detect_options
=
{
url
:
languagedetect_api_url
,
form
:
{
'query'
:
event
.
message
.
text
},
headers
:
config
.
naver_header
};
//papago 언어 감지
request
.
post
(
detect_options
,
function
(
error
,
response
,
body
){
console
.
log
(
response
.
statusCode
);
if
(
!
error
&&
response
.
statusCode
==
200
){
var
detect_body
=
JSON
.
parse
(
response
.
body
);
var
source
=
''
;
var
target
=
''
;
var
result
=
{
type
:
'text'
,
text
:
''
};
//언어 감지가 제대로 됐는지 확인
console
.
log
(
detect_body
.
langCode
);
app
.
post
(
'/message'
,
function
(
req
,
res
){
var
msg
=
req
.
body
.
content
;
console
.
log
(
'전달받은 메시지: '
+
msg
);
var
sent
=
{};
switch
(
msg
)
{
case
'번역'
:
case
'1'
:
if
(
detect_body
.
langCode
==
'ko'
||
detect_body
.
langCode
==
'en'
){
source
=
detect_body
.
langCode
==
'ko'
?
'ko'
:
'en'
;
target
=
source
==
'ko'
?
'en'
:
'ko'
;
//papago 번역 option
var
options
=
{
url
:
translate_api_url
,
// 한국어(source : ko), 영어(target: en), 카톡에서 받는 메시지(text)
form
:
{
'source'
:
source
,
'target'
:
target
,
'text'
:
event
.
message
.
text
},
headers
:
config
.
naver_header
};
request
.
post
(
options
,
function
(
error
,
response
,
body
){
// Translate API Sucess
if
(
!
error
&&
response
.
statusCode
==
200
){
// JSON
var
objBody
=
JSON
.
parse
(
response
.
body
);
// Message 잘 찍히는지 확인
result
.
text
=
objBody
.
message
.
result
.
translatedText
;
//console.log(result.text);
//번역된 문장 보내기
client
.
replyMessage
(
event
.
replyToken
,
result
).
then
(
resolve
).
catch
(
reject
);
}
});
const
message
=
{
type
:
'text'
,
text
:
'메뉴를 선택해 주세요 \t 1.번역 \t 2. 문법'
}
send
=
{
'message'
:{
'text'
:
result
.
text
}
}
break
;
case
'문법'
:
case
'문법체크'
:
case
'2'
:
break
;
}
function
handleEvent
(
event
)
{
if
(
event
.
type
!==
'message'
||
event
.
message
.
type
!==
'text'
)
{
// ignore non-text-message event
return
Promise
.
resolve
(
null
);
}
return
client
.
replyMessage
(
event
.
replyToken
,
message
);
app
.
post
(
'/message/reply'
,
function
(
req
,
res
){
const
msg
=
req
.
body
;
switch
(
msg
)
{
case
'번역'
:
case
'1'
:
return
new
Promise
(
function
(
resolve
,
reject
)
{
//언어 감지 option
var
detect_options
=
{
url
:
languagedetect_api_url
,
form
:
{
'query'
:
event
.
message
.
text
},
headers
:
config
.
naver_header
};
//papago 언어 감지
request
.
post
(
detect_options
,
function
(
error
,
response
,
body
){
console
.
log
(
response
.
statusCode
);
if
(
!
error
&&
response
.
statusCode
==
200
)
{
var
detect_body
=
JSON
.
parse
(
response
.
body
);
var
source
=
''
;
var
target
=
''
;
var
result
=
{
type
:
'text'
,
text
:
''
};
//언어 감지가 제대로 됐는지 확인
console
.
log
(
detect_body
.
langCode
);
if
(
detect_body
.
langCode
==
'ko'
||
detect_body
.
langCode
==
'en'
)
{
source
=
detect_body
.
langCode
==
'ko'
?
'ko'
:
'en'
;
target
=
source
==
'ko'
?
'en'
:
'ko'
;
//papago 번역 option
var
options
=
{
url
:
translate_api_url
,
// 한국어(source : ko), 영어(target: en), 카톡에서 받는 메시지(text)
form
:
{
'source'
:
source
,
'target'
:
target
,
'text'
:
event
.
message
.
text
},
headers
:
config
.
naver_header
};
request
.
post
(
options
,
function
(
error
,
response
,
body
)
{
// Translate API Sucess
if
(
!
error
&&
response
.
statusCode
==
200
)
{
// JSON
var
objBody
=
JSON
.
parse
(
response
.
body
);
// Message 잘 찍히는지 확인
result
.
text
=
objBody
.
message
.
result
.
translatedText
;
//console.log(result.text);
//번역된 문장 보내기
client
.
replyMessage
(
event
.
replyToken
,
result
).
then
(
resolve
).
catch
(
reject
);
}
});
}
}});
});
break
;
case
'문법'
:
textgears
({
key
:
'9WUGcY6ZayYMphG7'
,
text
:
event
.
message
.
text
,
}).
then
(
res
=>
{
for
(
const
error
of
res
.
errors
)
{
console
.
log
(
'Bad: %s. Better: %s'
,
error
.
bad
,
error
.
better
.
join
(
', '
));
}
});
break
;
}
});
}
});
app
.
listen
(
3000
,
function
()
{
console
.
log
(
'Linebot listening on port 3000!'
);
})
...
...
Please
register
or
login
to post a comment