EC2 Default User

EDIT : push.js

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