Showing
2 changed files
with
144 additions
and
0 deletions
jaksimsamil-server/src/api/line/app.js
0 → 100644
1 | +const lineCtrl = require("./line.ctrl"); | ||
2 | + | ||
3 | + | ||
4 | +var express = require('express'); | ||
5 | +const request = require('request'); | ||
6 | +const TARGET_URL = 'https://api.line.me/v2/bot/message/reply' | ||
7 | +const TOKEN = 'tdN03DyXDAcKGZsuiPndaU7UC2GzuEQYUhPCkDnGWXGGdiPmV15QYbFU1U5JP5EpiETIKLqOmci/RxFZTfD7FRMED0OpT+3qgEYBNf8T9/jRzHRm7rblBLtzFBVOeXeYRcluOsz4koE4JaAJaUtMtwdB04t89/1O/w1cDnyilFU=' | ||
8 | +const fs = require('fs'); | ||
9 | +const path = require('path'); | ||
10 | +const HTTPS = require('https'); | ||
11 | +const domain = "www.osstest17.ml" | ||
12 | +const sslport = 23023; | ||
13 | + | ||
14 | +const bodyParser = require('body-parser'); | ||
15 | +var app = express(); | ||
16 | +app.use(bodyParser.json()); | ||
17 | +let username; //username 저장 | ||
18 | +app.post('/hook', function (req, res) { | ||
19 | + | ||
20 | + var eventObj = req.body.events[0]; | ||
21 | + //var source = eventObj.source; | ||
22 | + //var message = eventObj.message; | ||
23 | + | ||
24 | + // request log | ||
25 | + console.log('======================', new Date() ,'======================'); | ||
26 | + console.log('[request]', req.body); | ||
27 | + console.log('[request source] ', eventObj.source); | ||
28 | + console.log('[request message]', eventObj.message); | ||
29 | + if((eventObj.message.text == '문제추천')&&(username != undefined)){ | ||
30 | + console.log("문제를 추천합니다."); | ||
31 | + recommendData = lineCtrl.lineRecommend(username); | ||
32 | + recommendBJ(eventObj.replyToken, recommendData); | ||
33 | + } else { | ||
34 | + let isExist = lineCtrl.findID(eventObj.message.text); | ||
35 | + if (isExist == true) | ||
36 | + username = eventObj.message.text; | ||
37 | + else{ | ||
38 | + request.post( | ||
39 | + { | ||
40 | + url: TARGET_URL, | ||
41 | + headers: { | ||
42 | + 'Authorization': `Bearer ${TOKEN}` | ||
43 | + }, | ||
44 | + json: { | ||
45 | + "replyToken":replyToken, | ||
46 | + "messages":[ | ||
47 | + { | ||
48 | + "type":"text", | ||
49 | + "text": "작심삼일 혹은 백준 사이트의 ID값이 올바르지 않습니다. 다시 입력해주세요." | ||
50 | + } | ||
51 | + ] | ||
52 | + } | ||
53 | + },(body) => { | ||
54 | + console.log(body) | ||
55 | + }); | ||
56 | + } | ||
57 | + | ||
58 | + recommendBJ(eventObj.replyToken, eventObj.message.text); | ||
59 | + } | ||
60 | + res.sendStatus(200); | ||
61 | +}, | ||
62 | + | ||
63 | +function recommendBJ(replyToken, message) { | ||
64 | + var recommendProblem = "오늘의 추천문제는 " + message.problem_title + "입니다."; | ||
65 | + var problemURL = "https://www.boj.kr/" + recommendData.problem_number; | ||
66 | + request.post( | ||
67 | + { | ||
68 | + url: TARGET_URL, | ||
69 | + headers: { | ||
70 | + 'Authorization': `Bearer ${TOKEN}` | ||
71 | + }, | ||
72 | + json: { | ||
73 | + "replyToken":replyToken, | ||
74 | + "messages":[ | ||
75 | + { | ||
76 | + "type":"text", | ||
77 | + "text": recommendProblem | ||
78 | + }, | ||
79 | + { | ||
80 | + "type":"text", | ||
81 | + "text": problemURL | ||
82 | + } | ||
83 | + ] | ||
84 | + } | ||
85 | + },(body) => { | ||
86 | + console.log(body) | ||
87 | + }); | ||
88 | + | ||
89 | + | ||
90 | + res.sendStatus(200); | ||
91 | +}); | ||
92 | + | ||
93 | +try { | ||
94 | + const option = { | ||
95 | + ca: fs.readFileSync('/etc/letsencrypt/live/' + domain +'/fullchain.pem'), | ||
96 | + key: fs.readFileSync(path.resolve(process.cwd(), '/etc/letsencrypt/live/' + domain +'/privkey.pem'), 'utf8').toString(), | ||
97 | + cert: fs.readFileSync(path.resolve(process.cwd(), '/etc/letsencrypt/live/' + domain +'/cert.pem'), 'utf8').toString(), | ||
98 | + }; | ||
99 | + | ||
100 | + HTTPS.createServer(option, app).listen(sslport, () => { | ||
101 | + console.log(`[HTTPS] Server is started on port ${sslport}`); | ||
102 | + }); | ||
103 | + } catch (error) { | ||
104 | + console.log('[HTTPS] HTTPS 오류가 발생하였습니다. HTTPS 서버는 실행되지 않습니다.'); | ||
105 | + console.log(error); | ||
106 | + } | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
jaksimsamil-server/src/api/line/line.ctrl.js
0 → 100644
1 | +const Profile = require("../../models/profile"); | ||
2 | +const problem_set = require("../../data/problem_set"); | ||
3 | +const compareBJ = require("../../util/compareBJ"); | ||
4 | + | ||
5 | +/* | ||
6 | +POST api/notify/slack/recommend | ||
7 | +{ | ||
8 | + username: "username" | ||
9 | +} | ||
10 | +*/ | ||
11 | +exports.lineRecommend = (username) => { | ||
12 | + | ||
13 | + const profile = await Profile.findByUsername(username); | ||
14 | + if (!profile) { | ||
15 | + console.log("401"); | ||
16 | + return; | ||
17 | + } | ||
18 | + let unsolved_data = compareBJ.compareBJ( | ||
19 | + profile.getBJdata(), | ||
20 | + problem_set.problem_set | ||
21 | + ); | ||
22 | + let recommendData = compareBJ.randomItem(unsolved_data); | ||
23 | + | ||
24 | + if (!recommendData) { | ||
25 | + console.log("402"); | ||
26 | + return; | ||
27 | + } else { | ||
28 | + return recommendData; | ||
29 | + } | ||
30 | +}; | ||
31 | + | ||
32 | +exports.isExist = (id) => | ||
33 | +{ | ||
34 | + const profile = await Profile.findByUsername(username); | ||
35 | + if (!profile) { | ||
36 | + return false; | ||
37 | + } else return true; | ||
38 | +} | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
-
Please register or login to post a comment