Showing
7 changed files
with
144 additions
and
0 deletions
.gitignore
0 → 100644
app.js
0 → 100644
1 | +var express = require('express'); | ||
2 | +require("dotenv").config(); | ||
3 | +var url = 'http://apis.data.go.kr/6410000/busarrivalservice/getBusArrivalList'; | ||
4 | +var queryParams = '?' + encodeURIComponent('serviceKey') + 'Y6FZX1ZGniy+sKjl3AYf+bnh719O0mTDXOdJEODTJ4x1hgFVU60TCHS8Cj//pUtXTkEBp6k2RQ7GGuTqNeAiyA=='; /* Service Key*/ | ||
5 | +queryParams += '&' + encodeURIComponent('stationId') + '=' + encodeURIComponent('200000078'); /* */ | ||
6 | + | ||
7 | +const fs = require('fs'); | ||
8 | +const path = require('path'); | ||
9 | +const HTTPS = require('https'); | ||
10 | +const domain = "2018102225.oss2022chatbot.ml" | ||
11 | +const sslport = 23023; | ||
12 | + | ||
13 | +const bodyParser = require('body-parser'); | ||
14 | +const { recvMessage, makeMessage } = require('./src/chatbot'); | ||
15 | + | ||
16 | +var app = express(); | ||
17 | +app.use(bodyParser.json()); | ||
18 | + | ||
19 | +app.post('/hook', async function (req, res) { | ||
20 | + | ||
21 | + var eventObj = req.body.events[0]; | ||
22 | + var source = eventObj.source; | ||
23 | + var message = eventObj.message; | ||
24 | + | ||
25 | + const replyMessage = await makeMessage(source.userId, message.text); | ||
26 | + | ||
27 | + // request log | ||
28 | + console.log('======================', new Date() ,'======================'); | ||
29 | + console.log('[request source] ', eventObj.source); | ||
30 | + console.log('[request message]', eventObj.message); | ||
31 | + | ||
32 | + recvMessage(eventObj.replyToken, replyMessage); | ||
33 | + | ||
34 | + res.sendStatus(200); | ||
35 | +}); | ||
36 | + | ||
37 | + | ||
38 | + | ||
39 | +try { | ||
40 | + const option = { | ||
41 | + ca: fs.readFileSync('/etc/letsencrypt/live/' + domain +'/fullchain.pem'), | ||
42 | + key: fs.readFileSync(path.resolve(process.cwd(), '/etc/letsencrypt/live/' + domain +'/privkey.pem'), 'utf8').toString(), | ||
43 | + cert: fs.readFileSync(path.resolve(process.cwd(), '/etc/letsencrypt/live/' + domain +'/cert.pem'), 'utf8').toString(), | ||
44 | + }; | ||
45 | + | ||
46 | + HTTPS.createServer(option, app).listen(sslport, () => { | ||
47 | + console.log(`[HTTPS] Server is started on port ${sslport}`); | ||
48 | + }); | ||
49 | + } catch (error) { | ||
50 | + console.log('[HTTPS] HTTPS 오류가 발생하였습니다. HTTPS 서버는 실행되지 않습니다.'); | ||
51 | + console.log(error); | ||
52 | + } | ||
53 | + |
package-lock.json
0 → 100644
This diff is collapsed. Click to expand it.
package.json
0 → 100644
1 | +{ | ||
2 | + "name": "reply", | ||
3 | + "version": "1.0.0", | ||
4 | + "description": "", | ||
5 | + "main": "app.js", | ||
6 | + "scripts": { | ||
7 | + "start": "nodemon app.js" | ||
8 | + }, | ||
9 | + "author": "", | ||
10 | + "license": "ISC", | ||
11 | + "dependencies": { | ||
12 | + "body-parser": "^1.19.0", | ||
13 | + "dotenv": "^16.0.3", | ||
14 | + "express": "^4.17.1", | ||
15 | + "node-fetch": "^3.3.0", | ||
16 | + "request": "^2.88.2", | ||
17 | + "xml-js": "^1.6.11" | ||
18 | + } | ||
19 | +} |
src/bus/request.js
0 → 100644
1 | +var request = require('request'); | ||
2 | +const convert = require("xml-js") | ||
3 | + | ||
4 | +var url = 'http://apis.data.go.kr/6410000/busarrivalservice/getBusArrivalList'; | ||
5 | +var queryParams = '?' + encodeURIComponent('serviceKey') + process.env.SERVICE_KEY; /* Service Key*/ | ||
6 | +queryParams += '&' + encodeURIComponent('stationId') + '=' + encodeURIComponent('228000710'); /* */ | ||
7 | + | ||
8 | +function callRequest() { | ||
9 | + return new Promise((resolve, reject) => { | ||
10 | + request({ | ||
11 | + url: url + queryParams, | ||
12 | + method: 'GET' | ||
13 | + }, function (error, response, body) { | ||
14 | + const data = convert.xml2js(body, { compact: true }).response.msgBody | ||
15 | + if (data) | ||
16 | + resolve(JSON.stringify(data.busArrivalList[1])); | ||
17 | + else | ||
18 | + resolve("운행 중인 버스가 없습니다."); | ||
19 | + }); | ||
20 | + }) | ||
21 | +} | ||
22 | + | ||
23 | +module.exports = callRequest; | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
src/bus/routeidmap.js
0 → 100644
src/chatbot/index.js
0 → 100644
1 | +const request = require("request"); | ||
2 | +const callRequest = require("../bus/request") | ||
3 | + | ||
4 | +let userState = {}; | ||
5 | + | ||
6 | +async function makeMessage(replyToken, message) { | ||
7 | + if (message === '버스') { | ||
8 | + return await callRequest(); | ||
9 | + } else { | ||
10 | + return "명령어를 다시 입력하세요." | ||
11 | + } | ||
12 | +} | ||
13 | + | ||
14 | +function recvMessage(replyToken, messsage) { | ||
15 | + request.post( | ||
16 | + { | ||
17 | + url: process.env.LINE_TARGET_URL, | ||
18 | + headers: { | ||
19 | + 'Authorization': `Bearer ${process.env.CHATBOT_TOKEN}` | ||
20 | + }, | ||
21 | + json: { | ||
22 | + "replyToken": replyToken, | ||
23 | + "messages": [ | ||
24 | + { | ||
25 | + "type": "text", | ||
26 | + "text": messsage | ||
27 | + } | ||
28 | + ] | ||
29 | + } | ||
30 | + }, (error, response, body) => { | ||
31 | + // console.log(body) | ||
32 | + } | ||
33 | + ); | ||
34 | +}; | ||
35 | + | ||
36 | +module.exports = { | ||
37 | + recvMessage, | ||
38 | + makeMessage, | ||
39 | +} | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
-
Please register or login to post a comment