Showing
2 changed files
with
187 additions
and
1 deletions
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 |
-
Please register or login to post a comment