Showing
1 changed file
with
59 additions
and
41 deletions
| ... | @@ -10,55 +10,93 @@ const bodyParser = require('body-parser'); | ... | @@ -10,55 +10,93 @@ const bodyParser = require('body-parser'); |
| 10 | 10 | ||
| 11 | // 라인 메신져 | 11 | // 라인 메신져 |
| 12 | const TARGET_URL = 'https://api.line.me/v2/bot/message/reply' | 12 | const TARGET_URL = 'https://api.line.me/v2/bot/message/reply' |
| 13 | -const TOKEN = 'EyomUcCS5ksQZGEvfMJX4JsOZ3C3qrdnU7dK11wHngy' | 13 | +const TOKEN = 'OPv3udmn1UGx+xWZFm80KeZbeGJm63nyt+sTpvBr1Mbc1Y0FAeK4MohQLcWq5yytJ6vnaNgXZfigAEFdbjzs05b4QAU+9r1jcZ05t6QvhWDTRYT0Z+YDMMePXtrQYCT84kmkH0OpJ4smBFwLxrtwtAdB04t89/1O/w1cDnyilFU=' |
| 14 | 14 | ||
| 15 | // 공공보건포털_API | 15 | // 공공보건포털_API |
| 16 | const CORONA_URL = 'http://openapi.data.go.kr/openapi/service/rest/Covid19/getCovid19InfStateJson'; | 16 | const CORONA_URL = 'http://openapi.data.go.kr/openapi/service/rest/Covid19/getCovid19InfStateJson'; |
| 17 | const SERVICE_KEY = 'DMUPOq6b%2B%2F1gqfLEXQ6bnhgoTO73BfPprA6HVNcV%2F6vHMGG%2Bgiw%2BPDkPvX1eQ7b9uvl%2B42fxUPkU0W3Si907Lw%3D%3D' | 17 | const SERVICE_KEY = 'DMUPOq6b%2B%2F1gqfLEXQ6bnhgoTO73BfPprA6HVNcV%2F6vHMGG%2Bgiw%2BPDkPvX1eQ7b9uvl%2B42fxUPkU0W3Si907Lw%3D%3D' |
| 18 | -var queryParams = '?' + encodeURIComponent('ServiceKey') + '=DMUPOq6b%2B%2F1gqfLEXQ6bnhgoTO73BfPprA6HVNcV%2F6vHMGG%2Bgiw%2BPDkPvX1eQ7b9uvl%2B42fxUPkU0W3Si907Lw%3D%3D'; | ||
| 19 | 18 | ||
| 20 | // express 미들웨어 정의 | 19 | // express 미들웨어 정의 |
| 21 | var app = express(); | 20 | var app = express(); |
| 21 | +app.use(bodyParser.json()); | ||
| 22 | 22 | ||
| 23 | // 기타 비동기식 오류처리에 쓰일 모듈들 | 23 | // 기타 비동기식 오류처리에 쓰일 모듈들 |
| 24 | const fs = require('fs'); | 24 | const fs = require('fs'); |
| 25 | const path = require('path'); | 25 | const path = require('path'); |
| 26 | const HTTPS = require('https'); | 26 | const HTTPS = require('https'); |
| 27 | +const { measureMemory } = require('vm'); | ||
| 27 | const domain = "www.oss-chatbot-kwak.tk" | 28 | const domain = "www.oss-chatbot-kwak.tk" |
| 28 | const sslport = 23023; | 29 | const sslport = 23023; |
| 29 | 30 | ||
| 31 | +var date; | ||
| 30 | 32 | ||
| 31 | // 챗봇으로 입력 | 33 | // 챗봇으로 입력 |
| 32 | -app.post('/hook', function (request, response) { | 34 | +app.post('/hook', function (req, res) { |
| 33 | 35 | ||
| 34 | - var eventObj = request.body.events[0]; | 36 | + var eventObj = req.body.events[0]; |
| 35 | var source = eventObj.source; | 37 | var source = eventObj.source; |
| 36 | var message = eventObj.message; | 38 | var message = eventObj.message; |
| 37 | 39 | ||
| 38 | // request log | 40 | // request log |
| 39 | console.log('======================', new Date() ,'======================'); | 41 | console.log('======================', new Date() ,'======================'); |
| 40 | - console.log('[request]', request.body); | 42 | + console.log('[request]', req.body); |
| 41 | console.log('[request source] ', eventObj.source); | 43 | console.log('[request source] ', eventObj.source); |
| 42 | console.log('[request message]', eventObj.message); | 44 | console.log('[request message]', eventObj.message); |
| 43 | 45 | ||
| 44 | - | 46 | + // |
| 45 | - | 47 | + if(message.text == '/코로나' || message.text == '/corona'){ |
| 46 | - | 48 | + request.post( |
| 47 | - | 49 | + { |
| 48 | - response.sendStatus(200); | 50 | + url: TARGET_URL, |
| 51 | + headers: { | ||
| 52 | + 'Authorization': `Bearer ${TOKEN}` | ||
| 53 | + }, | ||
| 54 | + json: { | ||
| 55 | + "replyToken":eventObj.replyToken, | ||
| 56 | + "messages":[ | ||
| 57 | + { | ||
| 58 | + "type":"text", | ||
| 59 | + "text":"확인하고 싶으신 일자를 적어주세요." | ||
| 60 | + }, | ||
| 61 | + { | ||
| 62 | + "type":"text", | ||
| 63 | + "text":"예시) 20201209" | ||
| 64 | + } | ||
| 65 | + ] | ||
| 66 | + } | ||
| 67 | + },(error, response, body) => { | ||
| 68 | + console.log(body) | ||
| 69 | + }); | ||
| 70 | + return; | ||
| 71 | + } | ||
| 72 | + else | ||
| 73 | + { | ||
| 74 | + date = message.text; | ||
| 75 | + queryParams = '?' + encodeURIComponent('ServiceKey') + '=' + SERVICE_KEY; /* Service Key*/ | ||
| 76 | + queryParams += '&' + encodeURIComponent('pageNo') + '=' + encodeURIComponent('1'); /* */ | ||
| 77 | + queryParams += '&' + encodeURIComponent('numOfRows') + '=' + encodeURIComponent('4'); /* */ | ||
| 78 | + queryParams += '&' + encodeURIComponent('startCreateDt') + '=' + encodeURIComponent(date); | ||
| 79 | + queryParams += '&' + encodeURIComponent('endCreateDt') + '=' + encodeURIComponent(date); /* */ | ||
| 80 | + bot_reply(eventObj.replyToken, message); | ||
| 81 | + res.sendStatus(200); | ||
| 82 | + } | ||
| 83 | + | ||
| 49 | }); | 84 | }); |
| 50 | 85 | ||
| 51 | function bot_reply(replyToken, message) { | 86 | function bot_reply(replyToken, message) { |
| 52 | - request.post( | 87 | + request({ |
| 53 | - { | ||
| 54 | url: CORONA_URL + queryParams, | 88 | url: CORONA_URL + queryParams, |
| 55 | method: 'GET' | 89 | method: 'GET' |
| 56 | },(error, response, body) => { | 90 | },(error, response, body) => { |
| 57 | if(!error && response.statusCode == 200) { | 91 | if(!error && response.statusCode == 200) { |
| 58 | console.log('Status', response.statusCode); | 92 | console.log('Status', response.statusCode); |
| 59 | console.log('Headers', JSON.stringify(response.headers)); | 93 | console.log('Headers', JSON.stringify(response.headers)); |
| 60 | - var xmlToJson = convert.xml2json(body, {compact: false, spaces: 4}); | 94 | + var xmlToJson = convert.xml2json(response.body, {compact: true, spaces: 4}); |
| 61 | - console.log('Response received', xmlToJson); | 95 | + const jsonData = JSON.parse(xmlToJson); // json text -> json object |
| 96 | + console.log('Reponse received', xmlToJson); // json text | ||
| 97 | + | ||
| 98 | + var resultMessage = jsonData.response.body.items.item; | ||
| 99 | + | ||
| 62 | request.post( | 100 | request.post( |
| 63 | { | 101 | { |
| 64 | url: TARGET_URL, | 102 | url: TARGET_URL, |
| ... | @@ -70,7 +108,12 @@ function bot_reply(replyToken, message) { | ... | @@ -70,7 +108,12 @@ function bot_reply(replyToken, message) { |
| 70 | "messages":[ | 108 | "messages":[ |
| 71 | { | 109 | { |
| 72 | "type":"text", | 110 | "type":"text", |
| 73 | - "text":xmlToJson | 111 | + "text": '선택하신 날짜의 Corona Virus-19 현황은 다음과 같습니다.' |
| 112 | + | ||
| 113 | + }, | ||
| 114 | + { | ||
| 115 | + "type":"text", | ||
| 116 | + "text": JSON.stringify(resultMessage) | ||
| 74 | } | 117 | } |
| 75 | ] | 118 | ] |
| 76 | } | 119 | } |
| ... | @@ -81,30 +124,6 @@ function bot_reply(replyToken, message) { | ... | @@ -81,30 +124,6 @@ function bot_reply(replyToken, message) { |
| 81 | }); | 124 | }); |
| 82 | } | 125 | } |
| 83 | 126 | ||
| 84 | - | ||
| 85 | - | ||
| 86 | - | ||
| 87 | -/* 요청변수 | ||
| 88 | -var queryParams = '?' + encodeURIComponent('ServiceKey') + '=DMUPOq6b%2B%2F1gqfLEXQ6bnhgoTO73BfPprA6HVNcV%2F6vHMGG%2Bgiw%2BPDkPvX1eQ7b9uvl%2B42fxUPkU0W3Si907Lw%3D%3D'; // Service Key | ||
| 89 | -queryParams += '&' + encodeURIComponent('pageNo') + '=' + encodeURIComponent('1'); | ||
| 90 | -queryParams += '&' + encodeURIComponent('numOfRows') + '=' + encodeURIComponent('10'); | ||
| 91 | -queryParams += '&' + encodeURIComponent('startCreateDt') + '=' + encodeURIComponent('20200310'); | ||
| 92 | -queryParams += '&' + encodeURIComponent('endCreateDt') + '=' + encodeURIComponent('20200315'); | ||
| 93 | -*/ | ||
| 94 | - | ||
| 95 | - | ||
| 96 | -// 공공보건포털에 요청 | ||
| 97 | -/* | ||
| 98 | -request({ | ||
| 99 | - url: url + queryParams, | ||
| 100 | - method: 'GET' | ||
| 101 | -}, function (error, response, body) { | ||
| 102 | - console.log('Status', response.statusCode); | ||
| 103 | - console.log('Headers', JSON.stringify(response.headers)); | ||
| 104 | - console.log('Reponse received', body); | ||
| 105 | -}); | ||
| 106 | -*/ | ||
| 107 | - | ||
| 108 | // 오류처리 | 127 | // 오류처리 |
| 109 | try { | 128 | try { |
| 110 | const option = { | 129 | const option = { |
| ... | @@ -119,5 +138,4 @@ try { | ... | @@ -119,5 +138,4 @@ try { |
| 119 | } catch (error) { | 138 | } catch (error) { |
| 120 | console.log('[HTTPS] HTTPS 오류가 발생하였습니다. HTTPS 서버는 실행되지 않습니다.'); | 139 | console.log('[HTTPS] HTTPS 오류가 발생하였습니다. HTTPS 서버는 실행되지 않습니다.'); |
| 121 | console.log(error); | 140 | console.log(error); |
| 122 | - } | 141 | + } |
| 123 | - | ||
| ... | \ No newline at end of file | ... | \ No newline at end of file | ... | ... |
-
Please register or login to post a comment