김시환

update app.js

Showing 1 changed file with 48 additions and 72 deletions
...@@ -2,11 +2,11 @@ const Address = require('./Address.js'); ...@@ -2,11 +2,11 @@ const Address = require('./Address.js');
2 const express = require('express'); 2 const express = require('express');
3 const request = require('request'); 3 const request = require('request');
4 const TARGET_URL = 'https://api.line.me/v2/bot/message/reply' 4 const TARGET_URL = 'https://api.line.me/v2/bot/message/reply'
5 -const TOKEN = '채널 토큰으로 변경' 5 +const TOKEN = 'YK56BfHFgpILrxRk1FZrdcouFguf5CBA5qxM3zfDH6N9jR/cfPxVdK1P9vZHAk69mZDSlSkXNLy25pzqJbXrn3y76hwmH0Kiuvx3OadFYcuidWSp7VYAj4SqJSljv/q5KoYAOE2il8jmQkf4bQKvXQdB04t89/1O/w1cDnyilFU='
6 const fs = require('fs'); 6 const fs = require('fs');
7 const path = require('path'); 7 const path = require('path');
8 const HTTPS = require('https'); 8 const HTTPS = require('https');
9 -const domain = "도메인 변경" 9 +const domain = "ec2-18-215-162-16.compute-1.amazonaws.com"
10 const sslport = 23023; 10 const sslport = 23023;
11 const bodyParser = require('body-parser'); 11 const bodyParser = require('body-parser');
12 const app = express(); 12 const app = express();
...@@ -14,76 +14,52 @@ const app = express(); ...@@ -14,76 +14,52 @@ const app = express();
14 14
15 console.log(Address.getAddress('석수동길')); 15 console.log(Address.getAddress('석수동길'));
16 16
17 -app.use(express.urlencoded({ extended: false })); 17 +app.use(bodyParser.json());
18 -app.use(express.json());
19 -
20 -app.get('',(req,res)=> {
21 - res.send('tset')
22 -})
23 -
24 app.post('/hook', function (req, res) { 18 app.post('/hook', function (req, res) {
25 - var eventObj = req.body.events[0]; 19 + var eventObj = req.body.events[0];
26 - var source = eventObj.source; 20 + var source = eventObj.source;
27 - var message = eventObj.message; 21 + var message = eventObj.message;
28 - // request log 22 + // request log
29 - console.log('======================', new Date() ,'======================'); 23 + console.log('======================', new Date() ,'======================');
30 - console.log('[request]', req.body); 24 + console.log('[request]', req.body);
31 - console.log('[request source] ', eventObj.source); 25 + console.log('[request source] ', eventObj.source);
32 - console.log('[request message]', eventObj.message); 26 + console.log('[request message]', eventObj.message);
33 - request.post( 27 + request.post(
34 - { 28 + {
35 - url: TARGET_URL, 29 + url: TARGET_URL,
36 - headers: { 30 + headers: {
37 - 'Authorization': `Bearer ${TOKEN}` 31 + 'Authorization': `Bearer ${TOKEN}`
38 - }, 32 + },
39 - json: { 33 + json: {
40 - "replyToken":eventObj.replyToken, 34 + "replyToken":eventObj.replyToken,
41 - "messages":[ 35 + "messages":[
42 - { 36 + {
43 - "type":"text", 37 + "type":"text",
44 - "text":"Hello, user" 38 + "text":"Hello, user"
45 - }, 39 + },
46 - { 40 + {
47 - "type":"text", 41 + "type":"text",
48 - "text":"May I help you?" 42 + "text":"May I help you?"
49 - } 43 + }
50 - ] 44 + ]
51 - } 45 + }
52 - },(error, response, body) => { 46 + },(error, response, body) => {
53 - console.log(body) 47 + console.log(body)
54 - }); 48 + });
55 - 49 +
56 - res.sendStatus(200); 50 + res.sendStatus(200);
57 }); 51 });
58 - 52 +try {
59 - 53 + const option = {
60 -app.get('/keyboard', (req, res) => { 54 + ca: fs.readFileSync('/etc/letsencrypt/live/' + domain +'/fullchain.pem'),
61 - const data = {'type': 'text'} 55 + key: fs.readFileSync(path.resolve(process.cwd(), '/etc/letsencrypt/live/' + domain +'/privkey.pem'), 'utf8').toString(),
62 - res.json(data); 56 + cert: fs.readFileSync(path.resolve(process.cwd(), '/etc/letsencrypt/live/' + domain +'/cert.pem'), 'utf8').toString(),
63 -}); 57 + };
64 -
65 -app.post('/message', (req, res) => {
66 - const question = req.body.userRequest.utterance;
67 - const goMain = '처음으로';
68 58
69 - if (question === '테스트') {
70 - const data = {
71 - 'version': '2.0',
72 - 'template': {
73 - 'outputs': [{
74 - 'simpleText': {
75 - 'text': '테스트'
76 - }
77 - }],
78 - 'quickReplies': [{
79 - 'label': goMain,
80 - 'action': 'message',
81 - 'messageText': goMain
82 - }]
83 - }
84 - }
85 - }
86 - res.json(data);
87 -});
88 -
89 -app.listen(3000, () => console.log('node on 3000'));
...\ No newline at end of file ...\ No newline at end of file
59 + HTTPS.createServer(option, app).listen(sslport, () => {
60 + console.log(`[HTTPS] Server is started on port ${sslport}`);
61 + });
62 + } catch (error) {
63 + console.log('[HTTPS] HTTPS 오류가 발생하였습니다. HTTPS 서버는 실행되지 않습니다.');
64 + console.log(error);
65 + }
...\ No newline at end of file ...\ No newline at end of file
......