고혜연

Configure webhook

1 const express = require('express'); 1 const express = require('express');
2 +const axios = require('axios').default;
3 +const FormData = require('form-data');
4 +
5 +const constants = require('./constants');
2 6
3 const app = express(); 7 const app = express();
8 +app.use(express.json());
4 9
5 app.get('/', (req, res) => { 10 app.get('/', (req, res) => {
6 res.send('Hello world!'); 11 res.send('Hello world!');
7 }); 12 });
8 13
14 +app.get('/notify', (req, res) => {
15 + const url = 'https://notify-api.line.me/api/notify'
16 + const form = new FormData();
17 + form.append('message', 'Line Notify Testing...');
18 + form.append('stickerPackageId', 1);
19 + form.append('stickerId', 1);
20 +
21 + axios.post(url, form, { headers: form.getHeaders({ authorization: `Bearer ${constants.LINE.TOKEN}` })})
22 + .then(notifyResponse => {
23 + console.log(notifyResponse);
24 + res.status(200);
25 + res.send('Request Success');
26 + }).catch(err => {
27 + console.log(err);
28 + res.send('Request Error');
29 + });
30 +})
31 +
32 +app.post('/webhook', (req, res) => {
33 + const { body } = req;
34 + const eventObj = body.events[0];
35 + const { source, message, replyToken } = eventObj;
36 +
37 + // request log
38 + console.log('======================', new Date() ,'======================');
39 + console.log('[request]', body);
40 + console.log('[request source] ', source);
41 + console.log('[request message]', message);
42 +
43 + const url = 'https://notify-api.line.me/api/notify'
44 + axios.post(url, {
45 + replyToken,
46 + messages: [
47 + {
48 + type: 'text',
49 + text: 'Hello!',
50 + },
51 + {
52 + type: 'text',
53 + text: 'May I Help U?',
54 + },
55 + ],
56 + }, {
57 + headers: `Bearer ${constants.LINE.TOKEN}`,
58 + }).then(notifyResponse => {
59 + res.status(200);
60 + res.send('Success');
61 + }).catch(err => {
62 + res.send('Failed');
63 + });
64 +});
65 +
9 module.exports = app; 66 module.exports = app;
...\ No newline at end of file ...\ No newline at end of file
......
...@@ -13,9 +13,9 @@ module.exports = { ...@@ -13,9 +13,9 @@ module.exports = {
13 CONFIG: { 13 CONFIG: {
14 PORT: 443, 14 PORT: 443,
15 HTTPS_OPTIONS: { 15 HTTPS_OPTIONS: {
16 - ca: fs.readFileSync(`/etc/letsencrypt/live/${domain}/fullchain.pem`), 16 + // ca: fs.readFileSync(`/etc/letsencrypt/live/${domain}/fullchain.pem`),
17 - key: fs.readFileSync(path.resolve(process.cwd(), `/etc/letsencrypt/live/${domain}/privkey.pem`), 'utf8').toString(), 17 + // key: fs.readFileSync(path.resolve(process.cwd(), `/etc/letsencrypt/live/${domain}/privkey.pem`), 'utf8').toString(),
18 - cert: fs.readFileSync(path.resolve(process.cwd(), `/etc/letsencrypt/live/${domain}/cert.pem`), 'utf8').toString(), 18 + // cert: fs.readFileSync(path.resolve(process.cwd(), `/etc/letsencrypt/live/${domain}/cert.pem`), 'utf8').toString(),
19 } 19 }
20 } 20 }
21 }; 21 };
...\ No newline at end of file ...\ No newline at end of file
......
...@@ -3,12 +3,14 @@ const https = require('https'); ...@@ -3,12 +3,14 @@ const https = require('https');
3 const app = require('./app'); 3 const app = require('./app');
4 const constants = require('./constants'); 4 const constants = require('./constants');
5 5
6 -try {
7 - const options = constants.CONFIG.HTTPS_OPTIONS;
8 - https.createServer(options, app).listen(constants.CONFIG.PORT, () => {
9 - console.log(`[SYSTEM] HTTPS server is running on port ${constants.CONFIG.PORT}`);
10 - })
11 -} catch (err) {
12 - console.log('[SYSTEM] Cannot start HTTPS server.');
13 - console.log(err);
14 -}
...\ No newline at end of file ...\ No newline at end of file
6 +// try {
7 +// const options = constants.CONFIG.HTTPS_OPTIONS;
8 +// https.createServer(options, app).listen(constants.CONFIG.PORT, () => {
9 +// console.log(`[SYSTEM] HTTPS server is running on port ${constants.CONFIG.PORT}`);
10 +// })
11 +// } catch (err) {
12 +// console.log('[SYSTEM] Cannot start HTTPS server.');
13 +// console.log(err);
14 +// }
15 +
16 +app.listen(3000);
...\ No newline at end of file ...\ No newline at end of file
......
...@@ -22,7 +22,9 @@ ...@@ -22,7 +22,9 @@
22 "author": "윤성아", 22 "author": "윤성아",
23 "license": "MIT", 23 "license": "MIT",
24 "dependencies": { 24 "dependencies": {
25 - "express": "^4.17.1" 25 + "axios": "^0.21.1",
26 + "express": "^4.17.1",
27 + "form-data": "^4.0.0"
26 }, 28 },
27 "devDependencies":{ 29 "devDependencies":{
28 "nodemon": "^2.0.7" 30 "nodemon": "^2.0.7"
......