app.js 2.94 KB
//웹 프레임 웤
var express = require('express');

// 요청 리소스 표현 모듈
var request = require('request');

// api의 데이터포맷이 xml이므로 json으로 파싱할 필요가 있음
var convert = require('xml-js');
const bodyParser = require('body-parser');

// 라인 메신져
const TARGET_URL = 'https://api.line.me/v2/bot/message/reply'
const TOKEN = 'EyomUcCS5ksQZGEvfMJX4JsOZ3C3qrdnU7dK11wHngy'

// 공공보건포털_API
const TARGET_URL2 = 'http://openapi.data.go.kr/openapi/service/rest/Covid19/getCovid19InfStateJson';
const SERVICE_KEY = 'DMUPOq6b%2B%2F1gqfLEXQ6bnhgoTO73BfPprA6HVNcV%2F6vHMGG%2Bgiw%2BPDkPvX1eQ7b9uvl%2B42fxUPkU0W3Si907Lw%3D%3D'

// express 미들웨어 정의
var app = express();

// 기타 비동기식 오류처리에 쓰일 모듈들
const fs = require('fs'); 
const path = require('path'); 
const HTTPS = require('https');
const domain = "www.oss-chatbot-kwak.tk"
const sslport = 23023;


// 챗봇으로 입력
app.post('/hook', function (request, response) {

    var eventObj = request.body.events[0];
    var source = eventObj.source;
    var message = eventObj.message;

    // request log
    console.log('======================', new Date() ,'======================');
    console.log('[request]', request.body);
    console.log('[request source] ', eventObj.source);
    console.log('[request message]', eventObj.message);

    



    response.sendStatus(200);
});






/* 요청변수
var queryParams = '?' + encodeURIComponent('ServiceKey') + '=DMUPOq6b%2B%2F1gqfLEXQ6bnhgoTO73BfPprA6HVNcV%2F6vHMGG%2Bgiw%2BPDkPvX1eQ7b9uvl%2B42fxUPkU0W3Si907Lw%3D%3D'; // Service Key
queryParams += '&' + encodeURIComponent('pageNo') + '=' + encodeURIComponent('1'); 
queryParams += '&' + encodeURIComponent('numOfRows') + '=' + encodeURIComponent('10'); 
queryParams += '&' + encodeURIComponent('startCreateDt') + '=' + encodeURIComponent('20200310'); 
queryParams += '&' + encodeURIComponent('endCreateDt') + '=' + encodeURIComponent('20200315');
*/


// 공공보건포털에 요청
app.request({
    url: url + queryParams,
    method: 'GET'
}, function (error, response, body) {
    console.log('Status', response.statusCode);
    console.log('Headers', JSON.stringify(response.headers));
    console.log('Reponse received', body);
});




function getResponeElement({
   
});


// 오류처리
try {
    const option = {
      ca: fs.readFileSync('/etc/letsencrypt/live/' + domain +'/fullchain.pem'),
      key: fs.readFileSync(path.resolve(process.cwd(), '/etc/letsencrypt/live/' + domain +'/privkey.pem'), 'utf8').toString(),
      cert: fs.readFileSync(path.resolve(process.cwd(), '/etc/letsencrypt/live/' + domain +'/cert.pem'), 'utf8').toString(),
    };
  
    HTTPS.createServer(option, app).listen(sslport, () => {
      console.log(`[HTTPS] Server is started on port ${sslport}`);
    });
  } catch (error) {
    console.log('[HTTPS] HTTPS 오류가 발생하였습니다. HTTPS 서버는 실행되지 않습니다.');
    console.log(error);
  }