Showing
4 changed files
with
101 additions
and
40 deletions
1 | -var express = require('express'); | 1 | +const express = require('express'); |
2 | const request = require('request'); | 2 | const request = require('request'); |
3 | -const TARGET_URL = 'https://api.line.me/v2/bot/message/reply' | 3 | +const talk = require('./talk') |
4 | -const TOKEN = '채널 토큰으로 변경' | 4 | + |
5 | const fs = require('fs'); | 5 | const fs = require('fs'); |
6 | const path = require('path'); | 6 | const path = require('path'); |
7 | const HTTPS = require('https'); | 7 | const HTTPS = require('https'); |
8 | -const domain = "도메인 변경" | ||
9 | -const sslport = 23023; | ||
10 | - | ||
11 | const bodyParser = require('body-parser'); | 8 | const bodyParser = require('body-parser'); |
12 | -var app = express(); | ||
13 | -app.use(bodyParser.json()); | ||
14 | -app.post('/hook', function (req, res) { | ||
15 | 9 | ||
16 | - var eventObj = req.body.events[0]; | ||
17 | - var source = eventObj.source; | ||
18 | - var message = eventObj.message; | ||
19 | 10 | ||
20 | - // request log | 11 | +const TARGET_URL = 'https://api.line.me/v2/bot/message/reply' |
21 | - console.log('======================', new Date() ,'======================'); | 12 | +const tokens = JSON.parse(fs.readFileSync("setting.json")) |
22 | - console.log('[request]', req.body); | 13 | +const sslport = 23023; |
23 | - console.log('[request source] ', eventObj.source); | 14 | +const domain = tokens.domain |
24 | - console.log('[request message]', eventObj.message); | 15 | +const TOKEN = tokens.channel |
16 | +const id = tokens.id | ||
17 | +const pw = tokens.pw | ||
25 | 18 | ||
19 | +function sendText(replyToken, messages) { | ||
26 | request.post( | 20 | request.post( |
27 | { | 21 | { |
28 | url: TARGET_URL, | 22 | url: TARGET_URL, |
... | @@ -30,38 +24,77 @@ app.post('/hook', function (req, res) { | ... | @@ -30,38 +24,77 @@ app.post('/hook', function (req, res) { |
30 | 'Authorization': `Bearer ${TOKEN}` | 24 | 'Authorization': `Bearer ${TOKEN}` |
31 | }, | 25 | }, |
32 | json: { | 26 | json: { |
33 | - "replyToken":eventObj.replyToken, | 27 | + "replyToken": replyToken, |
34 | - "messages":[ | 28 | + "messages": [ |
35 | { | 29 | { |
36 | - "type":"text", | 30 | + "type": "text", |
37 | - "text":"Hello, user" | 31 | + "text": messages |
38 | - }, | 32 | + } |
33 | + ] | ||
34 | + } | ||
35 | + }, (error, response, body) => { | ||
36 | + console.log(body) | ||
37 | + }); | ||
38 | +} | ||
39 | + | ||
40 | +// https://developers.line.biz/en/reference/messaging-api/#image-message | ||
41 | +// OR | ||
42 | +// https://developers.line.biz/en/reference/messaging-api/#location-message | ||
43 | + | ||
44 | +function sendImage(replyToken, imageUrl) { | ||
45 | + request.post( | ||
46 | + { | ||
47 | + url: TARGET_URL, | ||
48 | + headers: { | ||
49 | + 'Authorization': `Bearer ${TOKEN}` | ||
50 | + }, | ||
51 | + json: { | ||
52 | + "replyToken": replyToken, | ||
53 | + "messages": [ | ||
39 | { | 54 | { |
40 | - "type":"text", | 55 | + "type": "image", |
41 | - "text":"May I help you?" | 56 | + "originalContentUrl": imageUrl, |
57 | + "previewImageUrl": imageUrl | ||
42 | } | 58 | } |
43 | ] | 59 | ] |
44 | } | 60 | } |
45 | - },(error, response, body) => { | 61 | + }, (error, response, body) => { |
46 | console.log(body) | 62 | console.log(body) |
47 | }); | 63 | }); |
48 | - | 64 | +} |
65 | + | ||
66 | +var app = express(); | ||
67 | +app.use(bodyParser.json()); | ||
68 | +app.post('/hook', function (req, res) { | ||
69 | + | ||
70 | + var eventObj = req.body.events[0]; | ||
71 | + // var source = eventObj.source; | ||
72 | + // var message = eventObj.message; | ||
73 | + | ||
74 | + // request log | ||
75 | + console.log('======================', new Date(), '======================'); | ||
76 | + console.log('[request]', req.body); | ||
77 | + console.log('[request source] ', eventObj.source); | ||
78 | + console.log('[request message]', eventObj.message); | ||
79 | + | ||
80 | + sendText(eventObj.replyToken, talk.start(id, pw)) // Test only | ||
81 | + //sendImage(eventObj.replyToken) | ||
49 | 82 | ||
50 | res.sendStatus(200); | 83 | res.sendStatus(200); |
51 | }); | 84 | }); |
52 | 85 | ||
53 | try { | 86 | try { |
54 | const option = { | 87 | const option = { |
55 | - ca: fs.readFileSync('/etc/letsencrypt/live/' + domain +'/fullchain.pem'), | 88 | + ca: fs.readFileSync('/etc/letsencrypt/live/' + domain + '/fullchain.pem'), |
56 | - key: fs.readFileSync(path.resolve(process.cwd(), '/etc/letsencrypt/live/' + domain +'/privkey.pem'), 'utf8').toString(), | 89 | + key: fs.readFileSync(path.resolve(process.cwd(), '/etc/letsencrypt/live/' + domain + '/privkey.pem'), 'utf8').toString(), |
57 | - cert: fs.readFileSync(path.resolve(process.cwd(), '/etc/letsencrypt/live/' + domain +'/cert.pem'), 'utf8').toString(), | 90 | + cert: fs.readFileSync(path.resolve(process.cwd(), '/etc/letsencrypt/live/' + domain + '/cert.pem'), 'utf8').toString(), |
58 | }; | 91 | }; |
59 | - | 92 | + |
60 | HTTPS.createServer(option, app).listen(sslport, () => { | 93 | HTTPS.createServer(option, app).listen(sslport, () => { |
61 | - console.log(`[HTTPS] Server is started on port ${sslport}`); | 94 | + console.log(`[HTTPS] Server is started on port ${sslport}`); |
62 | }); | 95 | }); |
63 | - } catch (error) { | 96 | +} catch (error) { |
64 | console.log('[HTTPS] HTTPS 오류가 발생하였습니다. HTTPS 서버는 실행되지 않습니다.'); | 97 | console.log('[HTTPS] HTTPS 오류가 발생하였습니다. HTTPS 서버는 실행되지 않습니다.'); |
65 | console.log(error); | 98 | console.log(error); |
66 | - } | 99 | +} |
67 | - | 100 | + | ... | ... |
... | @@ -13,7 +13,7 @@ | ... | @@ -13,7 +13,7 @@ |
13 | 13 | ||
14 | const ADayForMS = 1000 * 60 * 60 * 24 | 14 | const ADayForMS = 1000 * 60 * 60 * 24 |
15 | 15 | ||
16 | -export async function is_possible_schedule(date, assignments) { | 16 | +async function is_possible_schedule(date, assignments) { |
17 | const assignments_dates = assignments.map(it => new Date(it.due_date)) | 17 | const assignments_dates = assignments.map(it => new Date(it.due_date)) |
18 | const is_disqualified = assignments_dates.filter(it => { | 18 | const is_disqualified = assignments_dates.filter(it => { |
19 | const current_timestamp = date.getTime() | 19 | const current_timestamp = date.getTime() |
... | @@ -23,8 +23,11 @@ export async function is_possible_schedule(date, assignments) { | ... | @@ -23,8 +23,11 @@ export async function is_possible_schedule(date, assignments) { |
23 | }) | 23 | }) |
24 | 24 | ||
25 | if(is_disqualified) { | 25 | if(is_disqualified) { |
26 | - return is_disqualified.at(0) | 26 | + const disq = is_disqualified.at(0) |
27 | + return `${disq.course_name}: ${disq.assignment_name} [${disq.points} 점]` | ||
27 | } else { | 28 | } else { |
28 | return null | 29 | return null |
29 | } | 30 | } |
30 | -} | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
31 | +} | ||
32 | + | ||
33 | +module.exports.is_possible_schedule = is_possible_schedule | ||
... | \ No newline at end of file | ... | \ No newline at end of file | ... | ... |
1 | -class Talk { | ||
2 | - | ||
3 | -} | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
1 | +const canvas = require('./khcanvas') | ||
2 | +const selector = require('./schedule_selector') | ||
3 | + | ||
4 | +function start(id, pw) { | ||
5 | + const today = new Date() | ||
6 | + const filter_result = filter_date(today, id, pw) | ||
7 | + | ||
8 | + if(filter_result) { | ||
9 | + return filter_result | ||
10 | + } | ||
11 | + | ||
12 | + | ||
13 | +} | ||
14 | + | ||
15 | +function filter_date(date, id, pw) { | ||
16 | + const schedule = canvas.get_schedule(id, pw, date) | ||
17 | + const first_todo = selector.is_possible_schedule(date, schedule) | ||
18 | + | ||
19 | + if(first_todo) { | ||
20 | + return `제출되지 않은 과제가 있습니다. ${first_todo}` | ||
21 | + } | ||
22 | + | ||
23 | + return null | ||
24 | +} | ||
25 | + | ||
26 | +module.exports.start = start | ||
... | \ No newline at end of file | ... | \ No newline at end of file | ... | ... |
-
Please register or login to post a comment