EC2 Default User

EDIT : push.js

Showing 1 changed file with 41 additions and 51 deletions
...@@ -5,77 +5,67 @@ const BROAD_TARGET_URL = 'https://api.line.me/v2/bot/message/broadcast' ...@@ -5,77 +5,67 @@ 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 = '';
12 +
13 +// Broadcast -> 채널 전체 유저에게
14 +const PushFunc = function() {
15 + var api_url = 'https://openapi.naver.com/v1/search/news?query=' + encodeURI('상한가 주식'); // json 결과
16 + var options = {
17 + url: api_url,
18 + headers: {'X-Naver-Client-Id':client_id, 'X-Naver-Client-Secret': client_secret}
19 + };
20 + request.get(options, function (error, response, body) {
21 + const obj = JSON.parse(body);
22 + const str = JSON.stringify(obj.items[0]);
23 + const obj2 = JSON.parse(str);
24 + if(headline == '' || headline != JSON.stringify(obj2.title).replace(/<[^>]*>?/g, '')) {
25 + if (!error && response.statusCode == 200) {
26 + headline = JSON.stringify(obj2.title).replace(/<[^>]*>?/g, '')
27 + request.post(
11 { 28 {
12 - url: TARGET_URL, 29 + url: BROAD_TARGET_URL,
13 headers: { 30 headers: {
14 'Authorization': `Bearer ${TOKEN}` 31 'Authorization': `Bearer ${TOKEN}`
15 }, 32 },
16 json: { 33 json: {
17 - "to": `${USER_ID}`, 34 + "messages": [
18 - "messages":[
19 { 35 {
20 - "type":"text", 36 + "type" :"text",
21 - "text":"Hello, user" 37 + "text": "[상한가 소식]"
22 }, 38 },
23 { 39 {
24 "type":"text", 40 "type":"text",
25 - "text":"May I help you?" 41 + "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 }, 42 },
42 - json: {
43 - "to": [`${USER_ID}`],
44 - "messages":[
45 { 43 {
46 "type":"text", 44 "type":"text",
47 - "text":"Hello, user" 45 + "text": "<<<주요문단>>>\n" + JSON.stringify(obj2.description).replace(/<[^>]*>?/g, '')
48 }, 46 },
49 { 47 {
50 "type":"text", 48 "type":"text",
51 - "text":"May I help you?" 49 + "text": "뉴스 바로가기\n" + JSON.stringify(obj2.link)
52 } 50 }
53 ] 51 ]
54 } 52 }
55 },(error, response, body) => { 53 },(error, response, body) => {
56 console.log(body) 54 console.log(body)
57 - });
58 -*/
59 -
60 -// Broadcast -> 채널 전체 유저에게
61 - request.post(
62 - {
63 - url: BROAD_TARGET_URL,
64 - headers: {
65 - 'Authorization': `Bearer ${TOKEN}`
66 - },
67 - json: {
68 - "messages":[
69 - {
70 - "type":"text",
71 - "text":"Hello, user"
72 - },
73 - {
74 - "type":"text",
75 - "text":"May I help you?"
76 } 55 }
77 - ] 56 + )
57 + } else {
58 + res.status(response.statusCode).end();
59 + console.log('error = ' + response.statusCode);
78 } 60 }
79 - },(error, response, body) => { 61 + } else {
80 - console.log(body) 62 + console.log('새로운 소식 없음')
63 + }
64 +
81 }); 65 });
66 +
67 +
68 + };
69 +
70 +
71 +const intervalId = setInterval(PushFunc, 60000);
...\ No newline at end of file ...\ No newline at end of file
......