Merge branch 'main' of http://khuhub.khu.ac.kr/2021105655/ossw-project
Showing
3 changed files
with
154 additions
and
0 deletions
app.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 = 'R0CqLhe3bFeQRPNy73/j9vtadUXNMnDRHIN6Nvg/PimeyHOgrLE7EAPfc8X3XNw8VjQKRJSHcJor6Tj0GTWjS3+Ns0PLDFRIyELsrSVptRbt/AEJ292DZfPn3uW7xrWfMEKwqHTN9+/ILbRHcdbu1QdB04t89/1O/w1cDnyilFU=' | ||
5 | +const fs = require('fs'); | ||
6 | +const path = require('path'); | ||
7 | +const HTTPS = require('https'); | ||
8 | +const domain = "2021105655.osschatbot2022.ml" | ||
9 | +const sslport = 23023; | ||
10 | +var XMLHttpRequest = require('xhr2'); | ||
11 | + | ||
12 | +const bodyParser = require('body-parser'); | ||
13 | +var app = express(); | ||
14 | +app.use(bodyParser.json()); | ||
15 | +app.post('/hook', function (req, res) { | ||
16 | + | ||
17 | + var eventObj = req.body.events[0]; | ||
18 | + var source = eventObj.source; | ||
19 | + var message = eventObj.message; | ||
20 | + | ||
21 | + // request log | ||
22 | + console.log('======================', new Date(), '======================'); | ||
23 | + console.log('[request]', req.body); | ||
24 | + console.log('[request source] ', eventObj.source); | ||
25 | + console.log('[request message]', eventObj.message); | ||
26 | + | ||
27 | + let newRequest = new XMLHttpRequest(); | ||
28 | + newRequest.onreadystatechange = () => { | ||
29 | + if (newRequest.status == 200 && newRequest.readyState == 4) { | ||
30 | + var dt = new Date(); | ||
31 | + if (dt.getDay() == 0 || dt.getDay() == 6) { | ||
32 | + resJSON0 = JSON.parse(newRequest.responseText).root[0].LASTNEXT[0] | ||
33 | + resJSON1 = JSON.parse(newRequest.responseText).root[0].LASTNEXT[1] | ||
34 | + if (resJSON0.go === "next_mon") { | ||
35 | + let nextRequest = new XMLHttpRequest(); | ||
36 | + nextRequest.onreadystatechange = () => { | ||
37 | + if (nextRequest.status == 200 && nextRequest.readyState == 4) { | ||
38 | + newResJSON = JSON.parse(nextRequest.responseText).root[0].WEEKLYMENU[0] | ||
39 | + request.post( | ||
40 | + { | ||
41 | + url: TARGET_URL, | ||
42 | + headers: { | ||
43 | + 'Authorization': `Bearer ${TOKEN}` | ||
44 | + }, | ||
45 | + json: { | ||
46 | + "replyToken": eventObj.replyToken, | ||
47 | + "messages": [ | ||
48 | + { | ||
49 | + "type": "text", | ||
50 | + "text": newResJSON.fo_menu_lun1 | ||
51 | + } | ||
52 | + ] | ||
53 | + } | ||
54 | + }, (error, response, body) => { | ||
55 | + console.log(body) | ||
56 | + }); | ||
57 | + } | ||
58 | + } | ||
59 | + nextRequest.open('POST', 'https://dorm2.khu.ac.kr/food/getWeeklyMenu.kmc') | ||
60 | + nextRequest.setRequestHeader('Content-type', 'application/x-www-form-urlencoded') | ||
61 | + nextRequest.send("locgbn=K1&sch_date=" + resJSON0.mon_date + "&fo_gbn=stu") | ||
62 | + } else if (resJSON1.go === "next_mon") { | ||
63 | + let nextRequest = new XMLHttpRequest(); | ||
64 | + nextRequest.onreadystatechange = () => { | ||
65 | + if (nextRequest.status == 200 && nextRequest.readyState == 4) { | ||
66 | + newResJSON = JSON.parse(nextRequest.responseText).root[0].WEEKLYMENU[0] | ||
67 | + request.post( | ||
68 | + { | ||
69 | + url: TARGET_URL, | ||
70 | + headers: { | ||
71 | + 'Authorization': `Bearer ${TOKEN}` | ||
72 | + }, | ||
73 | + json: { | ||
74 | + "replyToken": eventObj.replyToken, | ||
75 | + "messages": [ | ||
76 | + { | ||
77 | + "type": "text", | ||
78 | + "text": newResJSON.fo_menu_lun1 | ||
79 | + } | ||
80 | + ] | ||
81 | + } | ||
82 | + }, (error, response, body) => { | ||
83 | + console.log(body) | ||
84 | + }); | ||
85 | + } | ||
86 | + } | ||
87 | + nextRequest.open('POST', 'https://dorm2.khu.ac.kr/food/getWeeklyMenu.kmc') | ||
88 | + nextRequest.setRequestHeader('Content-type', 'application/x-www-form-urlencoded') | ||
89 | + nextRequest.send("locgbn=K1&sch_date=" + resJSON1.mon_date + "&fo_gbn=stu") | ||
90 | + } | ||
91 | + } | ||
92 | + else { | ||
93 | + resJSON = JSON.parse(newRequest.responseText).root[0].WEEKLYMENU[0] | ||
94 | + request.post( | ||
95 | + { | ||
96 | + url: TARGET_URL, | ||
97 | + headers: { | ||
98 | + 'Authorization': `Bearer ${TOKEN}` | ||
99 | + }, | ||
100 | + json: { | ||
101 | + "replyToken": eventObj.replyToken, | ||
102 | + "messages": [ | ||
103 | + { | ||
104 | + "type": "text", | ||
105 | + "text": resJSON.fo_menu_lun1 | ||
106 | + } | ||
107 | + ] | ||
108 | + } | ||
109 | + }, (error, response, body) => { | ||
110 | + console.log(body) | ||
111 | + }); | ||
112 | + } | ||
113 | + } | ||
114 | + } | ||
115 | + newRequest.open('POST', 'https://dorm2.khu.ac.kr/food/getWeeklyMenu.kmc') | ||
116 | + newRequest.setRequestHeader('Content-type', 'application/x-www-form-urlencoded') | ||
117 | + newRequest.send("locgbn=K1&sch_date=&fo_gbn=stu") | ||
118 | + | ||
119 | + res.sendStatus(200); | ||
120 | +}); | ||
121 | + | ||
122 | +try { | ||
123 | + const option = { | ||
124 | + ca: fs.readFileSync('/etc/letsencrypt/live/' + domain + '/fullchain.pem'), | ||
125 | + key: fs.readFileSync(path.resolve(process.cwd(), '/etc/letsencrypt/live/' + domain + '/privkey.pem'), 'utf8').toString(), | ||
126 | + cert: fs.readFileSync(path.resolve(process.cwd(), '/etc/letsencrypt/live/' + domain + '/cert.pem'), 'utf8').toString(), | ||
127 | + }; | ||
128 | + | ||
129 | + HTTPS.createServer(option, app).listen(sslport, () => { | ||
130 | + console.log(`[HTTPS] Server is started on port ${sslport}`); | ||
131 | + }); | ||
132 | +} catch (error) { | ||
133 | + console.log('[HTTPS] HTTPS 오류가 발생하였습니다. HTTPS 서버는 실행되지 않습니다.'); | ||
134 | + console.log(error); | ||
135 | +} | ||
136 | + |
package-lock.json
0 → 100644
This diff is collapsed. Click to expand it.
package.json
0 → 100644
1 | +{ | ||
2 | + "name": "reply", | ||
3 | + "version": "1.0.0", | ||
4 | + "description": "", | ||
5 | + "main": "app.js", | ||
6 | + "scripts": { | ||
7 | + "test": "echo \"Error: no test specified\" && exit 1" | ||
8 | + }, | ||
9 | + "author": "", | ||
10 | + "license": "ISC", | ||
11 | + "dependencies": { | ||
12 | + "body-parser": "^1.19.0", | ||
13 | + "express": "^4.17.1", | ||
14 | + "request": "^2.88.2", | ||
15 | + "xhr2": "^0.2.1" | ||
16 | + } | ||
17 | + } | ||
18 | + | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
-
Please register or login to post a comment