Toggle navigation
Toggle navigation
This project
Loading...
Sign in
term-project
/
project
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
Solhee
2020-12-08 18:09:56 +0900
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
675ef658beb374364a60c08d74d3e0e88083cfa2
675ef658
1 parent
edb45b32
UserIntefaceAndModeChange
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
225 additions
and
184 deletions
express/app.js
express/app.js
View file @
675ef65
...
...
@@ -8,22 +8,14 @@ const TOKEN = 'z5iy5sMU1W4xZAlwvn0/5x4U+4ZsqI0hKO1ZZNFxUGlNzGBjFg2D1u6/Ij5C/Sbkn
var
ProductCategoryName
;
var
first_url
=
'http://apis.data.go.kr/1470000/FoodAdtvInfoService/getFoodAdtvInfoList'
;
var
first_key
=
'ofY2ppOq5kBqT5jYPaGsW%2BEy7OR5a1bf5Z9PHvqNKvwO5DSCaU2x2qCj%2FoXnuB1YVbMTlErkHWSMEsR5b7isrw%3D%3D'
;
/*
var queryParams1 = '?' + encodeURIComponent('ServiceKey') + '=' +first_key;
//queryParams1 += '&' + encodeURIComponent('prdlst_cd') + '=' + encodeURIComponent('C0118010300000');
queryParams1 += '&' + encodeURIComponent('pc_kor_nm') + '=' + encodeURIComponent(ProductCategoryName);
//queryParams1 += '&' + encodeURIComponent('pageNo') + '=' + encodeURIComponent('213');
//queryParams1 += '&' + encodeURIComponent('numOfRows') + '=' + encodeURIComponent('100');
*/
var
NameOfProduct
;
// 제품명 넣는 자리
var
second_url
=
'http://openapi.foodsafetykorea.go.kr/api'
;
var
second_key
=
'f8ce3271a2dc4decb83b'
;
var
filetype
=
'json'
;
var
startIndex
=
1
;
var
endIndex
=
5
;
//var fullRequest = encodeURIComponent("http://openapi.foodsafetykorea.go.kr/api/f8ce3271a2dc4decb83b/C002/xml/1/5/PRDLST_REPORT_NO=200101260032")
;
//var fullRequest ='http://openapi.foodsafetykorea.go.kr/api/f8ce3271a2dc4decb83b/C002/json/1/5';
var
simpleResult
=
true
;
const
fs
=
require
(
'fs'
);
const
path
=
require
(
'path'
);
...
...
@@ -35,15 +27,28 @@ var app = express();
app
.
use
(
bodyParser
.
json
());
function
first
(
text1
){
if
(
text1
==
'y'
){
return
true
;
}
else
return
false
;
function
sendOneLineMessage
(
eventObj
,
line
)
{
request
.
post
({
url
:
TARGET_URL
,
headers
:{
'Authorization'
:
`Bearer
${
TOKEN
}
`
},
json
:{
"replyToken"
:
eventObj
.
replyToken
,
"messages"
:[
{
"type"
:
"text"
,
"text"
:
line
}
]
}
},(
error
,
response
,
body
)
=>
{
console
.
log
(
body
)
});
}
function
hello
(
eventObj
){
function
hello
AndErrorMsg
(
eventObj
){
request
.
post
({
url
:
TARGET_URL
,
headers
:{
...
...
@@ -58,11 +63,65 @@ function hello(eventObj){
},
{
"type"
:
"text"
,
"text"
:
"무엇을 도와드릴까요?"
"text"
:
"정확한 식품명을 기입해주세요.\n안내가 필요하신 경우 '안내'라고 입력해주세요."
}
]
}
},(
error
,
response
,
body
)
=>
{
console
.
log
(
body
)
});
}
function
prdNameNotFound
(
eventObj
){
request
.
post
({
url
:
TARGET_URL
,
headers
:{
'Authorization'
:
`Bearer
${
TOKEN
}
`
},
json
:{
"replyToken"
:
eventObj
.
replyToken
,
"messages"
:[
{
"type"
:
"text"
,
"text"
:
"죄송합니다. \n해당 식품의 정보를 찾을 수 없습니다.\n"
+
"정확한 식품명을 기입해주세요.\n"
+
"사용 안내가 필요하신 경우 '안내'라고 입력해주세요."
},
{
"type"
:
"sticker"
,
"packageId"
:
"11537"
,
"stickerId"
:
"52002770"
}
]
}
},(
error
,
response
,
body
)
=>
{
console
.
log
(
body
)
});
}
function
instruction
(
eventObj
){
request
.
post
({
url
:
TARGET_URL
,
headers
:{
'Authorization'
:
`Bearer
${
TOKEN
}
`
},
json
:{
"replyToken"
:
eventObj
.
replyToken
,
"messages"
:[
{
"type"
:
"text"
,
"text"
:
"식품 유해정보가 궁금하시면 y를 입력해주세요"
"text"
:
"식품유해정보 알리미 사용 안내입니다."
},
{
"type"
:
"text"
,
"text"
:
"사용방법\n"
+
"1. 식품명을 '정확하게' 입력해주세요.\n"
+
"2. 입력하신 식품명이 식약처 데이터베이스에 존재하는 경우,\n"
+
"식품 유형과 첨가물, 첨가물 안전정보에 대해 받아보실 수 있습니다.\n"
+
"3. '모드' 명령어를 통해 결과 간단히 보기/자세히 보기 모드를 전환할 수 있습니다.\n"
+
"4. 결과 간단히 보기 모드에서는 첨가물과 식품유형을 확인하실 수 있습니다.\n"
+
"5. 자세한 결과 보기 모드에서는 위 정보 및 첨가물 안전정보를 확인하실 수 있습니다."
}
]
...
...
@@ -72,207 +131,189 @@ function hello(eventObj){
});
}
app
.
post
(
'/hook'
,
function
(
req
,
res
)
{
var
eventObj
=
req
.
body
.
events
[
0
];
var
source
=
eventObj
.
source
;
var
message
=
eventObj
.
message
;
var
message_text
=
message
.
text
;
//var text1;
var
text2
;
var
findfoods
;
// request log
console
.
log
(
'======================'
,
new
Date
()
,
'======================'
);
console
.
log
(
'[request]'
,
req
.
body
);
console
.
log
(
'[request source] '
,
eventObj
.
source
);
console
.
log
(
'[request message]'
,
eventObj
.
message
);
text
=
first
(
message_text
);
if
(
text
==
true
){
console
.
log
(
'Hi'
,
message
);
hello
(
eventObj
);
if
(
message_text
==
'안내'
){
console
.
log
(
'[안내 출력]'
);
instruction
(
eventObj
);
}
else
if
(
message_text
==
'모드'
)
{
console
.
log
(
"모드를 변경합니다"
)
simpleResult
=
!
simpleResult
;
if
(
simpleResult
)
{
var
msg
=
"결과 간단히 보기 모드 입니다."
;
}
else
{
var
msg
=
"자세한 결과 보기 모드입니다. 첨가물 안전정보를 확인하실 수 있습니다."
}
sendOneLineMessage
(
eventObj
,
msg
);
}
else
{
getfoodinfo
(
eventObj
.
replyToken
);
getfood
(
eventObj
.
replyToken
);
getfoodinfoByPdtName
(
eventObj
,
message_text
);
}
res
.
sendStatus
(
200
);
});
//rawmaterial
// 식품의약품 안전처 품목제조보고(원재료) API 요청 예시입니다
console
.
log
(
"[Input msg] "
,
NameOfProduct
);
function
getfoodinfoByPdtName
(
eventObj
,
prdName
)
{
var
queryParams
=
'/'
+
encodeURIComponent
(
second_key
);
queryParams
+=
'/'
+
encodeURIComponent
(
'C002'
);
queryParams
+=
'/'
+
encodeURIComponent
(
filetype
);
queryParams
+=
'/'
+
encodeURIComponent
(
startIndex
);
queryParams
+=
'/'
+
encodeURIComponent
(
endIndex
);
queryParams
+=
'/'
+
encodeURIComponent
(
'PRDLST_NM'
)
+
'='
+
encodeURIComponent
(
prdName
);
function
getfoodinfo
(
replyToken
){
request
.
post
(
{
url
:
TARGET_URL
,
headers
:{
'Authorization'
:
`Bearer
${
TOKEN
}
`
},
json
:{
"replyToken"
:
replyToken
,
"messages"
:[
{
"type"
:
"text"
,
"text"
:
"입력하신 상품은 "
+
replyToken
+
"입니다."
}
]
request
({
url
:
second_url
+
queryParams
,
method
:
'GET'
},
function
(
error
,
response
,
body
)
{
if
(
error
){
helloAndErrorMsg
(
oventObj
);
console
.
log
(
'에러입니다.'
)
}
},(
error
,
reponse
,
body
)
=>
{
console
.
log
(
body
)
});
else
{
var
result2
=
body
;
var
resObj2
=
eval
(
"("
+
result2
+
")"
);
var
resultCode2
=
resObj2
.
C002
.
RESULT
.
CODE
;
var
queryParams2
=
'/'
+
encodeURIComponent
(
second_key
);
queryParams2
+=
'/'
+
encodeURIComponent
(
'C002'
);
queryParams2
+=
'/'
+
encodeURIComponent
(
filetype
);
queryParams2
+=
'/'
+
encodeURIComponent
(
startIndex
);
queryParams2
+=
'/'
+
encodeURIComponent
(
endIndex
);
queryParams2
+=
'/'
+
encodeURIComponent
(
'PRDLST_NM'
)
+
'='
+
encodeURIComponent
(
replyToken
);
request
({
second_url
:
second_url
+
queryParams2
,
//url : fullRequest,
method
:
'GET'
},
function
(
error
,
response
,
body
)
{
if
(
error
){
console
.
log
(
'에러입니다.'
)
}
else
{
if
(
response
.
statusCode
==
200
){
console
.
log
(
"[입력] : "
,
prdName
);
console
.
log
(
"식품명으로 식품유형과 첨가물 정보를 받아옵니다..."
);
var
result
=
body
;
var
resObj
=
eval
(
"("
+
result
+
")"
);
var
ProductCategory
=
resObj
.
C002
.
row
[
0
].
PRDLST_DCNM
;
// 첨가물정보 API가 입력으로 받을 parameter
//console.log(result);
console
.
log
(
"[처리결과] "
,
resObj
.
C002
.
RESULT
.
MSG
);
console
.
log
(
"[유형] "
,
ProductCategory
);
console
.
log
(
"[첨가물] "
,
resObj
.
C002
.
row
[
0
].
RAWMTRL_NM
)
}
request
.
post
({
url
:
TARGET_URL
,
headers
:
{
'Authorization'
:
`Bearer
${
TOKEN
}
`
},
json
:{
"messages"
:[
{
"type"
:
"text"
,
"text"
:
"[처리결과] "
+
resObj
.
C002
.
RESULT
.
MSG
},
{
"type"
:
"text"
,
"text"
:
"[유형] "
+
ProductCategory
},
{
"type"
:
"text"
,
"text"
:
"[첨가물] "
+
resObj
.
C002
.
row
[
0
].
RAWMTRL_NM
}
]
}
}),(
error
,
response
,
body
)
=>
{
console
.
log
(
body
)
}
}
});
}
if
(
resultCode2
==
"INFO-200"
)
//'유효하지 않은 입력'
{
console
.
log
(
"존재하지 않는 식품명입니다."
);
prdNameNotFound
(
eventObj
);
}
else
if
(
response
.
statusCode
==
200
)
{
// 유효한 입력
var
ProductCategory
=
resObj2
.
C002
.
row
[
0
].
PRDLST_DCNM
;
// 첨가물정보 API가 입력으로 받을 parameter
//console.log(result);
console
.
log
(
"성공적으로 정보를 받았습니다."
)
console
.
log
(
"[처리결과] "
,
resObj2
.
C002
.
RESULT
.
MSG
);
console
.
log
(
"[유형] "
,
ProductCategory
);
console
.
log
(
"[첨가물] "
,
resObj2
.
C002
.
row
[
0
].
RAWMTRL_NM
)
// 처음에 하던거
function
getfood
(
replyToken
){
request
.
post
(
{
url
:
TARGET_URL
,
headers
:{
'Authorization'
:
`Bearer
${
TOKEN
}
`
},
json
:{
"replyToken"
:
replyToken
,
"messages"
:[
{
"type"
:
"text"
,
"text"
:
"입력하신 상품은 "
+
replyToken
+
"입니다."
}
]
}
},(
error
,
reponse
,
body
)
=>
{
console
.
log
(
body
)
});
// 다음 API호출
var
queryParams
=
'?'
+
encodeURIComponent
(
'ServiceKey'
)
+
'='
+
first_key
;
queryParams
+=
'&'
+
encodeURIComponent
(
'pc_kor_nm'
)
+
'='
+
encodeURIComponent
(
ProductCategory
);
var
queryParams1
=
'?'
+
encodeURIComponent
(
'ServiceKey'
)
+
'='
+
first_key
;
//queryParams1 += '&' + encodeURIComponent('prdlst_cd') + '=' + encodeURIComponent('C0118010300000');
queryParams1
+=
'&'
+
encodeURIComponent
(
'pc_kor_nm'
)
+
'='
+
encodeURIComponent
(
replyToken
);
//queryParams1 += '&' + encodeURIComponent('pageNo') + '=' + encodeURIComponent('213');
//queryParams1 += '&' + encodeURIComponent('numOfRows') + '=' + encodeURIComponent('100');
request
({
first_url
:
first_url
+
queryParams1
,
method
:
'GET'
},
function
(
error
,
response
,
body
)
{
if
(
error
){
console
.
log
(
'에러입니다.'
)
}
else
{
if
(
response
.
statusCode
==
200
){
var
result
=
body
;
var
xmltojson
=
convert
.
xml2json
(
result
,{
compact
:
true
,
spaces
:
4
});
var
resObj
=
eval
(
"("
+
xmltojson
+
")"
);
var
resultItems
=
resObj
.
response
.
body
.
items
;
var
resultLeng
=
Object
.
keys
(
resultItems
);
if
(
Object
.
keys
(
resultItems
).
length
!=
0
){
console
.
log
(
"valid input : 식품 첨가물 정보를 받아옵니다."
);
var
responseMessage
=
'[ '
+
resultItems
.
item
[
0
].
PC_KOR_NM
.
_text
+
' ]\n'
;
//하나의 카테고리에 대한 정보만 받는다고 가정해 반복문 밖으로 뺐습니다.
for
(
var
i
=
0
;
i
<
resultItems
.
item
.
length
;
i
++
)
{
//var responseMessage ='[ ' + resultItems.item[i].PC_KOR_NM._text + ' ]\n';
var
addictive
=
resultItems
.
item
[
i
].
T_KOR_NM
.
_text
;
var
specVal
=
resultItems
.
item
[
i
].
SPEC_VAL_SUMUP
.
_text
;
var
yn
=
resultItems
.
item
[
i
].
INJRY_YN
.
_text
;
responseMessage
+=
addictive
+
' : '
+
specVal
+
'['
+
yn
+
']'
+
'\n'
;
}
request
({
url
:
first_url
+
queryParams
,
method
:
'GET'
},
function
(
error
,
response
,
body
)
{
if
(
error
){
console
.
log
(
'에러입니다.'
)
}
else
if
(
response
.
statusCode
==
200
)
{
var
result1
=
body
;
var
xmltojson
=
convert
.
xml2json
(
result1
,{
compact
:
true
,
spaces
:
4
});
var
resObj1
=
eval
(
"("
+
xmltojson
+
")"
);
var
resultItems
=
resObj1
.
response
.
body
.
items
;
if
(
Object
.
keys
(
resultItems
).
length
!=
0
)
{
console
.
log
(
"valid input : 식품 첨가물 정보를 받아옵니다."
);
var
responseMessage
=
'[ '
+
resultItems
.
item
[
0
].
PC_KOR_NM
.
_text
+
' ]\n'
;
//하나의 카테고리에 대한 정보만 받는다고 가정해 반복문 밖으로 뺐습니다.
for
(
var
i
=
0
;
i
<
resultItems
.
item
.
length
;
i
++
)
{
//var responseMessage ='[ ' + resultItems.item[i].PC_KOR_NM._text + ' ]\n';
var
addictive
=
resultItems
.
item
[
i
].
T_KOR_NM
.
_text
;
var
specVal
=
resultItems
.
item
[
i
].
SPEC_VAL_SUMUP
.
_text
;
var
yn
=
resultItems
.
item
[
i
].
INJRY_YN
.
_text
;
responseMessage
+=
addictive
+
' : '
+
specVal
+
'['
+
yn
+
']'
+
'\n'
;
}
}
else
{
var
replyment
=
{
"replyToken"
:
eventObj
.
replyToken
,
"messages"
:[
{
"type"
:
"text"
,
"text"
:
prdName
+
"에 대해 알아볼까요?"
},
{
"type"
:
"text"
,
"text"
:
"[유형]\n"
+
ProductCategory
},
{
"type"
:
"text"
,
"text"
:
"[첨가물]\n"
+
resObj2
.
C002
.
row
[
0
].
RAWMTRL_NM
}
]
}
console
.
log
(
"Invalid Input : 에러 메시지 전송"
);
var
responseMessage
=
"잘못 된 입력입니다. 라벨의 식품유형을 확인하고 다시 입력해주세요."
;
if
(
!
simpleResult
)
{
var
moreInfo
=
{
"type"
:
"text"
,
"text"
:
"더 자세한 첨가물 안전 정보\n"
+
responseMessage
};
replyment
.
messages
.
push
(
moreInfo
);
}
}
console
.
log
(
'[responese message]'
,
responseMessage
);
request
.
post
({
url
:
TARGET_URL
,
headers
:
{
'Authorization'
:
`Bearer
${
TOKEN
}
`
},
json
:{
"messages"
:
[
//메시지 전송
request
.
post
(
{
url
:
TARGET_URL
,
headers
:
{
'Authorization'
:
`Bearer
${
TOKEN
}
`
},
json
:
replyment
},(
error
,
response
,
body
)
=>
{
console
.
log
(
body
)
});
}
else
{
"type"
:
"text"
,
"text"
:
responseMessage
console
.
log
(
"Invalid Input : 에러 메시지 전송"
);
var
responseMessage
=
"잘못 된 입력입니다. 라벨의 식품유형을 확인하고 다시 입력해주세요."
;
}
]
console
.
log
(
'[responese message]'
,
responseMessage
);
}
}),(
error
,
response
,
body
)
=>
{
console
.
log
(
body
)
}
});
}
else
{
console
.
log
(
"입력 이외의 오류가 발생하였습니다."
);
}
}
}
});
});
}
try
{
const
option
=
{
ca
:
fs
.
readFileSync
(
'/etc/letsencrypt/live/'
+
domain
+
'/fullchain.pem'
),
...
...
Please
register
or
login
to post a comment