Add features (Re-recommendation function, Resolving Errors, Handling Exceptions)
Showing
1 changed file
with
208 additions
and
30 deletions
... | @@ -12,40 +12,176 @@ var app = express(); | ... | @@ -12,40 +12,176 @@ var app = express(); |
12 | app.use(bodyParser.json()); | 12 | app.use(bodyParser.json()); |
13 | const config = require('./config') | 13 | const config = require('./config') |
14 | const mongoose = require("mongoose"); | 14 | const mongoose = require("mongoose"); |
15 | -const { range } = require("express/lib/request"); | 15 | +const {range} = require("express/lib/request"); |
16 | -const connect = mongoose.connect(config.url, | 16 | +const connect = mongoose |
17 | - { | 17 | + .connect(config.url, { |
18 | useNewUrlParser: true, | 18 | useNewUrlParser: true, |
19 | - useUnifiedTopology: true, | 19 | + useUnifiedTopology: true |
20 | }) | 20 | }) |
21 | .then(() => console.log("DB : Succesfully Connected")) | 21 | .then(() => console.log("DB : Succesfully Connected")) |
22 | .catch((err) => console.log(err.message)); | 22 | .catch((err) => console.log(err.message)); |
23 | var express = require('express'); | 23 | var express = require('express'); |
24 | -const { Subinfo } = require('./models/model'); | 24 | +const {Subinfo} = require('./DB_collect/models/model'); |
25 | 25 | ||
26 | ID_data_array = [] | 26 | ID_data_array = [] |
27 | 27 | ||
28 | app.post('/hook', function (req, res) { | 28 | app.post('/hook', function (req, res) { |
29 | 29 | ||
30 | - var eventObj = req.body.events[0]; | 30 | + var eventObj = req |
31 | + .body | ||
32 | + .events[0]; | ||
31 | var source = eventObj.source; | 33 | var source = eventObj.source; |
32 | var message = eventObj.message; | 34 | var message = eventObj.message; |
33 | 35 | ||
34 | var user_ID = eventObj.source['userId'] | 36 | var user_ID = eventObj.source['userId'] |
35 | var data = eventObj.message['text'] | 37 | var data = eventObj.message['text'] |
38 | + | ||
39 | + var found = false; | ||
40 | + if (data == '추천') { | ||
41 | + | ||
42 | + for (var i in ID_data_array) { | ||
43 | + if (ID_data_array[i][0] == user_ID) { | ||
44 | + if (ID_data_array[i].length == 5) { | ||
45 | + | ||
46 | + var campusinfo = ID_data_array[i][1] | ||
47 | + var collegeinfo = ID_data_array[i][2] | ||
48 | + var departmentinfo = ID_data_array[i][3] | ||
49 | + var gradeinfo = ID_data_array[i][4] | ||
50 | + | ||
51 | + var name = [] | ||
52 | + var time = [] | ||
53 | + var prof = [] | ||
54 | + var credit = [] | ||
55 | + | ||
56 | + Subinfo | ||
57 | + .find( | ||
58 | + {campus: `${campusinfo}`, college: `${collegeinfo}`, department: `${departmentinfo}`, grade: `${gradeinfo}`} | ||
59 | + ) | ||
60 | + .then((sub) => { | ||
61 | + if (sub) { | ||
62 | + for (var i in sub) { | ||
63 | + if (sub[i]['subject'].length == 0) { | ||
64 | + continue | ||
65 | + } | ||
66 | + | ||
67 | + if (sub[i]['choice'] == '필수') { | ||
68 | + name.push(sub[i]['name']) | ||
69 | + credit.push(sub[i]['credit']) | ||
70 | + | ||
71 | + var random_num = (Math.floor(Math.random() * (sub[i]['subject'].length))) | ||
72 | + | ||
73 | + if (time.length > 0) { | ||
74 | + while (true) { | ||
75 | + try { | ||
76 | + var tmp = 0 | ||
77 | + var random_num = (Math.floor(Math.random() * (sub[i]['subject'].length))) | ||
78 | + | ||
79 | + for (var j in time) { | ||
80 | + if (time[j].slice(0, 1) == sub[i]['subject'][random_num]['time'].slice(0, 1)) { | ||
81 | + var tmp_time1 = Number(`${time[j].slice(2, 4) + '.' + time[j].slice(5, 7)}`) | ||
82 | + var tmp_time2 = Number(`${time[j].slice(8, 10) + '.' + time[j].slice(11, 13)}`) | ||
83 | + var tmp_time3 = Number( | ||
84 | + `${sub[i]['subject'][random_num]['time'].slice(2, 4) + '.' + sub[i]['subject'][random_num]['time'].slice( | ||
85 | + 5, | ||
86 | + 7 | ||
87 | + )}` | ||
88 | + ) | ||
89 | + | ||
90 | + if (tmp_time3 >= tmp_time1 & tmp_time3 <= tmp_time2) { | ||
91 | + tmp = 1 | ||
92 | + break | ||
93 | + } | ||
94 | + } | ||
95 | + } | ||
96 | + if (tmp == 0) { | ||
97 | + break | ||
98 | + } | ||
99 | + } catch (err) { | ||
100 | + console.error(err); | ||
101 | + } | ||
102 | + } | ||
103 | + } | ||
104 | + time.push(sub[i]['subject'][random_num]['time']) | ||
105 | + prof.push(sub[i]['subject'][random_num]['prof']) | ||
106 | + } | ||
107 | + } | ||
108 | + } else { | ||
109 | + console.log(err) | ||
110 | + } | ||
111 | + var message_array = [] | ||
112 | + | ||
113 | + var result_split = [] | ||
114 | + for (var i in name) { | ||
115 | + result_split[i] = '\n'; | ||
116 | + var splitname = time[i].split('<br>') | ||
117 | + for (var j in splitname) { | ||
118 | + if (j != splitname.length - 1) { | ||
119 | + result_split[i] += (splitname[j] + '\n'); | ||
120 | + } else { | ||
121 | + result_split[i] += splitname[j]; | ||
122 | + } | ||
123 | + } | ||
124 | + var message_json = { | ||
125 | + 'type': 'text', | ||
126 | + 'text': `${ '과목명: ' + name[i] + '\n강의 시간:' + result_split[i] + '\n교수님: ' + prof[i] + '' + | ||
127 | + '\n학점: ' + credit[i]}` | ||
128 | + } | ||
129 | + message_array.push(message_json) | ||
130 | + } | ||
131 | + if (message_array.length == 0) { | ||
132 | + message_array = [ | ||
133 | + { | ||
134 | + 'type': 'text', | ||
135 | + 'text': '입력과정에서 오류가 있습니다. \'시작\'을 입력해주세요.' | ||
136 | + } | ||
137 | + ] | ||
138 | + } else { | ||
139 | + message_array[message_array.length - 1] = { | ||
140 | + 'type': 'text', | ||
141 | + 'text': `${ '과목명: ' + name[name.length - 1] + '\n강의 시간: ' + result_split[result_split.length - 1] + | ||
142 | + '\n교수님: ' + prof[prof.length - 1] + '\n학점: ' + credit[credit.length - 1] + '\n' + | ||
143 | + '\n만약 다른 추천을 원하면 \'추천\'을 입력해주세요.\n처음 입력으로 돌아가고 싶으시면 \'시작\'을 입력해주세요'}` | ||
144 | + } | ||
145 | + } | ||
146 | + | ||
147 | + request.post({ | ||
148 | + url: TARGET_URL, | ||
149 | + headers: { | ||
150 | + 'Authorization': `Bearer ${TOKEN}` | ||
151 | + }, | ||
152 | + | ||
153 | + json: { | ||
154 | + "replyToken": eventObj.replyToken, | ||
155 | + "messages": message_array | ||
156 | + } | ||
157 | + }, (error, response, body) => { | ||
158 | + console.log(body) | ||
159 | + }); | ||
160 | + }) | ||
161 | + } | ||
162 | + break | ||
163 | + } | ||
164 | + } | ||
165 | + } else { | ||
166 | + if (data == '다시') { | ||
167 | + for (var i in ID_data_array) { | ||
168 | + if (ID_data_array[i][0] == user_ID) { | ||
169 | + ID_data_array[i] = [user_ID]; | ||
170 | + } | ||
171 | + } | ||
172 | + } else { | ||
173 | + if (data != '시작') { | ||
36 | if (ID_data_array.length == 0) { | 174 | if (ID_data_array.length == 0) { |
37 | ID_data_array.push([user_ID]) | 175 | ID_data_array.push([user_ID]) |
38 | ID_data_array[0].push(data) | 176 | ID_data_array[0].push(data) |
39 | - } | 177 | + } else { |
40 | - else { | ||
41 | var find = 0 | 178 | var find = 0 |
42 | for (var i in ID_data_array) { | 179 | for (var i in ID_data_array) { |
43 | if (ID_data_array[i][0] == user_ID) { | 180 | if (ID_data_array[i][0] == user_ID) { |
44 | find = 1 | 181 | find = 1 |
45 | if (ID_data_array[i].length < 5) { | 182 | if (ID_data_array[i].length < 5) { |
46 | ID_data_array[i].push(data) | 183 | ID_data_array[i].push(data) |
47 | - } | 184 | + } else { |
48 | - else { | ||
49 | ID_data_array[i] = [user_ID]; | 185 | ID_data_array[i] = [user_ID]; |
50 | ID_data_array[i].push(data); | 186 | ID_data_array[i].push(data); |
51 | } | 187 | } |
... | @@ -63,7 +199,6 @@ app.post('/hook', function (req, res) { | ... | @@ -63,7 +199,6 @@ app.post('/hook', function (req, res) { |
63 | console.log('[request source] ', eventObj.source); | 199 | console.log('[request source] ', eventObj.source); |
64 | console.log('[request message]', eventObj.message); | 200 | console.log('[request message]', eventObj.message); |
65 | 201 | ||
66 | - console.log(ID_data_array) | ||
67 | for (var i in ID_data_array) { | 202 | for (var i in ID_data_array) { |
68 | console.log(ID_data_array[i]) | 203 | console.log(ID_data_array[i]) |
69 | } | 204 | } |
... | @@ -82,9 +217,12 @@ app.post('/hook', function (req, res) { | ... | @@ -82,9 +217,12 @@ app.post('/hook', function (req, res) { |
82 | var prof = [] | 217 | var prof = [] |
83 | var credit = [] | 218 | var credit = [] |
84 | 219 | ||
85 | - Subinfo.find({ campus: `${campusinfo}`, college: `${collegeinfo}`, department: `${departmentinfo}`, grade: `${gradeinfo}` }).then((sub) => { | 220 | + Subinfo |
221 | + .find( | ||
222 | + {campus: `${campusinfo}`, college: `${collegeinfo}`, department: `${departmentinfo}`, grade: `${gradeinfo}`} | ||
223 | + ) | ||
224 | + .then((sub) => { | ||
86 | if (sub) { | 225 | if (sub) { |
87 | - console.log(sub) | ||
88 | for (var i in sub) { | 226 | for (var i in sub) { |
89 | if (sub[i]['subject'].length == 0) { | 227 | if (sub[i]['subject'].length == 0) { |
90 | continue | 228 | continue |
... | @@ -106,7 +244,12 @@ app.post('/hook', function (req, res) { | ... | @@ -106,7 +244,12 @@ app.post('/hook', function (req, res) { |
106 | if (time[j].slice(0, 1) == sub[i]['subject'][random_num]['time'].slice(0, 1)) { | 244 | if (time[j].slice(0, 1) == sub[i]['subject'][random_num]['time'].slice(0, 1)) { |
107 | var tmp_time1 = Number(`${time[j].slice(2, 4) + '.' + time[j].slice(5, 7)}`) | 245 | var tmp_time1 = Number(`${time[j].slice(2, 4) + '.' + time[j].slice(5, 7)}`) |
108 | var tmp_time2 = Number(`${time[j].slice(8, 10) + '.' + time[j].slice(11, 13)}`) | 246 | var tmp_time2 = Number(`${time[j].slice(8, 10) + '.' + time[j].slice(11, 13)}`) |
109 | - var tmp_time3 = Number(`${sub[i]['subject'][random_num]['time'].slice(2, 4) + '.' + sub[i]['subject'][random_num]['time'].slice(5, 7)}`) | 247 | + var tmp_time3 = Number( |
248 | + `${sub[i]['subject'][random_num]['time'].slice(2, 4) + '.' + sub[i]['subject'][random_num]['time'].slice( | ||
249 | + 5, | ||
250 | + 7 | ||
251 | + )}` | ||
252 | + ) | ||
110 | 253 | ||
111 | if (tmp_time3 >= tmp_time1 & tmp_time3 <= tmp_time2) { | 254 | if (tmp_time3 >= tmp_time1 & tmp_time3 <= tmp_time2) { |
112 | tmp = 1 | 255 | tmp = 1 |
... | @@ -117,8 +260,7 @@ app.post('/hook', function (req, res) { | ... | @@ -117,8 +260,7 @@ app.post('/hook', function (req, res) { |
117 | if (tmp == 0) { | 260 | if (tmp == 0) { |
118 | break | 261 | break |
119 | } | 262 | } |
120 | - } | 263 | + } catch (err) { |
121 | - catch (err) { | ||
122 | console.error(err); | 264 | console.error(err); |
123 | } | 265 | } |
124 | } | 266 | } |
... | @@ -127,29 +269,46 @@ app.post('/hook', function (req, res) { | ... | @@ -127,29 +269,46 @@ app.post('/hook', function (req, res) { |
127 | prof.push(sub[i]['subject'][random_num]['prof']) | 269 | prof.push(sub[i]['subject'][random_num]['prof']) |
128 | } | 270 | } |
129 | } | 271 | } |
130 | - } | 272 | + } else { |
131 | - else { | ||
132 | console.log(err) | 273 | console.log(err) |
133 | } | 274 | } |
134 | - | ||
135 | var message_array = [] | 275 | var message_array = [] |
136 | 276 | ||
277 | + var result_split = [] | ||
137 | for (var i in name) { | 278 | for (var i in name) { |
279 | + result_split[i] = '\n'; | ||
280 | + var splitname = time[i].split('<br>') | ||
281 | + for (var j in splitname) { | ||
282 | + if (j != splitname.length - 1) { | ||
283 | + result_split[i] += (splitname[j] + '\n'); | ||
284 | + } else { | ||
285 | + result_split[i] += splitname[j]; | ||
286 | + } | ||
287 | + } | ||
138 | var message_json = { | 288 | var message_json = { |
139 | 'type': 'text', | 289 | 'type': 'text', |
140 | - 'text': `${'과목명: ' + name[i] + '\n' + '강의 시간: ' + time[i] + '\n' + '교수님: ' + prof[i] + '\n' + '학점: ' + credit[i]}` | 290 | + 'text': `${ '과목명: ' + name[i] + '\n강의 시간:' + result_split[i] + '\n교수님: ' + prof[i] + '' + |
291 | + '\n학점: ' + credit[i]}` | ||
141 | } | 292 | } |
142 | message_array.push(message_json) | 293 | message_array.push(message_json) |
143 | } | 294 | } |
144 | if (message_array.length == 0) { | 295 | if (message_array.length == 0) { |
145 | - message_array = [{ | 296 | + message_array = [ |
297 | + { | ||
146 | 'type': 'text', | 298 | 'type': 'text', |
147 | 'text': '입력과정에서 오류가 있습니다. \'시작\'을 입력해주세요.' | 299 | 'text': '입력과정에서 오류가 있습니다. \'시작\'을 입력해주세요.' |
148 | - }] | 300 | + } |
301 | + ] | ||
302 | + } else { | ||
303 | + message_array[message_array.length - 1] = { | ||
304 | + 'type': 'text', | ||
305 | + 'text': `${ '과목명: ' + name[name.length - 1] + '\n강의 시간: ' + result_split[result_split.length - 1] + | ||
306 | + '\n교수님: ' + prof[prof.length - 1] + '\n학점: ' + credit[credit.length - 1] + '\n' + | ||
307 | + '\n만약 다른 추천을 원하면 \'추천\'을 입력해주세요.\n처음 입력으로 돌아가고 싶으시면 \'시작\'을 입력해주세요'}` | ||
308 | + } | ||
149 | } | 309 | } |
150 | 310 | ||
151 | - request.post( | 311 | + request.post({ |
152 | - { | ||
153 | url: TARGET_URL, | 312 | url: TARGET_URL, |
154 | headers: { | 313 | headers: { |
155 | 'Authorization': `Bearer ${TOKEN}` | 314 | 'Authorization': `Bearer ${TOKEN}` |
... | @@ -158,8 +317,7 @@ app.post('/hook', function (req, res) { | ... | @@ -158,8 +317,7 @@ app.post('/hook', function (req, res) { |
158 | json: { | 317 | json: { |
159 | "replyToken": eventObj.replyToken, | 318 | "replyToken": eventObj.replyToken, |
160 | "messages": message_array | 319 | "messages": message_array |
161 | - }, | 320 | + } |
162 | - | ||
163 | }, (error, response, body) => { | 321 | }, (error, response, body) => { |
164 | console.log(body) | 322 | console.log(body) |
165 | }); | 323 | }); |
... | @@ -169,16 +327,36 @@ app.post('/hook', function (req, res) { | ... | @@ -169,16 +327,36 @@ app.post('/hook', function (req, res) { |
169 | } | 327 | } |
170 | } | 328 | } |
171 | res.sendStatus(200); | 329 | res.sendStatus(200); |
330 | + | ||
331 | + } | ||
332 | + } | ||
333 | + | ||
334 | + } | ||
335 | + | ||
172 | }); | 336 | }); |
173 | 337 | ||
174 | try { | 338 | try { |
175 | const option = { | 339 | const option = { |
176 | - ca: fs.readFileSync('/etc/letsencrypt/live/' + domain + '/fullchain.pem'), | 340 | + ca: fs.readFileSync( |
177 | - key: fs.readFileSync(path.resolve(process.cwd(), '/etc/letsencrypt/live/' + domain + '/privkey.pem'), 'utf8').toString(), | 341 | + '/etc/letsencrypt/live/' + domain + '/fullchain.pem' |
178 | - cert: fs.readFileSync(path.resolve(process.cwd(), '/etc/letsencrypt/live/' + domain + '/cert.pem'), 'utf8').toString(), | 342 | + ), |
343 | + key: fs | ||
344 | + .readFileSync( | ||
345 | + path.resolve(process.cwd(), '/etc/letsencrypt/live/' + domain + '/privkey.pem'), | ||
346 | + 'utf8' | ||
347 | + ) | ||
348 | + .toString(), | ||
349 | + cert: fs | ||
350 | + .readFileSync( | ||
351 | + path.resolve(process.cwd(), '/etc/letsencrypt/live/' + domain + '/cert.pem'), | ||
352 | + 'utf8' | ||
353 | + ) | ||
354 | + .toString() | ||
179 | }; | 355 | }; |
180 | 356 | ||
181 | - HTTPS.createServer(option, app).listen(sslport, () => { | 357 | + HTTPS |
358 | + .createServer(option, app) | ||
359 | + .listen(sslport, () => { | ||
182 | console.log(`[HTTPS] Server is started on port ${sslport}`); | 360 | console.log(`[HTTPS] Server is started on port ${sslport}`); |
183 | }); | 361 | }); |
184 | } catch (error) { | 362 | } catch (error) { | ... | ... |
-
Please register or login to post a comment