Showing
3 changed files
with
106 additions
and
23 deletions
1 | +//웹 프레임 웤 | ||
1 | var express = require('express'); | 2 | var express = require('express'); |
2 | -var app = express(); | ||
3 | 3 | ||
4 | +// 요청 리소스 표현 모듈 | ||
5 | +var request = require('request'); | ||
6 | + | ||
7 | +// api의 데이터포맷이 xml이므로 json으로 파싱할 필요가 있음 | ||
8 | +var convert = require('xml-js'); | ||
9 | +const bodyParser = require('body-parser'); | ||
10 | + | ||
11 | +// 라인 메신져 | ||
12 | +const TARGET_URL = 'https://api.line.me/v2/bot/message/reply' | ||
13 | +const TOKEN = 'EyomUcCS5ksQZGEvfMJX4JsOZ3C3qrdnU7dK11wHngy' | ||
4 | 14 | ||
5 | // 공공보건포털_API | 15 | // 공공보건포털_API |
6 | -var request = require('request'); | 16 | +const TARGET_URL2 = 'http://openapi.data.go.kr/openapi/service/rest/Covid19/getCovid19InfStateJson'; |
7 | -// 파싱할 URL | 17 | +const SERVICE_KEY = 'DMUPOq6b%2B%2F1gqfLEXQ6bnhgoTO73BfPprA6HVNcV%2F6vHMGG%2Bgiw%2BPDkPvX1eQ7b9uvl%2B42fxUPkU0W3Si907Lw%3D%3D' |
8 | -var url = 'http://openapi.data.go.kr/openapi/service/rest/Covid19/getCovid19InfStateJson'; | 18 | + |
9 | -var queryParams = '?' + encodeURIComponent('ServiceKey') + '=서비스키'; /* Service Key*/ | 19 | +// express 미들웨어 정의 |
10 | -queryParams += '&' + encodeURIComponent('pageNo') + '=' + encodeURIComponent('1'); /* */ | 20 | +var app = express(); |
11 | -queryParams += '&' + encodeURIComponent('numOfRows') + '=' + encodeURIComponent('10'); /* */ | 21 | + |
12 | -queryParams += '&' + encodeURIComponent('startCreateDt') + '=' + encodeURIComponent('20200310'); /* */ | 22 | +// 기타 비동기식 오류처리에 쓰일 모듈들 |
13 | -queryParams += '&' + encodeURIComponent('endCreateDt') + '=' + encodeURIComponent('20200315'); /* */ | 23 | +const fs = require('fs'); |
14 | - | 24 | +const path = require('path'); |
15 | -request({ | 25 | +const HTTPS = require('https'); |
26 | +const domain = "www.oss-chatbot-kwak.tk" | ||
27 | +const sslport = 23023; | ||
28 | + | ||
29 | + | ||
30 | +// 챗봇으로 입력 | ||
31 | +app.post('/hook', function (request, response) { | ||
32 | + | ||
33 | + var eventObj = request.body.events[0]; | ||
34 | + var source = eventObj.source; | ||
35 | + var message = eventObj.message; | ||
36 | + | ||
37 | + // request log | ||
38 | + console.log('======================', new Date() ,'======================'); | ||
39 | + console.log('[request]', request.body); | ||
40 | + console.log('[request source] ', eventObj.source); | ||
41 | + console.log('[request message]', eventObj.message); | ||
42 | + | ||
43 | + | ||
44 | + | ||
45 | + | ||
46 | + | ||
47 | + response.sendStatus(200); | ||
48 | +}); | ||
49 | + | ||
50 | + | ||
51 | + | ||
52 | + | ||
53 | + | ||
54 | + | ||
55 | +/* 요청변수 | ||
56 | +var queryParams = '?' + encodeURIComponent('ServiceKey') + '=DMUPOq6b%2B%2F1gqfLEXQ6bnhgoTO73BfPprA6HVNcV%2F6vHMGG%2Bgiw%2BPDkPvX1eQ7b9uvl%2B42fxUPkU0W3Si907Lw%3D%3D'; // Service Key | ||
57 | +queryParams += '&' + encodeURIComponent('pageNo') + '=' + encodeURIComponent('1'); | ||
58 | +queryParams += '&' + encodeURIComponent('numOfRows') + '=' + encodeURIComponent('10'); | ||
59 | +queryParams += '&' + encodeURIComponent('startCreateDt') + '=' + encodeURIComponent('20200310'); | ||
60 | +queryParams += '&' + encodeURIComponent('endCreateDt') + '=' + encodeURIComponent('20200315'); | ||
61 | +*/ | ||
62 | + | ||
63 | + | ||
64 | +// 공공보건포털에 요청 | ||
65 | +app.request({ | ||
16 | url: url + queryParams, | 66 | url: url + queryParams, |
17 | method: 'GET' | 67 | method: 'GET' |
18 | }, function (error, response, body) { | 68 | }, function (error, response, body) { |
19 | - //console.log('Status', response.statusCode); | ||
20 | - //console.log('Headers', JSON.stringify(response.headers)); | ||
21 | - //console.log('Reponse received', body); | ||
22 | -}); | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
69 | + console.log('Status', response.statusCode); | ||
70 | + console.log('Headers', JSON.stringify(response.headers)); | ||
71 | + console.log('Reponse received', body); | ||
72 | +}); | ||
73 | + | ||
74 | + | ||
75 | + | ||
76 | + | ||
77 | +function getResponeElement({ | ||
78 | + | ||
79 | +}); | ||
80 | + | ||
81 | + | ||
82 | +// 오류처리 | ||
83 | +try { | ||
84 | + const option = { | ||
85 | + ca: fs.readFileSync('/etc/letsencrypt/live/' + domain +'/fullchain.pem'), | ||
86 | + key: fs.readFileSync(path.resolve(process.cwd(), '/etc/letsencrypt/live/' + domain +'/privkey.pem'), 'utf8').toString(), | ||
87 | + cert: fs.readFileSync(path.resolve(process.cwd(), '/etc/letsencrypt/live/' + domain +'/cert.pem'), 'utf8').toString(), | ||
88 | + }; | ||
89 | + | ||
90 | + HTTPS.createServer(option, app).listen(sslport, () => { | ||
91 | + console.log(`[HTTPS] Server is started on port ${sslport}`); | ||
92 | + }); | ||
93 | + } catch (error) { | ||
94 | + console.log('[HTTPS] HTTPS 오류가 발생하였습니다. HTTPS 서버는 실행되지 않습니다.'); | ||
95 | + console.log(error); | ||
96 | + } | ||
97 | + | ... | ... |
This diff is collapsed. Click to expand it.
... | @@ -4,13 +4,21 @@ | ... | @@ -4,13 +4,21 @@ |
4 | "description": "", | 4 | "description": "", |
5 | "main": "app.js", | 5 | "main": "app.js", |
6 | "scripts": { | 6 | "scripts": { |
7 | - "test": "echo \"Error: no test specified\" && exit 1", | 7 | + "test": "echo \"Error: no test specified\" && exit 1" |
8 | - "start": "node server.js" | ||
9 | }, | 8 | }, |
10 | - "author": "KwakWonseok", | 9 | + "repository": { |
11 | - "license": "MIT", | 10 | + "type": "git", |
12 | - "dependencies": { | 11 | + "url": "http://khuhub.khu.ac.kr/2014104077/OSS-Project.git" |
13 | - "@line/bot-sdk": "^6.4.0", | 12 | + } |
14 | - "express": "^4.16.4" | 13 | + "author": { |
14 | + "name": "KwakWonseok", | ||
15 | + "email": "rhkrdnjstjr1@khu.ac.kr", | ||
16 | + "url": "http://khuhub.khu.ac.kr/u/2014104077" | ||
17 | + } | ||
18 | + "license": "MIT", | ||
19 | + "dependencies": { | ||
20 | + "@line/bot-sdk": "^6.4.0", | ||
21 | + "express": "^4.17.1", | ||
22 | + "request": "^2.88.2" | ||
23 | + } | ||
15 | } | 24 | } |
16 | -} | ... | ... |
-
Please register or login to post a comment