박주영

챗봇에 출력할 내용들

...@@ -11,6 +11,33 @@ var queryParams = '?' + encodeURIComponent('ServiceKey') + '=' +key; ...@@ -11,6 +11,33 @@ var queryParams = '?' + encodeURIComponent('ServiceKey') + '=' +key;
11 queryParams += '&' + encodeURIComponent('pc_kor_nm') + '=' + encodeURIComponent('과.채음료'); 11 queryParams += '&' + encodeURIComponent('pc_kor_nm') + '=' + encodeURIComponent('과.채음료');
12 //queryParams += '&' + encodeURIComponent('pageNo') + '=' + encodeURIComponent('3'); 12 //queryParams += '&' + encodeURIComponent('pageNo') + '=' + encodeURIComponent('3');
13 //queryParams += '&' + encodeURIComponent('numOfRows') + '=' + encodeURIComponent('100'); 13 //queryParams += '&' + encodeURIComponent('numOfRows') + '=' + encodeURIComponent('100');
14 +
15 +var ProductCategoryName = '스위트초콜릿';
16 +
17 +
18 +var first_url = 'http://apis.data.go.kr/1470000/FoodAdtvInfoService/getFoodAdtvInfoList';
19 +var first_key ='ofY2ppOq5kBqT5jYPaGsW%2BEy7OR5a1bf5Z9PHvqNKvwO5DSCaU2x2qCj%2FoXnuB1YVbMTlErkHWSMEsR5b7isrw%3D%3D';
20 +var queryParams1 = '?' + encodeURIComponent('ServiceKey') + '=' +first_key;
21 +//queryParams1 += '&' + encodeURIComponent('prdlst_cd') + '=' + encodeURIComponent('C0118010300000');
22 +queryParams1 += '&' + encodeURIComponent('pc_kor_nm') + '=' + encodeURIComponent(ProductCategoryName);
23 +//queryParams1 += '&' + encodeURIComponent('pageNo') + '=' + encodeURIComponent('213');
24 +//queryParams1 += '&' + encodeURIComponent('numOfRows') + '=' + encodeURIComponent('100');
25 +
26 +var NameOfProduct ='하루견과 요거트S'; // 제품명 넣는 자리
27 +var second_url ='http://openapi.foodsafetykorea.go.kr/api';
28 +var second_key ='f8ce3271a2dc4decb83b';
29 +var filetype ='json';
30 +var startIndex = 1;
31 +var endIndex = 5;
32 +var queryParams2 = '/' + encodeURIComponent(second_key);
33 +queryParams2 += '/' + encodeURIComponent('C002');
34 +queryParams2 += '/' + encodeURIComponent(filetype);
35 +queryParams2 += '/' + encodeURIComponent(startIndex);
36 +queryParams2 += '/' + encodeURIComponent(endIndex);
37 +queryParams2 += '/' + encodeURIComponent('PRDLST_NM') + '=' + encodeURIComponent(NameOfProduct);
38 +//var fullRequest = encodeURIComponent("http://openapi.foodsafetykorea.go.kr/api/f8ce3271a2dc4decb83b/C002/xml/1/5/PRDLST_REPORT_NO=200101260032");
39 +//var fullRequest ='http://openapi.foodsafetykorea.go.kr/api/f8ce3271a2dc4decb83b/C002/json/1/5';
40 +
14 const fs = require('fs'); 41 const fs = require('fs');
15 const path = require('path'); 42 const path = require('path');
16 const HTTPS = require('https'); 43 const HTTPS = require('https');
...@@ -22,13 +49,13 @@ app.use(bodyParser.json()); ...@@ -22,13 +49,13 @@ app.use(bodyParser.json());
22 49
23 50
24 function first(text1){ 51 function first(text1){
25 - if(text1 == '궁금해요') 52 + if(text1 == 'y')
26 return true; 53 return true;
27 else 54 else
28 return false; 55 return false;
29 } 56 }
30 57
31 -function hello(text2){ 58 +function hello(eventObj){
32 request.post({ 59 request.post({
33 url: TARGET_URL, 60 url: TARGET_URL,
34 headers:{ 61 headers:{
...@@ -57,36 +84,6 @@ function hello(text2){ ...@@ -57,36 +84,6 @@ function hello(text2){
57 }); 84 });
58 } 85 }
59 86
60 -function inputfood(eventObj){
61 - request.post({
62 - url: TARGET_URL,
63 - headers: {
64 - 'Authorization': `Bearer ${TOKEN}`
65 - },
66 - json:{
67 - "replyToken" :eventObj.replyToken,
68 - "messages":[
69 - {
70 - "type":"text",
71 - "text": "입력하신 상품은 " + eventObj.replyToken +"입니다."
72 - }
73 - ]
74 - }
75 - }, (error, response,body) =>{
76 - console.log(body)
77 -
78 - });
79 -}
80 -
81 -function findfood(foodtext){
82 - //음식정보가 있으면 return true;
83 - //음식정보가 없으면 return false;
84 -}
85 -
86 -function printinformation(foodtext){
87 - //유해정보 출력하기
88 -
89 -}
90 app.post('/hook', function (req, res) { 87 app.post('/hook', function (req, res) {
91 88
92 var eventObj = req.body.events[0]; 89 var eventObj = req.body.events[0];
...@@ -108,79 +105,171 @@ app.post('/hook', function (req, res) { ...@@ -108,79 +105,171 @@ app.post('/hook', function (req, res) {
108 console.log('Hi',message); 105 console.log('Hi',message);
109 hello(text2); 106 hello(text2);
110 } 107 }
111 - inputfood(eventObj); 108 + getfoodinfo(eventObj.replyToken);
112 - 109 + getfood(eventObj.replyToken);
113 - findfoods = findfood(message.text);
114 - if(findfoods ==true){
115 - console.log('음식을 찾았습니다.');
116 - printinformation(findfoods);
117 - }
118 -
119 110
111 + res.sendStatus(200);
112 +});
120 113
114 +//rawmaterial
115 +// 식품의약품 안전처 품목제조보고(원재료) API 요청 예시입니다
121 116
117 +console.log("[Input msg] ", NameOfProduct);
122 118
119 +function getfoodinfo(replyToken){
120 + request.post(
121 + {
122 + url: TARGET_URL,
123 + headers:{
124 + 'Authorization': `Bearer ${TOKEN}`
125 + },
126 + json:{
127 + "replyToken": replyToken,
128 + "messages":[
129 + {
130 + "type":"text",
131 + "text": "입력하신 상품은 " + replyToken +"입니다."
132 + }
133 + ]
134 + }
135 + },(error, reponse, body) => {
136 + console.log(body)
137 + });
123 138
139 + request({
140 + second_url: second_url + queryParams2,
141 + //url : fullRequest,
142 + method: 'GET'
143 + }, function (error, response, body) {
144 + if(error){
145 + console.log('에러입니다.')
146 + }
147 + else{
148 + if(response.statusCode ==200){
149 + console.log("식품명으로 식품유형과 첨가물 정보를 받아옵니다...");
150 + var result =body;
151 + var resObj = eval("("+result+")");
152 + var ProductCategory = resObj.C002.row[0].PRDLST_DCNM;
153 + // 첨가물정보 API가 입력으로 받을 parameter
124 154
125 - //getfoodinfo(eventObj.replyToken, eventObj.message.text); // eventObj.message.text 로 pc_kor_nm 받기 155 + //console.log(result);
156 + console.log("[처리결과] ",resObj.C002.RESULT.MSG);
157 + console.log("[유형] ", ProductCategory);
158 + console.log("[첨가물] ",resObj.C002.row[0].RAWMTRL_NM )
126 159
160 + }
161 + request.post({
162 + url:TARGET_URL,
163 + headers: {
164 + 'Authorization': `Bearer ${TOKEN}`
165 + },
166 + json:{
167 + "messages":[
168 + {
169 + "type":"text",
170 + "text":"[처리결과] " + resObj.C002.RESULT.MSG
171 + },
172 + {
173 + "type":"text",
174 + "text":"[유형] " + ProductCategory
175 + },
176 + {
177 + "type":"text",
178 + "text":"[첨가물] " + resObj.C002.row[0].RAWMTRL_NM
179 + }
180 + ]
181 + }
182 + }),(error, response, body) =>{
183 + console.log(body)
184 + }
185 + }
127 186
128 - //res.sendStatus(200); 187 + });
129 -}); 188 +}
130 189
131 -/*
132 -function getfoodinfo(replyToken, kor_name) {
133 190
191 +// 처음에 하던거
192 +function getfood(replyToken){
134 request.post( 193 request.post(
135 { 194 {
136 - url: FOODAPI_URL + queryParams, 195 + url: TARGET_URL,
137 - },(error, response, body) => { 196 + headers:{
197 + 'Authorization': `Bearer ${TOKEN}`
198 + },
199 + json:{
200 + "replyToken": replyToken,
201 + "messages":[
202 + {
203 + "type":"text",
204 + "text": "입력하신 상품은 " + replyToken +"입니다."
205 + }
206 + ]
207 + }
208 + },(error, reponse, body) => {
209 + console.log(body)
210 + });
211 +
212 + request({
213 + first_url: first_url + queryParams1,
214 + method: 'GET'
215 + }, function (error, response, body) {
138 if(error){ 216 if(error){
139 console.log('에러입니다.') 217 console.log('에러입니다.')
140 } 218 }
141 else{ 219 else{
142 - if(response.statusCode ==200) 220 + if(response.statusCode ==200){
143 - {
144 var result =body; 221 var result =body;
145 var xmltojson = convert.xml2json(result,{compact:true,spaces:4}); 222 var xmltojson = convert.xml2json(result,{compact:true,spaces:4});
146 - //console.log(xmltojson);
147 var resObj = eval("("+xmltojson+")"); 223 var resObj = eval("("+xmltojson+")");
148 var resultItems = resObj.response.body.items; 224 var resultItems = resObj.response.body.items;
149 - var responseMessage ='[ ' + resultItems.item[0].PC_KOR_NM._text + ' ]\n'; 225 + var resultLeng = Object.keys(resultItems);
150 226
227 + if(Object.keys(resultItems).length != 0){
228 + console.log("valid input : 식품 첨가물 정보를 받아옵니다.");
229 +
230 + var responseMessage ='[ ' + resultItems.item[0].PC_KOR_NM._text + ' ]\n';
231 + //하나의 카테고리에 대한 정보만 받는다고 가정해 반복문 밖으로 뺐습니다.
151 232
152 for(var i=0 ; i < resultItems.item.length; i ++) 233 for(var i=0 ; i < resultItems.item.length; i ++)
153 { 234 {
235 + //var responseMessage ='[ ' + resultItems.item[i].PC_KOR_NM._text + ' ]\n';
154 var addictive = resultItems.item[i].T_KOR_NM._text; 236 var addictive = resultItems.item[i].T_KOR_NM._text;
155 var specVal = resultItems.item[i].SPEC_VAL_SUMUP._text; 237 var specVal = resultItems.item[i].SPEC_VAL_SUMUP._text;
156 - responseMessage += addictive + ' : ' + specVal + '\n'; 238 + var yn = resultItems.item[i].INJRY_YN._text;
239 + responseMessage += addictive + ' : ' + specVal + '['+yn+']'+ '\n';
157 } 240 }
158 - console.log('[responese message]',responseMessage);
159 241
160 - request.post( 242 + }
243 + else
161 { 244 {
245 +
246 + console.log("Invalid Input : 에러 메시지 전송");
247 + var responseMessage = "잘못 된 입력입니다. 라벨의 식품유형을 확인하고 다시 입력해주세요.";
248 +
249 + }
250 + console.log('[responese message]',responseMessage);
251 + request.post({
162 url: TARGET_URL, 252 url: TARGET_URL,
163 headers: { 253 headers: {
164 'Authorization': `Bearer ${TOKEN}` 254 'Authorization': `Bearer ${TOKEN}`
165 }, 255 },
166 - json: { 256 + json:{
167 - "replyToken":replyToken, 257 + "messages": [
168 - "messages":[
169 { 258 {
170 "type":"text", 259 "type":"text",
171 - "text":responseMessage 260 + "text": responseMessage
172 } 261 }
173 ] 262 ]
174 } 263 }
175 - },(error, response, body) => { 264 + }),(error, response,body) =>{
176 console.log(body) 265 console.log(body)
177 - });
178 } 266 }
179 } 267 }
180 - }); 268 + }
181 269
270 + });
182 } 271 }
183 -*/ 272 +
184 try { 273 try {
185 const option = { 274 const option = {
186 ca: fs.readFileSync('/etc/letsencrypt/live/' + domain +'/fullchain.pem'), 275 ca: fs.readFileSync('/etc/letsencrypt/live/' + domain +'/fullchain.pem'),
......