이종상

Api added

...@@ -8,10 +8,15 @@ const HTTPS = require('https'); ...@@ -8,10 +8,15 @@ const HTTPS = require('https');
8 const domain = "2015104206.oss-2021.tk" 8 const domain = "2015104206.oss-2021.tk"
9 const sslport = 23023; 9 const sslport = 23023;
10 10
11 +const SERACH_URL = 'https://openapi.naver.com/v1/search/local.xml?query=%EC%A3%BC%EC%8B%9D&display=10&start=1&sort=random'
12 +const SEARCH_ID = 'A9UHUOhOXDtai4Rszdjw'
13 +const SEARCH_SECRET = 'ptmB9hKvCw'
14 +
11 const bodyParser = require('body-parser'); 15 const bodyParser = require('body-parser');
12 var app = express(); 16 var app = express();
13 var stack = 0; 17 var stack = 0;
14 var meal, country, cold, spicy; 18 var meal, country, cold, spicy;
19 +
15 app.use(bodyParser.json()); 20 app.use(bodyParser.json());
16 app.post('/hook', function (req, res) { 21 app.post('/hook', function (req, res) {
17 22
...@@ -23,7 +28,6 @@ app.post('/hook', function (req, res) { ...@@ -23,7 +28,6 @@ app.post('/hook', function (req, res) {
23 28
24 /* 29 /*
25 아침 뭐먹지? 30 아침 뭐먹지?
26 --> JSON에서 [아침]에 해당하는 meal 정보 가져옴
27 -> 한식, 중식, 일식, 양식 어떤게 좋으세요?(한,중,일,양,아무거나) 31 -> 한식, 중식, 일식, 양식 어떤게 좋으세요?(한,중,일,양,아무거나)
28 -> 따뜻한게 좋으세요, 차가운게 좋으세요?(따뜻한거, 차가운거, 아무거나) 32 -> 따뜻한게 좋으세요, 차가운게 좋으세요?(따뜻한거, 차가운거, 아무거나)
29 -> 매운게 좋으세요, 안매운게 좋으세요?(매운거, 안매운거, 아무거나) 33 -> 매운게 좋으세요, 안매운게 좋으세요?(매운거, 안매운거, 아무거나)
...@@ -145,8 +149,37 @@ https://developers.naver.com/docs/search/local/ ...@@ -145,8 +149,37 @@ https://developers.naver.com/docs/search/local/
145 { 149 {
146 if(eventObj.message.text.indexOf("찬") !== -1) { cold = "hot"} 150 if(eventObj.message.text.indexOf("찬") !== -1) { cold = "hot"}
147 else if(eventObj.message.text.indexOf("뜨거운") !== -1) {cold = "cold"} 151 else if(eventObj.message.text.indexOf("뜨거운") !== -1) {cold = "cold"}
152 + stack = 4;
153 +
154 + request.post(
155 + {
156 + url: TARGET_URL,
157 + headers: {
158 + 'Authorization': `Bearer ${TOKEN}`
159 + },
160 + json: {
161 + "replyToken":eventObj.replyToken,
162 + "messages":[
163 + {
164 + "type":"text",
165 + "text":"잠시만 기다려주세요!" // 결과 도출
166 + }
167 + ]
168 + }
169 + },(error, response, body) => {
170 + console.log(body)
171 + }
172 + );
173 + console.log('[request message]', meal);
174 + console.log('[request message]', country);
175 + console.log('[request message]', cold);
176 + console.log('[request message]', spicy);
177 + }
178 + }
179 + else if(stack == 4 && (eventObj.message.text.indexOf("결과") !== -1))
180 + {
181 + {
148 182
149 - stack = 3;
150 request.post( 183 request.post(
151 { 184 {
152 url: TARGET_URL, 185 url: TARGET_URL,
...@@ -170,9 +203,19 @@ https://developers.naver.com/docs/search/local/ ...@@ -170,9 +203,19 @@ https://developers.naver.com/docs/search/local/
170 console.log('[request message]', country); 203 console.log('[request message]', country);
171 console.log('[request message]', cold); 204 console.log('[request message]', cold);
172 console.log('[request message]', spicy); 205 console.log('[request message]', spicy);
206 + const option = {
207 + query: '순대',
208 + start: 1,
209 + display: 3,
210 + sort: 'sim',
211 + filter: 'small'
212 + }
213 + ImageSearch(option);
214 +
215 +
173 } 216 }
174 } 217 }
175 - else if(eventObj.message.text.indexOf("처음부터") !== -1) 218 + else if(eventObj.message.text.indexOf("처음부터") !== -1) // 초기화 알고리즘
176 { 219 {
177 stack = 0; 220 stack = 0;
178 request.post( 221 request.post(
...@@ -195,7 +238,8 @@ https://developers.naver.com/docs/search/local/ ...@@ -195,7 +238,8 @@ https://developers.naver.com/docs/search/local/
195 } 238 }
196 ); 239 );
197 } 240 }
198 - else{ 241 + else // 예외 파트
242 + {
199 request.post( 243 request.post(
200 { 244 {
201 url: TARGET_URL, 245 url: TARGET_URL,
...@@ -231,6 +275,25 @@ https://developers.naver.com/docs/search/local/ ...@@ -231,6 +275,25 @@ https://developers.naver.com/docs/search/local/
231 res.sendStatus(200); 275 res.sendStatus(200);
232 }); 276 });
233 277
278 +
279 +function ImageSearch(option) {
280 +
281 + request.get({
282 + uri:'https://openapi.naver.com/v1/search/image',
283 + qs :option,
284 + headers:{
285 + 'X-Naver-Client-Id':SEARCH_ID,
286 + 'X-Naver-Client-Secret':SEARCH_SECRET
287 +
288 + }
289 + }, function(err,res,body){
290 + let json = JSON.parse(body)
291 + console.log(json)
292 + })
293 +}
294 +
295 +
296 +
234 try { 297 try {
235 const option = { 298 const option = {
236 ca: fs.readFileSync('/etc/letsencrypt/live/' + domain +'/fullchain.pem'), 299 ca: fs.readFileSync('/etc/letsencrypt/live/' + domain +'/fullchain.pem'),
......