고혜연

Configure webhook

const express = require('express');
const axios = require('axios').default;
const FormData = require('form-data');
const constants = require('./constants');
const app = express();
app.use(express.json());
app.get('/', (req, res) => {
res.send('Hello world!');
});
app.get('/notify', (req, res) => {
const url = 'https://notify-api.line.me/api/notify'
const form = new FormData();
form.append('message', 'Line Notify Testing...');
form.append('stickerPackageId', 1);
form.append('stickerId', 1);
axios.post(url, form, { headers: form.getHeaders({ authorization: `Bearer ${constants.LINE.TOKEN}` })})
.then(notifyResponse => {
console.log(notifyResponse);
res.status(200);
res.send('Request Success');
}).catch(err => {
console.log(err);
res.send('Request Error');
});
})
app.post('/webhook', (req, res) => {
const { body } = req;
const eventObj = body.events[0];
const { source, message, replyToken } = eventObj;
// request log
console.log('======================', new Date() ,'======================');
console.log('[request]', body);
console.log('[request source] ', source);
console.log('[request message]', message);
const url = 'https://notify-api.line.me/api/notify'
axios.post(url, {
replyToken,
messages: [
{
type: 'text',
text: 'Hello!',
},
{
type: 'text',
text: 'May I Help U?',
},
],
}, {
headers: `Bearer ${constants.LINE.TOKEN}`,
}).then(notifyResponse => {
res.status(200);
res.send('Success');
}).catch(err => {
res.send('Failed');
});
});
module.exports = app;
\ No newline at end of file
......
......@@ -13,9 +13,9 @@ module.exports = {
CONFIG: {
PORT: 443,
HTTPS_OPTIONS: {
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(),
// 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(),
}
}
};
\ No newline at end of file
......
......@@ -3,12 +3,14 @@ const https = require('https');
const app = require('./app');
const constants = require('./constants');
try {
const options = constants.CONFIG.HTTPS_OPTIONS;
https.createServer(options, app).listen(constants.CONFIG.PORT, () => {
console.log(`[SYSTEM] HTTPS server is running on port ${constants.CONFIG.PORT}`);
})
} catch (err) {
console.log('[SYSTEM] Cannot start HTTPS server.');
console.log(err);
}
\ No newline at end of file
// try {
// const options = constants.CONFIG.HTTPS_OPTIONS;
// https.createServer(options, app).listen(constants.CONFIG.PORT, () => {
// console.log(`[SYSTEM] HTTPS server is running on port ${constants.CONFIG.PORT}`);
// })
// } catch (err) {
// console.log('[SYSTEM] Cannot start HTTPS server.');
// console.log(err);
// }
app.listen(3000);
\ No newline at end of file
......
......@@ -22,7 +22,9 @@
"author": "윤성아",
"license": "MIT",
"dependencies": {
"express": "^4.17.1"
"axios": "^0.21.1",
"express": "^4.17.1",
"form-data": "^4.0.0"
},
"devDependencies":{
"nodemon": "^2.0.7"
......