EC2 Default User

Merge branch 'experiment'

Showing 1 changed file with 117 additions and 70 deletions
...@@ -5,77 +5,124 @@ const BROAD_TARGET_URL = 'https://api.line.me/v2/bot/message/broadcast' ...@@ -5,77 +5,124 @@ const BROAD_TARGET_URL = 'https://api.line.me/v2/bot/message/broadcast'
5 const TOKEN = '채널 토큰으로 교체' 5 const TOKEN = '채널 토큰으로 교체'
6 const USER_ID = '사용자의 ID, 메세지 수신 시에 확인할 수 있음' 6 const USER_ID = '사용자의 ID, 메세지 수신 시에 확인할 수 있음'
7 7
8 -//Single User -> 특정 인물 한명에게만 user_id 8 +var client_id = 'naver client_id';
9 -/* 9 +var client_secret = 'naver client_secret';
10 -request.post(
11 - {
12 - url: TARGET_URL,
13 - headers: {
14 - 'Authorization': `Bearer ${TOKEN}`
15 - },
16 - json: {
17 - "to": `${USER_ID}`,
18 - "messages":[
19 - {
20 - "type":"text",
21 - "text":"Hello, user"
22 - },
23 - {
24 - "type":"text",
25 - "text":"May I help you?"
26 - }
27 - ]
28 - }
29 - },(error, response, body) => {
30 - console.log(body)
31 - });
32 -*/
33 10
34 -// Multicast User -> 채널에 불특정 다수에게(user_id적힌 사람들에게) 11 +var headline_max = '';
35 -/* 12 +var headline_min = '';
36 -request.post(
37 - {
38 - url: MULTI_TARGET_URL,
39 - headers: {
40 - 'Authorization': `Bearer ${TOKEN}`
41 - },
42 - json: {
43 - "to": [`${USER_ID}`],
44 - "messages":[
45 - {
46 - "type":"text",
47 - "text":"Hello, user"
48 - },
49 - {
50 - "type":"text",
51 - "text":"May I help you?"
52 - }
53 - ]
54 - }
55 - },(error, response, body) => {
56 - console.log(body)
57 - });
58 -*/
59 13
60 // Broadcast -> 채널 전체 유저에게 14 // Broadcast -> 채널 전체 유저에게
61 - request.post( 15 +const PushFunc_max = function() {
62 - { 16 + var api_url = 'https://openapi.naver.com/v1/search/news?query=' + encodeURI('상한가 주식'); // json 결과
63 - url: BROAD_TARGET_URL, 17 + var options = {
64 - headers: { 18 + url: api_url,
65 - 'Authorization': `Bearer ${TOKEN}` 19 + headers: {'X-Naver-Client-Id':client_id, 'X-Naver-Client-Secret': client_secret}
66 - }, 20 + };
67 - json: { 21 + request.get(options, function (error, response, body) {
68 - "messages":[ 22 + const obj = JSON.parse(body);
69 - { 23 + const str = JSON.stringify(obj.items[0]);
70 - "type":"text", 24 + const obj2 = JSON.parse(str);
71 - "text":"Hello, user" 25 + if(headline_max == '' || headline_max != JSON.stringify(obj2.title).replace(/<[^>]*>?/g, '')) {
72 - }, 26 + if (!error && response.statusCode == 200) {
73 - { 27 + headline_max = JSON.stringify(obj2.title).replace(/<[^>]*>?/g, '')
74 - "type":"text", 28 + request.post(
75 - "text":"May I help you?" 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)
76 } 111 }
77 - ]
78 - }
79 - },(error, response, body) => {
80 - console.log(body)
81 - });
...\ No newline at end of file ...\ No newline at end of file
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
......