EC2 Default User

Merge branch 'experiment'

Showing 1 changed file with 81 additions and 34 deletions
...@@ -5,59 +5,81 @@ const BROAD_TARGET_URL = 'https://api.line.me/v2/bot/message/broadcast' ...@@ -5,59 +5,81 @@ 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( 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(
11 { 29 {
12 - url: TARGET_URL, 30 + url: BROAD_TARGET_URL,
13 headers: { 31 headers: {
14 'Authorization': `Bearer ${TOKEN}` 32 'Authorization': `Bearer ${TOKEN}`
15 }, 33 },
16 json: { 34 json: {
17 - "to": `${USER_ID}`, 35 + "messages": [
18 - "messages":[
19 { 36 {
20 - "type":"text", 37 + "type" :"text",
21 - "text":"Hello, user" 38 + "text": "[상한가 소식]"
22 }, 39 },
23 { 40 {
24 "type":"text", 41 "type":"text",
25 - "text":"May I help you?" 42 + "text": "<<<헤드라인>>>\n" + JSON.stringify(obj2.title).replace(/<[^>]*>?/g, '')
26 - }
27 - ]
28 - }
29 - },(error, response, body) => {
30 - console.log(body)
31 - });
32 -*/
33 -
34 -// Multicast User -> 채널에 불특정 다수에게(user_id적힌 사람들에게)
35 -/*
36 -request.post(
37 - {
38 - url: MULTI_TARGET_URL,
39 - headers: {
40 - 'Authorization': `Bearer ${TOKEN}`
41 }, 43 },
42 - json: {
43 - "to": [`${USER_ID}`],
44 - "messages":[
45 { 44 {
46 "type":"text", 45 "type":"text",
47 - "text":"Hello, user" 46 + "text": "<<<주요문단>>>\n" + JSON.stringify(obj2.description).replace(/<[^>]*>?/g, '')
48 }, 47 },
49 { 48 {
50 "type":"text", 49 "type":"text",
51 - "text":"May I help you?" 50 + "text": "뉴스 바로가기\n" + JSON.stringify(obj2.link)
52 } 51 }
53 ] 52 ]
54 } 53 }
55 },(error, response, body) => { 54 },(error, response, body) => {
56 console.log(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 +
57 }); 66 });
58 -*/
59 67
60 -// Broadcast -> 채널 전체 유저에게 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, '')
61 request.post( 83 request.post(
62 { 84 {
63 url: BROAD_TARGET_URL, 85 url: BROAD_TARGET_URL,
...@@ -65,17 +87,42 @@ request.post( ...@@ -65,17 +87,42 @@ request.post(
65 'Authorization': `Bearer ${TOKEN}` 87 'Authorization': `Bearer ${TOKEN}`
66 }, 88 },
67 json: { 89 json: {
68 - "messages":[ 90 + "messages": [
91 + {
92 + "type" :"text",
93 + "text": "[하한가 소식]"
94 + },
95 + {
96 + "type":"text",
97 + "text": "<<<헤드라인>>>\n" + JSON.stringify(obj2.title).replace(/<[^>]*>?/g, '')
98 + },
69 { 99 {
70 "type":"text", 100 "type":"text",
71 - "text":"Hello, user" 101 + "text": "<<<주요문단>>>\n" + JSON.stringify(obj2.description).replace(/<[^>]*>?/g, '')
72 }, 102 },
73 { 103 {
74 "type":"text", 104 "type":"text",
75 - "text":"May I help you?" 105 + "text": "뉴스 바로가기\n" + JSON.stringify(obj2.link)
76 } 106 }
77 ] 107 ]
78 } 108 }
79 },(error, response, body) => { 109 },(error, response, body) => {
80 console.log(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 +
81 }); 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
......