Showing
1 changed file
with
286 additions
and
0 deletions
app.js
0 → 100644
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 = 'Z2sc8dyYuNcGkJmaoWKsZuWW1xRvJIhK8X7ZGA1jyVai7UdHsnriYQ8EdHPP1DICspF8HxxfvqxH9+hePe5y5JUf8OFYC20pSKGVm5ZFUsRn5+f6puVwQ1sirgU3+/+5EUkuCAH4KxRXcZTNRK2sdQdB04t89/1O/w1cDnyilFU=';//'채널 토큰으로 변경' | ||
6 | +const fs = require('fs'); | ||
7 | +const path = require('path'); | ||
8 | +const HTTPS = require('https'); | ||
9 | +const domain = "2017103049.osschatbot2022.ml";//"도메인 변경" | ||
10 | +const sslport = 23023; | ||
11 | + | ||
12 | +var client_id = 'uiPeCv0BhpI5i0cyMZ5T'; | ||
13 | +var client_secret = 'c1E9jfG020'; | ||
14 | +var newsOn = false; | ||
15 | +var newsOff = true; | ||
16 | +var headline_max = ''; | ||
17 | +var headline_min = ''; | ||
18 | + | ||
19 | +const bodyParser = require('body-parser'); | ||
20 | +var app = express(); | ||
21 | +app.use(bodyParser.json()); | ||
22 | +app.post('/hook', function (req, res) { | ||
23 | + | ||
24 | + var eventObj = req.body.events[0]; | ||
25 | + var source = eventObj.source; | ||
26 | + var message = eventObj.message; | ||
27 | + | ||
28 | +// request-reply | ||
29 | + console.log('======================', new Date() ,'======================'); | ||
30 | + console.log('[request]', req.body); | ||
31 | + console.log('[request source] ', eventObj.source); | ||
32 | + console.log('[request message]', eventObj.message); | ||
33 | + | ||
34 | + if (message.text == 'news on') { | ||
35 | + newsOn = true; | ||
36 | + newsOff = false; | ||
37 | + request.post( | ||
38 | + { | ||
39 | + url: TARGET_URL, | ||
40 | + headers: { | ||
41 | + 'Authorization': `Bearer ${TOKEN}` | ||
42 | + }, | ||
43 | + json: { | ||
44 | + "replyToken":eventObj.replyToken, | ||
45 | + "messages":[ | ||
46 | + { | ||
47 | + "type":"text", | ||
48 | + "text":"[news On] 키워드를 입력 하세요." | ||
49 | + } | ||
50 | + ] | ||
51 | + } | ||
52 | + },(error, response, body) => { | ||
53 | + console.log(body) | ||
54 | + }); | ||
55 | + } else if ( message.text == 'news off') { | ||
56 | + newsOn = false; | ||
57 | + newsOff = true; | ||
58 | + request.post( | ||
59 | + { | ||
60 | + url: TARGET_URL, | ||
61 | + headers: { | ||
62 | + 'Authorization': `Bearer ${TOKEN}` | ||
63 | + }, | ||
64 | + json: { | ||
65 | + "replyToken":eventObj.replyToken, | ||
66 | + "messages":[ | ||
67 | + { | ||
68 | + "type":"text", | ||
69 | + "text":"[news Off] 뉴스를 종료합니다." | ||
70 | + } | ||
71 | + ] | ||
72 | + } | ||
73 | + },(error, response, body) => { | ||
74 | + console.log(body) | ||
75 | + }); | ||
76 | + } | ||
77 | + | ||
78 | + if (newsOn & message.text != 'help' & message.text != 'news on') { | ||
79 | + var api_url = 'https://openapi.naver.com/v1/search/news?query=' + encodeURI(message.text); // json 결과 | ||
80 | + var options = { | ||
81 | + url: api_url, | ||
82 | + headers: {'X-Naver-Client-Id':client_id, 'X-Naver-Client-Secret': client_secret} | ||
83 | + }; | ||
84 | + request.get(options, function (error, response, body) { | ||
85 | + const obj = JSON.parse(body); | ||
86 | + const str = JSON.stringify(obj.items[0]); | ||
87 | + const obj2 = JSON.parse(str); | ||
88 | + if (!error && response.statusCode == 200) { | ||
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": "<<<헤드라인>>>\n" + JSON.stringify(obj2.title).replace(/<[^>]*>?/g, '') | ||
101 | + }, | ||
102 | + { | ||
103 | + "type":"text", | ||
104 | + "text": "<<<주요문단>>>\n" + JSON.stringify(obj2.description).replace(/<[^>]*>?/g, '') | ||
105 | + }, | ||
106 | + { | ||
107 | + "type":"text", | ||
108 | + "text": "뉴스 바로가기\n" + JSON.stringify(obj2.link) | ||
109 | + } | ||
110 | + ] | ||
111 | + } | ||
112 | + },(error, response, body) => { | ||
113 | + console.log(body) | ||
114 | + } | ||
115 | + ) | ||
116 | + } else { | ||
117 | + res.status(response.statusCode).end(); | ||
118 | + console.log('error = ' + response.statusCode); | ||
119 | + } | ||
120 | + }); | ||
121 | + | ||
122 | + } | ||
123 | + | ||
124 | + if (message.text == 'help') { | ||
125 | + request.post( | ||
126 | + { | ||
127 | + url: TARGET_URL, | ||
128 | + headers: { | ||
129 | + 'Authorization': `Bearer ${TOKEN}` | ||
130 | + }, | ||
131 | + json: { | ||
132 | + "replyToken":eventObj.replyToken, | ||
133 | + "messages":[ | ||
134 | + { | ||
135 | + "type":"text", | ||
136 | + "text":"*****************\ | ||
137 | + \n반갑습니다. 주식 news 알리미 입니다.\ | ||
138 | + \n1. 뉴스 검색 기능 활성화\ | ||
139 | + \n\'news on\' 입력\ | ||
140 | + \n2. 뉴스 검색 기능 비활성화\ | ||
141 | + \n\'news off\' 입력\ | ||
142 | + \n*****************" | ||
143 | + } | ||
144 | + ] | ||
145 | + } | ||
146 | + },(error, response, body) => { | ||
147 | + console.log(body) | ||
148 | + }); | ||
149 | + } | ||
150 | + | ||
151 | + | ||
152 | + res.sendStatus(200); | ||
153 | +}); | ||
154 | + | ||
155 | +// Broadcast (push) | ||
156 | +const PushFunc_max = function() { | ||
157 | + var api_url = 'https://openapi.naver.com/v1/search/news?query=' + encodeURI('상한가 주식'); // json 결과 | ||
158 | + var options = { | ||
159 | + url: api_url, | ||
160 | + headers: {'X-Naver-Client-Id':client_id, 'X-Naver-Client-Secret': client_secret} | ||
161 | + }; | ||
162 | + request.get(options, function (error, response, body) { | ||
163 | + const obj = JSON.parse(body); | ||
164 | + const str = JSON.stringify(obj.items[0]); | ||
165 | + const obj2 = JSON.parse(str); | ||
166 | + if(headline_max == '' || headline_max != JSON.stringify(obj2.title).replace(/<[^>]*>?/g, '')) { | ||
167 | + if (!error && response.statusCode == 200) { | ||
168 | + headline_max = JSON.stringify(obj2.title).replace(/<[^>]*>?/g, '') | ||
169 | + request.post( | ||
170 | + { | ||
171 | + url: BROAD_TARGET_URL, | ||
172 | + headers: { | ||
173 | + 'Authorization': `Bearer ${TOKEN}` | ||
174 | + }, | ||
175 | + json: { | ||
176 | + "messages": [ | ||
177 | + { | ||
178 | + "type" :"text", | ||
179 | + "text": "[상한가 소식]" | ||
180 | + }, | ||
181 | + { | ||
182 | + "type":"text", | ||
183 | + "text": "<<<헤드라인>>>\n" + JSON.stringify(obj2.title).replace(/<[^>]*>?/g, '') | ||
184 | + }, | ||
185 | + { | ||
186 | + "type":"text", | ||
187 | + "text": "<<<주요문단>>>\n" + JSON.stringify(obj2.description).replace(/<[^>]*>?/g, '') | ||
188 | + }, | ||
189 | + { | ||
190 | + "type":"text", | ||
191 | + "text": "뉴스 바로가기\n" + JSON.stringify(obj2.link) | ||
192 | + } | ||
193 | + ] | ||
194 | + } | ||
195 | + },(error, response, body) => { | ||
196 | + console.log(body) | ||
197 | + } | ||
198 | + ) | ||
199 | + } else { | ||
200 | + res.status(response.statusCode).end(); | ||
201 | + console.log('error = ' + response.statusCode); | ||
202 | + } | ||
203 | + } else { | ||
204 | + console.log('새로운 소식 없음_max') | ||
205 | + } | ||
206 | + | ||
207 | + }); | ||
208 | + | ||
209 | + | ||
210 | + }; | ||
211 | +const PushFunc_min = function() { | ||
212 | + var api_url = 'https://openapi.naver.com/v1/search/news?query=' + encodeURI('하한가 주식'); // json 결과 | ||
213 | + var options = { | ||
214 | + url: api_url, | ||
215 | + headers: {'X-Naver-Client-Id':client_id, 'X-Naver-Client-Secret': client_secret} | ||
216 | + }; | ||
217 | + request.get(options, function (error, response, body) { | ||
218 | + const obj = JSON.parse(body); | ||
219 | + const str = JSON.stringify(obj.items[0]); | ||
220 | + const obj2 = JSON.parse(str); | ||
221 | + if(headline_min == '' || headline_min != JSON.stringify(obj2.title).replace(/<[^>]*>?/g, '')) { | ||
222 | + if (!error && response.statusCode == 200) { | ||
223 | + headline_min = JSON.stringify(obj2.title).replace(/<[^>]*>?/g, '') | ||
224 | + request.post( | ||
225 | + { | ||
226 | + url: BROAD_TARGET_URL, | ||
227 | + headers: { | ||
228 | + 'Authorization': `Bearer ${TOKEN}` | ||
229 | + }, | ||
230 | + json: { | ||
231 | + "messages": [ | ||
232 | + { | ||
233 | + "type" :"text", | ||
234 | + "text": "[하한가 소식]" | ||
235 | + }, | ||
236 | + { | ||
237 | + "type":"text", | ||
238 | + "text": "<<<헤드라인>>>\n" + JSON.stringify(obj2.title).replace(/<[^>]*>?/g, '') | ||
239 | + }, | ||
240 | + { | ||
241 | + "type":"text", | ||
242 | + "text": "<<<주요문단>>>\n" + JSON.stringify(obj2.description).replace(/<[^>]*>?/g, '') | ||
243 | + }, | ||
244 | + { | ||
245 | + "type":"text", | ||
246 | + "text": "뉴스 바로가기\n" + JSON.stringify(obj2.link) | ||
247 | + } | ||
248 | + ] | ||
249 | + } | ||
250 | + },(error, response, body) => { | ||
251 | + console.log(body) | ||
252 | + } | ||
253 | + ) | ||
254 | + } else { | ||
255 | + res.status(response.statusCode).end(); | ||
256 | + console.log('error = ' + response.statusCode); | ||
257 | + } | ||
258 | + } else { | ||
259 | + console.log('새로운 소식 없음_min') | ||
260 | + } | ||
261 | + | ||
262 | + }); | ||
263 | + | ||
264 | + | ||
265 | + }; | ||
266 | + | ||
267 | + | ||
268 | +const intervalId_max = setInterval(PushFunc_max, 60000); | ||
269 | +const intervalId_min = setInterval(PushFunc_min, 60000); | ||
270 | + | ||
271 | + | ||
272 | +try { | ||
273 | + const option = { | ||
274 | + ca: fs.readFileSync('/etc/letsencrypt/live/' + domain +'/fullchain.pem'), | ||
275 | + key: fs.readFileSync(path.resolve(process.cwd(), '/etc/letsencrypt/live/' + domain +'/privkey.pem'), 'utf8').toString(), | ||
276 | + cert: fs.readFileSync(path.resolve(process.cwd(), '/etc/letsencrypt/live/' + domain +'/cert.pem'), 'utf8').toString(), | ||
277 | + }; | ||
278 | + | ||
279 | + HTTPS.createServer(option, app).listen(sslport, () => { | ||
280 | + console.log(`[HTTPS] Server is started on port ${sslport}`); | ||
281 | + }); | ||
282 | + } catch (error) { | ||
283 | + console.log('[HTTPS] HTTPS 오류가 발생하였습니다. HTTPS 서버는 실행되지 않습니다.'); | ||
284 | + console.log(error); | ||
285 | + } | ||
286 | + |
-
Please register or login to post a comment