Merge branch 'master' of http://khuhub.khu.ac.kr/2015104206/OSS-2021-Project2015
Showing
1 changed file
with
207 additions
and
16 deletions
| ... | @@ -8,8 +8,15 @@ const HTTPS = require('https'); | ... | @@ -8,8 +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(); |
| 17 | +var stack = 0; | ||
| 18 | +var meal, country, cold, spicy; | ||
| 19 | + | ||
| 13 | app.use(bodyParser.json()); | 20 | app.use(bodyParser.json()); |
| 14 | app.post('/hook', function (req, res) { | 21 | app.post('/hook', function (req, res) { |
| 15 | 22 | ||
| ... | @@ -17,37 +24,43 @@ app.post('/hook', function (req, res) { | ... | @@ -17,37 +24,43 @@ app.post('/hook', function (req, res) { |
| 17 | var source = eventObj.source; | 24 | var source = eventObj.source; |
| 18 | var message = eventObj.message; | 25 | var message = eventObj.message; |
| 19 | 26 | ||
| 27 | + | ||
| 28 | + | ||
| 20 | /* | 29 | /* |
| 21 | 아침 뭐먹지? | 30 | 아침 뭐먹지? |
| 22 | --> JSON에서 [아침]에 해당하는 meal 정보 가져옴 | ||
| 23 | -> 한식, 중식, 일식, 양식 어떤게 좋으세요?(한,중,일,양,아무거나) | 31 | -> 한식, 중식, 일식, 양식 어떤게 좋으세요?(한,중,일,양,아무거나) |
| 24 | -> 따뜻한게 좋으세요, 차가운게 좋으세요?(따뜻한거, 차가운거, 아무거나) | 32 | -> 따뜻한게 좋으세요, 차가운게 좋으세요?(따뜻한거, 차가운거, 아무거나) |
| 25 | -> 매운게 좋으세요, 안매운게 좋으세요?(매운거, 안매운거, 아무거나) | 33 | -> 매운게 좋으세요, 안매운게 좋으세요?(매운거, 안매운거, 아무거나) |
| 26 | --> 빵, 밥, 면, 떡, 디저트 중에는요? | ||
| 27 | -> 그렇다면...이걸 드셔보세요! (사진 첨부) | 34 | -> 그렇다면...이걸 드셔보세요! (사진 첨부) |
| 28 | 35 | ||
| 29 | JSON 구성요소 | 36 | JSON 구성요소 |
| 30 | name : 음식 이름 | 37 | name : 음식 이름 |
| 31 | meal : 아침/점심/저녁 | 38 | meal : 아침/점심/저녁 |
| 32 | contury : 한식/일식/양식/중식 | 39 | contury : 한식/일식/양식/중식 |
| 33 | -cold : 따뜻/찬 | 40 | +cold : hot/cold |
| 34 | -carb : 빵/밥/면/떡/디저트 | 41 | +spicy : yes/no |
| 35 | image : 해당 음식 이미지 넣을 방법 찾아주세요 | 42 | image : 해당 음식 이미지 넣을 방법 찾아주세요 |
| 36 | 43 | ||
| 37 | -API 어떤거 쓸건지? (교수님 보기 좋게 하나는 써야할거같음) | 44 | +네이버 검색 API |
| 45 | +https://developers.naver.com/docs/search/local/ | ||
| 46 | +검색해서 이미지 띄워주는건 어떨까? | ||
| 38 | 47 | ||
| 39 | 48 | ||
| 40 | */ | 49 | */ |
| 41 | 50 | ||
| 42 | - var morning = ['시리얼', '토스트', ''] | 51 | + |
| 43 | 52 | ||
| 44 | // request log | 53 | // request log |
| 45 | console.log('======================', new Date() ,'======================'); | 54 | console.log('======================', new Date() ,'======================'); |
| 46 | console.log('[request]', req.body); | 55 | console.log('[request]', req.body); |
| 47 | console.log('[request source] ', eventObj.source); | 56 | console.log('[request source] ', eventObj.source); |
| 48 | console.log('[request message]', eventObj.message); | 57 | console.log('[request message]', eventObj.message); |
| 49 | - if(eventObj.message.text == "아침") | 58 | + if(eventObj.message.text.indexOf("아침") !== -1 ||eventObj.message.text.indexOf("점심") !== -1 ||eventObj.message.text.indexOf("저녁") !== -1 ) // "아침" 단어가 포함되어있으면 |
| 50 | { | 59 | { |
| 60 | + if(eventObj.message.text.indexOf("아침") !== -1) { meal = "morning"} | ||
| 61 | + else if(eventObj.message.text.indexOf("점심") !== -1) { meal = "lunch"} | ||
| 62 | + else if(eventObj.message.text.indexOf("저녁") !== -1) { meal = "dinner"} | ||
| 63 | + stack = 1; | ||
| 51 | request.post( | 64 | request.post( |
| 52 | { | 65 | { |
| 53 | url: TARGET_URL, | 66 | url: TARGET_URL, |
| ... | @@ -59,19 +72,173 @@ API 어떤거 쓸건지? (교수님 보기 좋게 하나는 써야할거같음) | ... | @@ -59,19 +72,173 @@ API 어떤거 쓸건지? (교수님 보기 좋게 하나는 써야할거같음) |
| 59 | "messages":[ | 72 | "messages":[ |
| 60 | { | 73 | { |
| 61 | "type":"text", | 74 | "type":"text", |
| 62 | - "text":"Hello, user" | 75 | + "text":"한식, 중식, 일식, 양식 중 어떤게 좋으세요?" |
| 63 | - }, | 76 | + } |
| 77 | + ] | ||
| 78 | + } | ||
| 79 | + },(error, response, body) => { | ||
| 80 | + console.log(body) | ||
| 81 | + } | ||
| 82 | + ); | ||
| 83 | + console.log('[request message]', stack); | ||
| 84 | + } | ||
| 85 | + else if(stack == 1 && (eventObj.message.text.indexOf("한식") !== -1 | ||
| 86 | + || eventObj.message.text.indexOf("중식") !== -1 | ||
| 87 | + || eventObj.message.text.indexOf("일식") !== -1 | ||
| 88 | + || eventObj.message.text.indexOf("양식") !== -1)) | ||
| 89 | + { | ||
| 90 | + { | ||
| 91 | + if(eventObj.message.text.indexOf("한식") !== -1) { country = "한식"} | ||
| 92 | + else if(eventObj.message.text.indexOf("중식") !== -1) {country = "중식"} | ||
| 93 | + else if(eventObj.message.text.indexOf("일식") !== -1) { country = "일식"} | ||
| 94 | + else if(eventObj.message.text.indexOf("양식") !== -1) { country = "양식"} | ||
| 95 | + stack = 2; | ||
| 96 | + request.post( | ||
| 97 | + { | ||
| 98 | + url: TARGET_URL, | ||
| 99 | + headers: { | ||
| 100 | + 'Authorization': `Bearer ${TOKEN}` | ||
| 101 | + }, | ||
| 102 | + json: { | ||
| 103 | + "replyToken":eventObj.replyToken, | ||
| 104 | + "messages":[ | ||
| 105 | + { | ||
| 106 | + "type":"text", | ||
| 107 | + "text":"매운거, 안매운거 어떤게 좋으세요?" | ||
| 108 | + } | ||
| 109 | + ] | ||
| 110 | + } | ||
| 111 | + },(error, response, body) => { | ||
| 112 | + console.log(body) | ||
| 113 | + } | ||
| 114 | + ); | ||
| 115 | + } | ||
| 116 | + } | ||
| 117 | + else if(stack == 2 && (eventObj.message.text.indexOf("매운거") !== -1 | ||
| 118 | + || eventObj.message.text.indexOf("안매운거") !== -1)) | ||
| 119 | + { | ||
| 120 | + { | ||
| 121 | + if(eventObj.message.text.indexOf("매운거") !== -1) { spicy = "yes"} | ||
| 122 | + else if(eventObj.message.text.indexOf("안매운거") !== -1) {spicy = "no"} | ||
| 123 | + | ||
| 124 | + stack = 3; | ||
| 125 | + request.post( | ||
| 126 | + { | ||
| 127 | + url: TARGET_URL, | ||
| 128 | + headers: { | ||
| 129 | + 'Authorization': `Bearer ${TOKEN}` | ||
| 130 | + }, | ||
| 131 | + json: { | ||
| 132 | + "replyToken":eventObj.replyToken, | ||
| 133 | + "messages":[ | ||
| 134 | + { | ||
| 135 | + "type":"text", | ||
| 136 | + "text":"찬거, 뜨거운거 어떤게 나으세요?" | ||
| 137 | + } | ||
| 138 | + ] | ||
| 139 | + } | ||
| 140 | + },(error, response, body) => { | ||
| 141 | + console.log(body) | ||
| 142 | + } | ||
| 143 | + ); | ||
| 144 | + } | ||
| 145 | + } | ||
| 146 | + else if(stack == 3 && (eventObj.message.text.indexOf("찬") !== -1 | ||
| 147 | + || eventObj.message.text.indexOf("뜨거운") !== -1)) | ||
| 148 | + { | ||
| 149 | + { | ||
| 150 | + if(eventObj.message.text.indexOf("찬") !== -1) { cold = "hot"} | ||
| 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 | + { | ||
| 182 | + | ||
| 183 | + request.post( | ||
| 184 | + { | ||
| 185 | + url: TARGET_URL, | ||
| 186 | + headers: { | ||
| 187 | + 'Authorization': `Bearer ${TOKEN}` | ||
| 188 | + }, | ||
| 189 | + json: { | ||
| 190 | + "replyToken":eventObj.replyToken, | ||
| 191 | + "messages":[ | ||
| 192 | + { | ||
| 193 | + "type":"text", | ||
| 194 | + "text":"잠시만 기다려주세요!" // 결과 도출 | ||
| 195 | + } | ||
| 196 | + ] | ||
| 197 | + } | ||
| 198 | + },(error, response, body) => { | ||
| 199 | + console.log(body) | ||
| 200 | + } | ||
| 201 | + ); | ||
| 202 | + console.log('[request message]', meal); | ||
| 203 | + console.log('[request message]', country); | ||
| 204 | + console.log('[request message]', cold); | ||
| 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 | + | ||
| 216 | + } | ||
| 217 | + } | ||
| 218 | + else if(eventObj.message.text.indexOf("처음부터") !== -1) // 초기화 알고리즘 | ||
| 219 | + { | ||
| 220 | + stack = 0; | ||
| 221 | + request.post( | ||
| 222 | + { | ||
| 223 | + url: TARGET_URL, | ||
| 224 | + headers: { | ||
| 225 | + 'Authorization': `Bearer ${TOKEN}` | ||
| 226 | + }, | ||
| 227 | + json: { | ||
| 228 | + "replyToken":eventObj.replyToken, | ||
| 229 | + "messages":[ | ||
| 64 | { | 230 | { |
| 65 | "type":"text", | 231 | "type":"text", |
| 66 | - "text":"드세요" | 232 | + "text":"초기화되었습니다. '아침, 점심, 저녁' 중 하나를 입력해주세요!" |
| 67 | } | 233 | } |
| 68 | ] | 234 | ] |
| 69 | } | 235 | } |
| 70 | },(error, response, body) => { | 236 | },(error, response, body) => { |
| 71 | console.log(body) | 237 | console.log(body) |
| 72 | - }); | 238 | + } |
| 239 | + ); | ||
| 73 | } | 240 | } |
| 74 | - else | 241 | + else // 예외 파트 |
| 75 | { | 242 | { |
| 76 | request.post( | 243 | request.post( |
| 77 | { | 244 | { |
| ... | @@ -84,25 +251,49 @@ API 어떤거 쓸건지? (교수님 보기 좋게 하나는 써야할거같음) | ... | @@ -84,25 +251,49 @@ API 어떤거 쓸건지? (교수님 보기 좋게 하나는 써야할거같음) |
| 84 | "messages":[ | 251 | "messages":[ |
| 85 | { | 252 | { |
| 86 | "type":"text", | 253 | "type":"text", |
| 87 | - "text":"No, THanks" | 254 | + "text":"잘못 입력하셨습니다. 다시 입력해주세요!" |
| 88 | - }, | 255 | + } |
| 256 | + ], | ||
| 257 | + "messages":[ | ||
| 89 | { | 258 | { |
| 90 | "type":"text", | 259 | "type":"text", |
| 91 | - "text":"May I help you?" | 260 | + "text":"처음부터 다시 하고 싶으시면, '처음부터'라고 입력해주세요!" |
| 92 | } | 261 | } |
| 93 | ] | 262 | ] |
| 94 | } | 263 | } |
| 95 | },(error, response, body) => { | 264 | },(error, response, body) => { |
| 96 | console.log(body) | 265 | console.log(body) |
| 97 | - }); | 266 | + } |
| 267 | + ); | ||
| 98 | } | 268 | } |
| 99 | 269 | ||
| 100 | 270 | ||
| 271 | + | ||
| 272 | + | ||
| 101 | 273 | ||
| 102 | 274 | ||
| 103 | res.sendStatus(200); | 275 | res.sendStatus(200); |
| 104 | }); | 276 | }); |
| 105 | 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 | + | ||
| 106 | try { | 297 | try { |
| 107 | const option = { | 298 | const option = { |
| 108 | ca: fs.readFileSync('/etc/letsencrypt/live/' + domain +'/fullchain.pem'), | 299 | ca: fs.readFileSync('/etc/letsencrypt/live/' + domain +'/fullchain.pem'), | ... | ... |
-
Please register or login to post a comment