장수창

merge app.js

1 +var express = require('express');
1 const request = require('request'); 2 const request = require('request');
2 -const TARGET_URL = 'https://api.line.me/v2/bot/message/push' 3 +const TARGET_URL = 'https://api.line.me/v2/bot/message/reply'
3 -const MULTI_TARGET_URL = 'https://api.line.me/v2/bot/message/multicast'
4 const BROAD_TARGET_URL = 'https://api.line.me/v2/bot/message/broadcast' 4 const BROAD_TARGET_URL = 'https://api.line.me/v2/bot/message/broadcast'
5 const TOKEN = '' 5 const TOKEN = ''
6 -const USER_ID = 'U2d841876b98eeabbdc2102f467546d00' 6 +const fs = require('fs');
7 +const path = require('path');
8 +const HTTPS = require('https');
9 +const domain = "www.osschatbot2020.tk"
10 +const sslport = 23023;
7 11
8 -// Single User 12 +const bodyParser = require('body-parser');
9 -// request.post( 13 +var app = express();
10 -// { 14 +
11 -// url: TARGET_URL, 15 +app.use(bodyParser.json());
12 -// headers: { 16 +app.post('/hook', function (req, res) {
13 -// 'Authorization': `Bearer ${TOKEN}`
14 -// },
15 -// json: {
16 -// "to": `${USER_ID}`,
17 -// "messages":[
18 -// {
19 -// "type":"text",
20 -// "text":"Hello, user"
21 -// },
22 -// {
23 -// "type":"text",
24 -// "text":"May I help you?"
25 -// }
26 -// ]
27 -// }
28 -// },(error, response, body) => {
29 -// console.log(body)
30 -// });
31 17
18 + var eventObj = req.body.events[0];
19 + var source = eventObj.source;
20 + var message = eventObj.message;
32 21
33 -// Multicast User 22 + // request log
34 -// request.post( 23 + console.log('======================', new Date() ,'======================');
35 -// { 24 + console.log('[request]', req.body);
36 -// url: MULTI_TARGET_URL, 25 + console.log('[request source] ', eventObj.source);
37 -// headers: { 26 + console.log('[request message]', eventObj.message);
38 -// 'Authorization': `Bearer ${TOKEN}`
39 -// },
40 -// json: {
41 -// "to": [`${USER_ID}`],
42 -// "messages":[
43 -// {
44 -// "type":"text",
45 -// "text":"Hello, user"
46 -// },
47 -// {
48 -// "type":"text",
49 -// "text":"May I help you?"
50 -// }
51 -// ]
52 -// }
53 -// },(error, response, body) => {
54 -// console.log(body)
55 -// });
56 27
28 + var isNationWide = message.text;
29 + var sub_nationwide = "국내";
30 + if(isNationWide.indexOf(sub_nationwide) !== -1){
31 + replyNationWide(eventObj);
32 + }
33 + else if(message.text == "야" || message.text == "도움말"){
34 + hello(eventObj)
35 + }
36 +
37 + res.sendStatus(200);
38 +});
39 +
40 +//reply
41 +function replyNationWide(eventObj) {
42 + //read status
43 + var strings = ['날짜', '확진환자', '격리해제', '검사진행', '사망자']
44 + var fs = require('fs');
45 + var array = fs.readFileSync('status.txt').toString().split("\n");
46 + for(i in array) {
47 + strings[i] = array[i]
48 + }
49 +
50 + request.post(
51 + {
52 + url: TARGET_URL,
53 + headers: {
54 + 'Authorization': `Bearer ${TOKEN}`
55 + },
56 + json: {
57 + "replyToken":eventObj.replyToken,
58 + "messages":[
59 + {
60 + "type":"text",
61 + "text":strings[0]
62 + },
63 + {
64 + "type":"text",
65 + "text":strings[1]
66 + },
67 + {
68 + "type":"text",
69 + "text":strings[2]
70 + },
71 + {
72 + "type":"text",
73 + "text":strings[3]
74 + },
75 + {
76 + "type":"text",
77 + "text":strings[4]
78 + },
79 + ]
80 + }
81 + },(error, response, body) => {
82 + console.log(body)
83 + });
84 +}
85 +
86 +//hello
87 +function hello(eventObj) {
88 +
89 + request.post(
90 + {
91 + url: TARGET_URL,
92 + headers: {
93 + 'Authorization': `Bearer ${TOKEN}`
94 + },
95 + json: {
96 + "replyToken":eventObj.replyToken,
97 + "messages":[
98 + {
99 + "type":"text",
100 + "text":"안녕하십니까. 코로나 도우미 입니다."
101 + },
102 + {
103 + "type":"text",
104 + "text":"국내 현황은 '국내'를 타이핑 해주세요."
105 + },
106 + {
107 + "type": "sticker",
108 + "packageId": "11539",
109 + "stickerId": "52114110"
110 + }
111 +
112 + ]
113 + }
114 + },(error, response, body) => {
115 + console.log(body)
116 + });
117 +}
57 118
58 // Broadcast 119 // Broadcast
120 +function broadcast(eventObj){
59 request.post( 121 request.post(
60 { 122 {
61 url: BROAD_TARGET_URL, 123 url: BROAD_TARGET_URL,
...@@ -76,4 +138,20 @@ const USER_ID = 'U2d841876b98eeabbdc2102f467546d00' ...@@ -76,4 +138,20 @@ const USER_ID = 'U2d841876b98eeabbdc2102f467546d00'
76 } 138 }
77 },(error, response, body) => { 139 },(error, response, body) => {
78 console.log(body) 140 console.log(body)
79 - });
...\ No newline at end of file ...\ No newline at end of file
141 + });
142 +}
143 +
144 +try {
145 + const option = {
146 + ca: fs.readFileSync('/etc/letsencrypt/live/' + domain +'/fullchain.pem'),
147 + key: fs.readFileSync(path.resolve(process.cwd(), '/etc/letsencrypt/live/' + domain +'/privkey.pem'), 'utf8').toString(),
148 + cert: fs.readFileSync(path.resolve(process.cwd(), '/etc/letsencrypt/live/' + domain +'/cert.pem'), 'utf8').toString(),
149 + };
150 +
151 + HTTPS.createServer(option, app).listen(sslport, () => {
152 + console.log(`[HTTPS] Server is started on port ${sslport}`);
153 + });
154 + } catch (error) {
155 + console.log('[HTTPS] HTTPS 오류가 발생하였습니다. HTTPS 서버는 실행되지 않습니다.');
156 + console.log(error);
157 + }
...\ No newline at end of file ...\ No newline at end of file
......
1 -var express = require('express');
2 -const request = require('request');
3 -const TARGET_URL = 'https://api.line.me/v2/bot/message/reply'
4 -const BROAD_TARGET_URL = 'https://api.line.me/v2/bot/message/broadcast'
5 -const TOKEN = ''
6 -const fs = require('fs');
7 -const path = require('path');
8 -const HTTPS = require('https');
9 -const domain = "www.osschatbot2020.tk"
10 -const sslport = 23023;
11 -
12 -const bodyParser = require('body-parser');
13 -var app = express();
14 -
15 -app.use(bodyParser.json());
16 -app.post('/hook', function (req, res) {
17 -
18 - var eventObj = req.body.events[0];
19 - var source = eventObj.source;
20 - var message = eventObj.message;
21 -
22 - // request log
23 - console.log('======================', new Date() ,'======================');
24 - console.log('[request]', req.body);
25 - console.log('[request source] ', eventObj.source);
26 - console.log('[request message]', eventObj.message);
27 -
28 - var isNationWide = message.text;
29 - var sub_nationwide = "국내";
30 - if(isNationWide.indexOf(sub_nationwide) !== -1){
31 - replyNationWide(eventObj);
32 - }
33 - else if(message.text == "야" || message.text == "도움말"){
34 - hello(eventObj)
35 - }
36 -
37 - res.sendStatus(200);
38 -});
39 -
40 -//reply
41 -function replyNationWide(eventObj) {
42 - //read status
43 - var strings = ['날짜', '확진환자', '격리해제', '검사진행', '사망자']
44 - var fs = require('fs');
45 - var array = fs.readFileSync('status.txt').toString().split("\n");
46 - for(i in array) {
47 - strings[i] = array[i]
48 - }
49 -
50 - request.post(
51 - {
52 - url: TARGET_URL,
53 - headers: {
54 - 'Authorization': `Bearer ${TOKEN}`
55 - },
56 - json: {
57 - "replyToken":eventObj.replyToken,
58 - "messages":[
59 - {
60 - "type":"text",
61 - "text":strings[0]
62 - },
63 - {
64 - "type":"text",
65 - "text":strings[1]
66 - },
67 - {
68 - "type":"text",
69 - "text":strings[2]
70 - },
71 - {
72 - "type":"text",
73 - "text":strings[3]
74 - },
75 - {
76 - "type":"text",
77 - "text":strings[4]
78 - },
79 - ]
80 - }
81 - },(error, response, body) => {
82 - console.log(body)
83 - });
84 -}
85 -
86 -//hello
87 -function hello(eventObj) {
88 -
89 - request.post(
90 - {
91 - url: TARGET_URL,
92 - headers: {
93 - 'Authorization': `Bearer ${TOKEN}`
94 - },
95 - json: {
96 - "replyToken":eventObj.replyToken,
97 - "messages":[
98 - {
99 - "type":"text",
100 - "text":"안녕하십니까. 코로나 도우미 입니다."
101 - },
102 - {
103 - "type":"text",
104 - "text":"국내 현황은 '국내'를 타이핑 해주세요."
105 - },
106 - {
107 - "type": "sticker",
108 - "packageId": "11539",
109 - "stickerId": "52114110"
110 - }
111 -
112 - ]
113 - }
114 - },(error, response, body) => {
115 - console.log(body)
116 - });
117 -}
118 -
119 -// Broadcast
120 -function broadcast(eventObj){
121 - request.post(
122 - {
123 - url: BROAD_TARGET_URL,
124 - headers: {
125 - 'Authorization': `Bearer ${TOKEN}`
126 - },
127 - json: {
128 - "messages":[
129 - {
130 - "type":"text",
131 - "text":"Hello, user"
132 - },
133 - {
134 - "type":"text",
135 - "text":"May I help you?"
136 - }
137 - ]
138 - }
139 - },(error, response, body) => {
140 - console.log(body)
141 - });
142 -}
143 -
144 -try {
145 - const option = {
146 - ca: fs.readFileSync('/etc/letsencrypt/live/' + domain +'/fullchain.pem'),
147 - key: fs.readFileSync(path.resolve(process.cwd(), '/etc/letsencrypt/live/' + domain +'/privkey.pem'), 'utf8').toString(),
148 - cert: fs.readFileSync(path.resolve(process.cwd(), '/etc/letsencrypt/live/' + domain +'/cert.pem'), 'utf8').toString(),
149 - };
150 -
151 - HTTPS.createServer(option, app).listen(sslport, () => {
152 - console.log(`[HTTPS] Server is started on port ${sslport}`);
153 - });
154 - } catch (error) {
155 - console.log('[HTTPS] HTTPS 오류가 발생하였습니다. HTTPS 서버는 실행되지 않습니다.');
156 - console.log(error);
157 - }
...\ No newline at end of file ...\ No newline at end of file