EC2 Default User

initial commit

node_modules/
.env
\ No newline at end of file
var express = require('express');
require("dotenv").config();
var url = 'http://apis.data.go.kr/6410000/busarrivalservice/getBusArrivalList';
var queryParams = '?' + encodeURIComponent('serviceKey') + 'Y6FZX1ZGniy+sKjl3AYf+bnh719O0mTDXOdJEODTJ4x1hgFVU60TCHS8Cj//pUtXTkEBp6k2RQ7GGuTqNeAiyA=='; /* Service Key*/
queryParams += '&' + encodeURIComponent('stationId') + '=' + encodeURIComponent('200000078'); /* */
const fs = require('fs');
const path = require('path');
const HTTPS = require('https');
const domain = "2018102225.oss2022chatbot.ml"
const sslport = 23023;
const bodyParser = require('body-parser');
const { recvMessage, makeMessage } = require('./src/chatbot');
var app = express();
app.use(bodyParser.json());
app.post('/hook', async function (req, res) {
var eventObj = req.body.events[0];
var source = eventObj.source;
var message = eventObj.message;
const replyMessage = await makeMessage(source.userId, message.text);
// request log
console.log('======================', new Date() ,'======================');
console.log('[request source] ', eventObj.source);
console.log('[request message]', eventObj.message);
recvMessage(eventObj.replyToken, replyMessage);
res.sendStatus(200);
});
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);
}
This diff is collapsed. Click to expand it.
{
"name": "reply",
"version": "1.0.0",
"description": "",
"main": "app.js",
"scripts": {
"start": "nodemon app.js"
},
"author": "",
"license": "ISC",
"dependencies": {
"body-parser": "^1.19.0",
"dotenv": "^16.0.3",
"express": "^4.17.1",
"node-fetch": "^3.3.0",
"request": "^2.88.2",
"xml-js": "^1.6.11"
}
}
var request = require('request');
const convert = require("xml-js")
var url = 'http://apis.data.go.kr/6410000/busarrivalservice/getBusArrivalList';
var queryParams = '?' + encodeURIComponent('serviceKey') + process.env.SERVICE_KEY; /* Service Key*/
queryParams += '&' + encodeURIComponent('stationId') + '=' + encodeURIComponent('228000710'); /* */
function callRequest() {
return new Promise((resolve, reject) => {
request({
url: url + queryParams,
method: 'GET'
}, function (error, response, body) {
const data = convert.xml2js(body, { compact: true }).response.msgBody
if (data)
resolve(JSON.stringify(data.busArrivalList[1]));
else
resolve("운행 중인 버스가 없습니다.");
});
})
}
module.exports = callRequest;
\ No newline at end of file
module.exports = {
200000115: "5100",
200000103: "9",
234000016: "1112",
234000001: "5500_1",
200000112: "7000",
234001243: "5107",
}
\ No newline at end of file
const request = require("request");
const callRequest = require("../bus/request")
let userState = {};
async function makeMessage(replyToken, message) {
if (message === '버스') {
return await callRequest();
} else {
return "명령어를 다시 입력하세요."
}
}
function recvMessage(replyToken, messsage) {
request.post(
{
url: process.env.LINE_TARGET_URL,
headers: {
'Authorization': `Bearer ${process.env.CHATBOT_TOKEN}`
},
json: {
"replyToken": replyToken,
"messages": [
{
"type": "text",
"text": messsage
}
]
}
}, (error, response, body) => {
// console.log(body)
}
);
};
module.exports = {
recvMessage,
makeMessage,
}
\ No newline at end of file