곽원석

Update app.js

Showing 1 changed file with 59 additions and 41 deletions
......@@ -10,55 +10,93 @@ const bodyParser = require('body-parser');
// 라인 메신져
const TARGET_URL = 'https://api.line.me/v2/bot/message/reply'
const TOKEN = 'EyomUcCS5ksQZGEvfMJX4JsOZ3C3qrdnU7dK11wHngy'
const TOKEN = 'OPv3udmn1UGx+xWZFm80KeZbeGJm63nyt+sTpvBr1Mbc1Y0FAeK4MohQLcWq5yytJ6vnaNgXZfigAEFdbjzs05b4QAU+9r1jcZ05t6QvhWDTRYT0Z+YDMMePXtrQYCT84kmkH0OpJ4smBFwLxrtwtAdB04t89/1O/w1cDnyilFU='
// 공공보건포털_API
const CORONA_URL = 'http://openapi.data.go.kr/openapi/service/rest/Covid19/getCovid19InfStateJson';
const SERVICE_KEY = 'DMUPOq6b%2B%2F1gqfLEXQ6bnhgoTO73BfPprA6HVNcV%2F6vHMGG%2Bgiw%2BPDkPvX1eQ7b9uvl%2B42fxUPkU0W3Si907Lw%3D%3D'
var queryParams = '?' + encodeURIComponent('ServiceKey') + '=DMUPOq6b%2B%2F1gqfLEXQ6bnhgoTO73BfPprA6HVNcV%2F6vHMGG%2Bgiw%2BPDkPvX1eQ7b9uvl%2B42fxUPkU0W3Si907Lw%3D%3D';
// express 미들웨어 정의
var app = express();
app.use(bodyParser.json());
// 기타 비동기식 오류처리에 쓰일 모듈들
const fs = require('fs');
const path = require('path');
const HTTPS = require('https');
const { measureMemory } = require('vm');
const domain = "www.oss-chatbot-kwak.tk"
const sslport = 23023;
var date;
// 챗봇으로 입력
app.post('/hook', function (request, response) {
app.post('/hook', function (req, res) {
var eventObj = request.body.events[0];
var eventObj = req.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]', req.body);
console.log('[request source] ', eventObj.source);
console.log('[request message]', eventObj.message);
response.sendStatus(200);
//
if(message.text == '/코로나' || message.text == '/corona'){
request.post(
{
url: TARGET_URL,
headers: {
'Authorization': `Bearer ${TOKEN}`
},
json: {
"replyToken":eventObj.replyToken,
"messages":[
{
"type":"text",
"text":"확인하고 싶으신 일자를 적어주세요."
},
{
"type":"text",
"text":"예시) 20201209"
}
]
}
},(error, response, body) => {
console.log(body)
});
return;
}
else
{
date = message.text;
queryParams = '?' + encodeURIComponent('ServiceKey') + '=' + SERVICE_KEY; /* Service Key*/
queryParams += '&' + encodeURIComponent('pageNo') + '=' + encodeURIComponent('1'); /* */
queryParams += '&' + encodeURIComponent('numOfRows') + '=' + encodeURIComponent('4'); /* */
queryParams += '&' + encodeURIComponent('startCreateDt') + '=' + encodeURIComponent(date);
queryParams += '&' + encodeURIComponent('endCreateDt') + '=' + encodeURIComponent(date); /* */
bot_reply(eventObj.replyToken, message);
res.sendStatus(200);
}
});
function bot_reply(replyToken, message) {
request.post(
{
request({
url: CORONA_URL + queryParams,
method: 'GET'
},(error, response, body) => {
if(!error && response.statusCode == 200) {
console.log('Status', response.statusCode);
console.log('Headers', JSON.stringify(response.headers));
var xmlToJson = convert.xml2json(body, {compact: false, spaces: 4});
console.log('Response received', xmlToJson);
var xmlToJson = convert.xml2json(response.body, {compact: true, spaces: 4});
const jsonData = JSON.parse(xmlToJson); // json text -> json object
console.log('Reponse received', xmlToJson); // json text
var resultMessage = jsonData.response.body.items.item;
request.post(
{
url: TARGET_URL,
......@@ -70,7 +108,12 @@ function bot_reply(replyToken, message) {
"messages":[
{
"type":"text",
"text":xmlToJson
"text": '선택하신 날짜의 Corona Virus-19 현황은 다음과 같습니다.'
},
{
"type":"text",
"text": JSON.stringify(resultMessage)
}
]
}
......@@ -81,30 +124,6 @@ function bot_reply(replyToken, message) {
});
}
/* 요청변수
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');
*/
// 공공보건포털에 요청
/*
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);
});
*/
// 오류처리
try {
const option = {
......@@ -119,5 +138,4 @@ try {
} catch (error) {
console.log('[HTTPS] HTTPS 오류가 발생하였습니다. HTTPS 서버는 실행되지 않습니다.');
console.log(error);
}
}
\ No newline at end of file
......