Get data from DB, Implement chatbot and Delete files for noncrash
Showing
6 changed files
with
187 additions
and
160 deletions
.gitignore
deleted
100644 → 0
chatbot.js
0 → 100644
1 | +var express = require('express'); | ||
2 | +const request = require('request'); | ||
3 | +const TARGET_URL = 'https://api.line.me/v2/bot/message/reply' | ||
4 | +const TOKEN = '' | ||
5 | +const fs = require('fs'); | ||
6 | +const path = require('path'); | ||
7 | +const HTTPS = require('https'); | ||
8 | +const domain = "" | ||
9 | +const sslport = 23023; | ||
10 | +const bodyParser = require('body-parser'); | ||
11 | +var app = express(); | ||
12 | +app.use(bodyParser.json()); | ||
13 | +const config = require('./config') | ||
14 | +const mongoose = require("mongoose"); | ||
15 | +const { range } = require("express/lib/request"); | ||
16 | +const connect = mongoose.connect(config.url, | ||
17 | + { | ||
18 | + useNewUrlParser: true, | ||
19 | + useUnifiedTopology: true, | ||
20 | + }) | ||
21 | + .then(() => console.log("DB : Succesfully Connected")) | ||
22 | + .catch((err) => console.log(err.message)); | ||
23 | +var express = require('express'); | ||
24 | +const { Subinfo } = require('./models/model'); | ||
25 | + | ||
26 | +ID_data_array = [] | ||
27 | + | ||
28 | +app.post('/hook', function (req, res) { | ||
29 | + | ||
30 | + var eventObj = req.body.events[0]; | ||
31 | + var source = eventObj.source; | ||
32 | + var message = eventObj.message; | ||
33 | + | ||
34 | + var user_ID = eventObj.source['userId'] | ||
35 | + var data = eventObj.message['text'] | ||
36 | + if (ID_data_array.length == 0) { | ||
37 | + ID_data_array.push([user_ID]) | ||
38 | + ID_data_array[0].push(data) | ||
39 | + } | ||
40 | + else { | ||
41 | + var find = 0 | ||
42 | + for (var i in ID_data_array) { | ||
43 | + if (ID_data_array[i][0] == user_ID) { | ||
44 | + find = 1 | ||
45 | + if (ID_data_array[i].length < 5) { | ||
46 | + ID_data_array[i].push(data) | ||
47 | + } | ||
48 | + else { | ||
49 | + ID_data_array[i] = [user_ID]; | ||
50 | + ID_data_array[i].push(data); | ||
51 | + } | ||
52 | + } | ||
53 | + } | ||
54 | + if (find == 0) { | ||
55 | + ID_data_array.push([user_ID]); | ||
56 | + ID_data_array[ID_data_array.length - 1].push(data); | ||
57 | + } | ||
58 | + } | ||
59 | + | ||
60 | + // request log | ||
61 | + console.log('======================', new Date(), '======================'); | ||
62 | + console.log('[request]', req.body); | ||
63 | + console.log('[request source] ', eventObj.source); | ||
64 | + console.log('[request message]', eventObj.message); | ||
65 | + | ||
66 | + console.log(ID_data_array) | ||
67 | + for (var i in ID_data_array) { | ||
68 | + console.log(ID_data_array[i]) | ||
69 | + } | ||
70 | + | ||
71 | + for (var i in ID_data_array) { | ||
72 | + if (ID_data_array[i][0] == user_ID) { | ||
73 | + if (ID_data_array[i].length == 5) { | ||
74 | + | ||
75 | + var campusinfo = ID_data_array[i][1] | ||
76 | + var collegeinfo = ID_data_array[i][2] | ||
77 | + var departmentinfo = ID_data_array[i][3] | ||
78 | + var gradeinfo = ID_data_array[i][4] | ||
79 | + | ||
80 | + var name = [] | ||
81 | + var time = [] | ||
82 | + var prof = [] | ||
83 | + var credit = [] | ||
84 | + | ||
85 | + Subinfo.find({ campus: `${campusinfo}`, college: `${collegeinfo}`, department: `${departmentinfo}`, grade: `${gradeinfo}` }).then((sub) => { | ||
86 | + if (sub) { | ||
87 | + console.log(sub) | ||
88 | + for (var i in sub) { | ||
89 | + if (sub[i]['subject'].length == 0) { | ||
90 | + continue | ||
91 | + } | ||
92 | + | ||
93 | + if (sub[i]['choice'] == '필수') { | ||
94 | + name.push(sub[i]['name']) | ||
95 | + credit.push(sub[i]['credit']) | ||
96 | + | ||
97 | + var random_num = (Math.floor(Math.random() * (sub[i]['subject'].length))) | ||
98 | + | ||
99 | + if (time.length > 0) { | ||
100 | + while (true) { | ||
101 | + try { | ||
102 | + var tmp = 0 | ||
103 | + var random_num = (Math.floor(Math.random() * (sub[i]['subject'].length))) | ||
104 | + | ||
105 | + for (var j in time) { | ||
106 | + 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)}`) | ||
108 | + 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)}`) | ||
110 | + | ||
111 | + if (tmp_time3 >= tmp_time1 & tmp_time3 <= tmp_time2) { | ||
112 | + tmp = 1 | ||
113 | + break | ||
114 | + } | ||
115 | + } | ||
116 | + } | ||
117 | + if (tmp == 0) { | ||
118 | + break | ||
119 | + } | ||
120 | + } | ||
121 | + catch (err) { | ||
122 | + console.error(err); | ||
123 | + } | ||
124 | + } | ||
125 | + } | ||
126 | + time.push(sub[i]['subject'][random_num]['time']) | ||
127 | + prof.push(sub[i]['subject'][random_num]['prof']) | ||
128 | + } | ||
129 | + } | ||
130 | + } | ||
131 | + else { | ||
132 | + console.log(err) | ||
133 | + } | ||
134 | + | ||
135 | + var message_array = [] | ||
136 | + | ||
137 | + for (var i in name) { | ||
138 | + var message_json = { | ||
139 | + 'type': 'text', | ||
140 | + 'text': `${'과목명: ' + name[i] + '\n' + '강의 시간: ' + time[i] + '\n' + '교수님: ' + prof[i] + '\n' + '학점: ' + credit[i]}` | ||
141 | + } | ||
142 | + message_array.push(message_json) | ||
143 | + } | ||
144 | + if (message_array.length == 0) { | ||
145 | + message_array = [{ | ||
146 | + 'type': 'text', | ||
147 | + 'text': '입력과정에서 오류가 있습니다. \'시작\'을 입력해주세요.' | ||
148 | + }] | ||
149 | + } | ||
150 | + | ||
151 | + request.post( | ||
152 | + { | ||
153 | + url: TARGET_URL, | ||
154 | + headers: { | ||
155 | + 'Authorization': `Bearer ${TOKEN}` | ||
156 | + }, | ||
157 | + | ||
158 | + json: { | ||
159 | + "replyToken": eventObj.replyToken, | ||
160 | + "messages": message_array | ||
161 | + }, | ||
162 | + | ||
163 | + }, (error, response, body) => { | ||
164 | + console.log(body) | ||
165 | + }); | ||
166 | + }) | ||
167 | + } | ||
168 | + break | ||
169 | + } | ||
170 | + } | ||
171 | + res.sendStatus(200); | ||
172 | +}); | ||
173 | + | ||
174 | +try { | ||
175 | + const option = { | ||
176 | + ca: fs.readFileSync('/etc/letsencrypt/live/' + domain + '/fullchain.pem'), | ||
177 | + key: fs.readFileSync(path.resolve(process.cwd(), '/etc/letsencrypt/live/' + domain + '/privkey.pem'), 'utf8').toString(), | ||
178 | + cert: fs.readFileSync(path.resolve(process.cwd(), '/etc/letsencrypt/live/' + domain + '/cert.pem'), 'utf8').toString(), | ||
179 | + }; | ||
180 | + | ||
181 | + HTTPS.createServer(option, app).listen(sslport, () => { | ||
182 | + console.log(`[HTTPS] Server is started on port ${sslport}`); | ||
183 | + }); | ||
184 | +} catch (error) { | ||
185 | + console.log('[HTTPS] HTTPS 오류가 발생하였습니다. HTTPS 서버는 실행되지 않습니다.'); | ||
186 | + console.log(error); | ||
187 | +} | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
getdata.js
deleted
100644 → 0
1 | -const config = require('./config') | ||
2 | -const mongoose = require("mongoose"); | ||
3 | -const {range} = require("express/lib/request"); | ||
4 | -const xlsx = require("xlsx"); | ||
5 | -const fs = require('fs') | ||
6 | - | ||
7 | -const connect = mongoose | ||
8 | - .connect(config.url, { | ||
9 | - useNewUrlParser: true, | ||
10 | - useUnifiedTopology: true | ||
11 | - }) | ||
12 | - .then(() => console.log("DB : Succesfully Connected")) | ||
13 | - .catch((err) => console.log(err.message)); | ||
14 | - | ||
15 | -var express = require('express'); | ||
16 | - | ||
17 | -const {Subinfo} = require('./models/model'); | ||
18 | -var request = require('request'); | ||
19 | - | ||
20 | -var departmentinfo = '컴퓨터공학과' | ||
21 | -var gradeinfo = '2학년1학기' | ||
22 | -var name = [] | ||
23 | -var time = [] | ||
24 | -var prof = [] | ||
25 | - | ||
26 | -Subinfo | ||
27 | - .find({department: `${departmentinfo}`, grade: `${gradeinfo}`}) | ||
28 | - .then((sub) => { | ||
29 | - if (sub) { | ||
30 | - for (var i in sub) { | ||
31 | - if (sub[i]['choice'] == '필수') { | ||
32 | - name.push(sub[i]['name']) | ||
33 | - | ||
34 | - var random_num = (Math.floor(Math.random() * (sub[i]['subject'].length))) | ||
35 | - sub[i]['subject'][random_num]['time'] | ||
36 | - | ||
37 | - if (time.length > 0) { | ||
38 | - /* | ||
39 | - while (true) { | ||
40 | - try { | ||
41 | - var tmp = 0 | ||
42 | - var random_num = (Math.floor(Math.random() * (sub[i]['subject'].length))) | ||
43 | - | ||
44 | - for (var j in time) { | ||
45 | - if (time[j].slice(0, 13) == sub[i]['subject'][random_num]['time'].slice(0, 13)) { | ||
46 | - tmp = 1 | ||
47 | - break | ||
48 | - } | ||
49 | - } | ||
50 | - if (tmp == 0) { | ||
51 | - break | ||
52 | - } | ||
53 | - } | ||
54 | - catch (err) { | ||
55 | - console.error(err); | ||
56 | - } | ||
57 | - } | ||
58 | - */ | ||
59 | - while (true) { | ||
60 | - try { | ||
61 | - var tmp = 0 | ||
62 | - var random_num = (Math.floor(Math.random() * (sub[i]['subject'].length))) | ||
63 | - | ||
64 | - for (var j in time) { | ||
65 | - if (time[j].slice(0, 1) == sub[i]['subject'][random_num]['time'].slice(0, 1)) { | ||
66 | - var tmp_time1 = Number(`${time[j].slice(2, 4) + '.' + time[j].slice(5, 7)}`) | ||
67 | - var tmp_time2 = Number(`${time[j].slice(8, 10) + '.' + time[j].slice(11, 13)}`) | ||
68 | - var tmp_time3 = Number( | ||
69 | - `${sub[i]['subject'][random_num]['time'].slice(2, 4) + '.' + sub[i]['subject'][random_num]['time'].slice( | ||
70 | - 5, | ||
71 | - 7 | ||
72 | - )}` | ||
73 | - ) | ||
74 | - | ||
75 | - if (tmp_time3 >= tmp_time1 & tmp_time3 <= tmp_time2) { | ||
76 | - tmp = 1 | ||
77 | - break | ||
78 | - } | ||
79 | - } | ||
80 | - } | ||
81 | - if (tmp == 0) { | ||
82 | - break | ||
83 | - } | ||
84 | - } catch (err) { | ||
85 | - console.error(err); | ||
86 | - } | ||
87 | - } | ||
88 | - } | ||
89 | - time.push(sub[i]['subject'][random_num]['time']) | ||
90 | - prof.push(sub[i]['subject'][random_num]['prof']) | ||
91 | - | ||
92 | - } | ||
93 | - } | ||
94 | - console.log(name) | ||
95 | - console.log(time) | ||
96 | - console.log(prof) | ||
97 | - } else { | ||
98 | - console.log(err) | ||
99 | - } | ||
100 | - }) | ||
101 | - | ||
102 | - //console.log(Math.floor(Math.random() * (10))) * / | ||
103 | - | ||
104 | -/* | ||
105 | -while (true) { | ||
106 | - try { | ||
107 | - var tmp = 0 | ||
108 | - var random_num = (Math.floor(Math.random() * (sub[i]['subject'].length))) | ||
109 | - | ||
110 | - for (var j in time) { | ||
111 | - if (time[j].slice(0, 1) == sub[i]['subject'][random_num]['time'].slice(0, 1)) { | ||
112 | - var tmp_time1 = Number(`${time[j].slice(2, 4) + '.' + time[j].slice(5, 7)}`) | ||
113 | - var tmp_time2 = Number(`${time[j].slice(8, 10) + '.' + time[j].slice(11, 13)}`) | ||
114 | - var tmp_time3 = Number(`${sub[i]['subject'][random_num]['time'].slice(2, 4) + '.' + sub[i]['subject'][random_num]['time'].slice(5, 7)}`) | ||
115 | - | ||
116 | - if (tmp_time3 >= tmp_time1 & tmp_time3 <= tmp_time2) { | ||
117 | - tmp = 1 | ||
118 | - break | ||
119 | - } | ||
120 | - } | ||
121 | - } | ||
122 | - if (tmp == 0) { | ||
123 | - break | ||
124 | - } | ||
125 | - } | ||
126 | - catch (err) { | ||
127 | - console.error(err); | ||
128 | - } | ||
129 | -} | ||
130 | -*/ | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
models/model.js
deleted
100644 → 0
1 | -const mongoose = require("mongoose"); | ||
2 | - | ||
3 | - const TimeSchema = new mongoose.Schema({ | ||
4 | - time: String, | ||
5 | - prof: String, | ||
6 | - }); | ||
7 | - | ||
8 | - const SubSchema = new mongoose.Schema({ | ||
9 | - campus: String, | ||
10 | - college: String, | ||
11 | - department: String, | ||
12 | - grade: String, | ||
13 | - name: String, | ||
14 | - credit: String, | ||
15 | - choice: String, | ||
16 | - subject: [TimeSchema] | ||
17 | - }); | ||
18 | - | ||
19 | - const Subinfo = mongoose.model('Subinfo', SubSchema); | ||
20 | - module.exports = {Subinfo}; | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
package-lock.json
deleted
100644 → 0
This diff is collapsed. Click to expand it.
-
Please register or login to post a comment