EC2 Default User

Delete : No longer needed .js

1 -const request = require('request');
2 -const TARGET_URL = 'https://api.line.me/v2/bot/message/push'
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'
5 -const TOKEN = '채널 토큰으로 교체'
6 -const USER_ID = '사용자의 ID, 메세지 수신 시에 확인할 수 있음'
7 -
8 -var client_id = 'naver client_id';
9 -var client_secret = 'naver client_secret';
10 -
11 -var headline_max = '';
12 -var headline_min = '';
13 -
14 -// Broadcast -> 채널 전체 유저에게
15 -const PushFunc_max = function() {
16 - var api_url = 'https://openapi.naver.com/v1/search/news?query=' + encodeURI('상한가 주식'); // json 결과
17 - var options = {
18 - url: api_url,
19 - headers: {'X-Naver-Client-Id':client_id, 'X-Naver-Client-Secret': client_secret}
20 - };
21 - request.get(options, function (error, response, body) {
22 - const obj = JSON.parse(body);
23 - const str = JSON.stringify(obj.items[0]);
24 - const obj2 = JSON.parse(str);
25 - if(headline_max == '' || headline_max != JSON.stringify(obj2.title).replace(/<[^>]*>?/g, '')) {
26 - if (!error && response.statusCode == 200) {
27 - headline_max = JSON.stringify(obj2.title).replace(/<[^>]*>?/g, '')
28 - request.post(
29 - {
30 - url: BROAD_TARGET_URL,
31 - headers: {
32 - 'Authorization': `Bearer ${TOKEN}`
33 - },
34 - json: {
35 - "messages": [
36 - {
37 - "type" :"text",
38 - "text": "[상한가 소식]"
39 - },
40 - {
41 - "type":"text",
42 - "text": "<<<헤드라인>>>\n" + JSON.stringify(obj2.title).replace(/<[^>]*>?/g, '')
43 - },
44 - {
45 - "type":"text",
46 - "text": "<<<주요문단>>>\n" + JSON.stringify(obj2.description).replace(/<[^>]*>?/g, '')
47 - },
48 - {
49 - "type":"text",
50 - "text": "뉴스 바로가기\n" + JSON.stringify(obj2.link)
51 - }
52 - ]
53 - }
54 - },(error, response, body) => {
55 - console.log(body)
56 - }
57 - )
58 - } else {
59 - res.status(response.statusCode).end();
60 - console.log('error = ' + response.statusCode);
61 - }
62 - } else {
63 - console.log('새로운 소식 없음_max')
64 - }
65 -
66 - });
67 -
68 -
69 - };
70 -const PushFunc_min = function() {
71 - var api_url = 'https://openapi.naver.com/v1/search/news?query=' + encodeURI('하한가 주식'); // json 결과
72 - var options = {
73 - url: api_url,
74 - headers: {'X-Naver-Client-Id':client_id, 'X-Naver-Client-Secret': client_secret}
75 - };
76 - request.get(options, function (error, response, body) {
77 - const obj = JSON.parse(body);
78 - const str = JSON.stringify(obj.items[0]);
79 - const obj2 = JSON.parse(str);
80 - if(headline_min == '' || headline_min != JSON.stringify(obj2.title).replace(/<[^>]*>?/g, '')) {
81 - if (!error && response.statusCode == 200) {
82 - headline_min = JSON.stringify(obj2.title).replace(/<[^>]*>?/g, '')
83 - request.post(
84 - {
85 - url: BROAD_TARGET_URL,
86 - headers: {
87 - 'Authorization': `Bearer ${TOKEN}`
88 - },
89 - json: {
90 - "messages": [
91 - {
92 - "type" :"text",
93 - "text": "[하한가 소식]"
94 - },
95 - {
96 - "type":"text",
97 - "text": "<<<헤드라인>>>\n" + JSON.stringify(obj2.title).replace(/<[^>]*>?/g, '')
98 - },
99 - {
100 - "type":"text",
101 - "text": "<<<주요문단>>>\n" + JSON.stringify(obj2.description).replace(/<[^>]*>?/g, '')
102 - },
103 - {
104 - "type":"text",
105 - "text": "뉴스 바로가기\n" + JSON.stringify(obj2.link)
106 - }
107 - ]
108 - }
109 - },(error, response, body) => {
110 - console.log(body)
111 - }
112 - )
113 - } else {
114 - res.status(response.statusCode).end();
115 - console.log('error = ' + response.statusCode);
116 - }
117 - } else {
118 - console.log('새로운 소식 없음_min')
119 - }
120 -
121 - });
122 -
123 -
124 - };
125 -
126 -
127 -const intervalId_max = setInterval(PushFunc_max, 60000);
128 -const intervalId_min = setInterval(PushFunc_min, 60000);
...\ 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 TOKEN = '채널 토큰으로 변경'
5 -const fs = require('fs');
6 -const path = require('path');
7 -const HTTPS = require('https');
8 -const domain = "도메인 변경"
9 -const sslport = 23023;
10 -
11 -var client_id = 'YOUR_CLIENT_ID';
12 -var client_secret = 'YOUR_CLIENT_SECRET';
13 -
14 -var newsOn = false;
15 -var newsOff = true;
16 -
17 -const bodyParser = require('body-parser');
18 -var app = express();
19 -app.use(bodyParser.json());
20 -app.post('/hook', function (req, res) {
21 -
22 - var eventObj = req.body.events[0];
23 - var source = eventObj.source;
24 - var message = eventObj.message;
25 -
26 - // request log
27 - console.log('======================', new Date() ,'======================');
28 - console.log('[request]', req.body);
29 - console.log('[request source] ', eventObj.source);
30 - console.log('[request message]', eventObj.message);
31 -
32 - if (message.text == 'news on') {
33 - newsOn = true;
34 - newsOff = false;
35 - request.post(
36 - {
37 - url: TARGET_URL,
38 - headers: {
39 - 'Authorization': `Bearer ${TOKEN}`
40 - },
41 - json: {
42 - "replyToken":eventObj.replyToken,
43 - "messages":[
44 - {
45 - "type":"text",
46 - "text":"[news On] 키워드를 입력 하세요."
47 - }
48 - ]
49 - }
50 - },(error, response, body) => {
51 - console.log(body)
52 - });
53 - } else if ( message.text == 'news off') {
54 - newsOn = false;
55 - newsOff = true;
56 - request.post(
57 - {
58 - url: TARGET_URL,
59 - headers: {
60 - 'Authorization': `Bearer ${TOKEN}`
61 - },
62 - json: {
63 - "replyToken":eventObj.replyToken,
64 - "messages":[
65 - {
66 - "type":"text",
67 - "text":"[news Off] 뉴스를 종료합니다."
68 - }
69 - ]
70 - }
71 - },(error, response, body) => {
72 - console.log(body)
73 - });
74 - }
75 -
76 - if (newsOn & message.text != 'help' & message.text != 'news on') {
77 - var api_url = 'https://openapi.naver.com/v1/search/news?query=' + encodeURI(message.text); // json 결과
78 - var options = {
79 - url: api_url,
80 - headers: {'X-Naver-Client-Id':client_id, 'X-Naver-Client-Secret': client_secret}
81 - };
82 - request.get(options, function (error, response, body) {
83 - const obj = JSON.parse(body);
84 - const str = JSON.stringify(obj.items[0]);
85 - const obj2 = JSON.parse(str);
86 - if (!error && response.statusCode == 200) {
87 - request.post(
88 - {
89 - url: TARGET_URL,
90 - headers: {
91 - 'Authorization': `Bearer ${TOKEN}`
92 - },
93 - json: {
94 - "replyToken":eventObj.replyToken,
95 - "messages": [
96 - {
97 - "type":"text",
98 - "text": "<<<헤드라인>>>\n" + JSON.stringify(obj2.title).replace(/<[^>]*>?/g, '')
99 - },
100 - {
101 - "type":"text",
102 - "text": "<<<주요문단>>>\n" + JSON.stringify(obj2.description).replace(/<[^>]*>?/g, '')
103 - },
104 - {
105 - "type":"text",
106 - "text": "뉴스 바로가기\n" + JSON.stringify(obj2.link)
107 - }
108 - ]
109 - }
110 - },(error, response, body) => {
111 - console.log(body)
112 - }
113 - )
114 - } else {
115 - res.status(response.statusCode).end();
116 - console.log('error = ' + response.statusCode);
117 - }
118 - });
119 -
120 - }
121 -
122 - if (message.text == 'help') {
123 - request.post(
124 - {
125 - url: TARGET_URL,
126 - headers: {
127 - 'Authorization': `Bearer ${TOKEN}`
128 - },
129 - json: {
130 - "replyToken":eventObj.replyToken,
131 - "messages":[
132 - {
133 - "type":"text",
134 - "text":"*****************\
135 - \n반갑습니다. 주식 news 알리미 입니다.\
136 - \n1. 뉴스 검색 기능 활성화\
137 - \n\'news on\' 입력\
138 - \n2. 뉴스 검색 기능 비활성화\
139 - \n\'news off\' 입력\
140 - \n*****************"
141 - }
142 - ]
143 - }
144 - },(error, response, body) => {
145 - console.log(body)
146 - });
147 - }
148 -
149 -
150 - res.sendStatus(200);
151 -});
152 -
153 -try {
154 - const option = {
155 - ca: fs.readFileSync('/etc/letsencrypt/live/' + domain +'/fullchain.pem'),
156 - key: fs.readFileSync(path.resolve(process.cwd(), '/etc/letsencrypt/live/' + domain +'/privkey.pem'), 'utf8').toString(),
157 - cert: fs.readFileSync(path.resolve(process.cwd(), '/etc/letsencrypt/live/' + domain +'/cert.pem'), 'utf8').toString(),
158 - };
159 -
160 - HTTPS.createServer(option, app).listen(sslport, () => {
161 - console.log(`[HTTPS] Server is started on port ${sslport}`);
162 - });
163 - } catch (error) {
164 - console.log('[HTTPS] HTTPS 오류가 발생하였습니다. HTTPS 서버는 실행되지 않습니다.');
165 - console.log(error);
166 - }
167 -