배지승

고래알람 설정 해제 기능 추가

Showing 1 changed file with 137 additions and 34 deletions
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 = 'ePTcnuOEn0TvohbtMRnbZRo0YqKVUOyjkkZ/jE6qh5VC5dajFgFBuWY+MAzdY8VKybwB34iz2AbFRtM0G9fL0+Xo4U+Lz7uBNpCo5m//COlWZdQsx1Ywmh61NopAomnNjWT41rNV1YyvI9aeX5CKxgdB04t89/1O/w1cDnyilFU='
6 +const USER_ID = 'U2988a9f8583a616c1285db2224662211'
7 +
8 +const TARGET_URL_reply = 'https://api.line.me/v2/bot/message/reply'
9 +const fs = require('fs');
10 +const path = require('path');
11 +const HTTPS = require('https');
12 +const domain = "2018102193.oss2021.tk"
13 +const sslport = 23023;
14 +const bodyParser = require('body-parser');
15 +
1 const express = require('express'); 16 const express = require('express');
2 const app = express(); 17 const app = express();
3 const fetch = require('node-fetch'); 18 const fetch = require('node-fetch');
...@@ -7,20 +22,47 @@ var cursor = '' ...@@ -7,20 +22,47 @@ var cursor = ''
7 url2 = 'https://api.whale-alert.io/v1/transactions?api_key=' + api_key + '&cursor=' + cursor; 22 url2 = 'https://api.whale-alert.io/v1/transactions?api_key=' + api_key + '&cursor=' + cursor;
8 const options = {method: 'GET', headers: {Accept: 'application/json'}}; 23 const options = {method: 'GET', headers: {Accept: 'application/json'}};
9 24
10 -//Initialize cursor 25 +const KR_TIME_DIFF = 9 * 60 * 60 * 1000;
11 var TimeNow = parseInt((new Date()).getTime() / 1000) 26 var TimeNow = parseInt((new Date()).getTime() / 1000)
12 var uts2time = new Date() 27 var uts2time = new Date()
13 28
29 +var PushTimeId;
14 console.log(TimeNow) 30 console.log(TimeNow)
15 -fetch(url2, options) 31 +
16 -.then(res => res.json()) 32 +app.use(bodyParser.json());
17 -.then(json => { 33 +app.post('/hook', function (req, res) {
18 - console.log(json) 34 + var eventObj = req.body.events[0];
19 - cursor = json.cursor 35 + var source = eventObj.source;
20 -}) 36 + var message = eventObj.message;
21 -.catch((err) => { 37 + // request log
22 - console.log('error : inital_fetch:'+err) 38 + console.log('======================', new Date() ,'======================');
23 -}) 39 + console.log('[request]', req.body);
40 + console.log('[request source] ', source);
41 + console.log('[request message]', message);
42 + // if(message.text === '김프'){
43 + // command = '김프';
44 + // }
45 + // else if (message.text === '거래량비율')
46 + // volumePumpingRatioThreeMinutePerWeek(eventObj.replyToken, message.text);
47 + // else{
48 + // if(command === '김프'){
49 + // kimp(eventObj.replyToken, message.text);
50 + // }
51 + // }
52 + //
53 + if(message.text == '고래알람'){
54 + PushTimeId = SetWhaleAlert()
55 + }
56 + else if(message.text == '고래알람해제'){
57 + clearTimeout(PushTimeId)
58 + }
59 + res.sendStatus(200);
60 +});
61 +
62 +
63 +
64 +
65 +
24 66
25 function chk_param(data, query_limit, callback){ 67 function chk_param(data, query_limit, callback){
26 //query_limit (non_int) => return 0; 68 //query_limit (non_int) => return 0;
...@@ -30,16 +72,19 @@ function chk_param(data, query_limit, callback){ ...@@ -30,16 +72,19 @@ function chk_param(data, query_limit, callback){
30 else {return callback(data, query_limit)} 72 else {return callback(data, query_limit)}
31 } 73 }
32 function data_collector(data, limit){ 74 function data_collector(data, limit){
33 - var ret = ''; 75 + var ret = new Array();
34 var str = ''; 76 var str = '';
35 77
36 - ret += '# of Transactions : ' + data.count + '\n' 78 + //ret += '# of Transactions : ' + data.count + '\n'
37 for(var i = 0; i < data.count; i++){ 79 for(var i = 0; i < data.count; i++){
38 - uts2time.setTime(data.transactions[i].timestamp * 1000) 80 + uts2time.setTime(data.transactions[i].timestamp * 1000 + KR_TIME_DIFF)
39 - str = '#' + (i+1)+' \tTime: ' + uts2time.toLocaleString() + '\tChainName: '+data.transactions[i].blockchain+'\tAmount: '+data.transactions[i].amount+' USD' 81 + str = /*'#' + (i+1)+*/'Time: ' + uts2time.toLocaleString() + '\nChainName: '+data.transactions[i].blockchain+'\nAmount: '+data.transactions[i].amount+' USD'
40 if(data.transactions[i].amount > limit){ 82 if(data.transactions[i].amount > limit){
41 //console.log(str) 83 //console.log(str)
42 - ret += '</br>' + str; 84 + // ret += '\n\n' + str;
85 + o ={ 'type' : 'text',
86 + 'text' : str}
87 + ret.push(o)
43 } 88 }
44 else{ 89 else{
45 //console.log(str) 90 //console.log(str)
...@@ -47,24 +92,82 @@ function data_collector(data, limit){ ...@@ -47,24 +92,82 @@ function data_collector(data, limit){
47 } 92 }
48 return ret; 93 return ret;
49 } 94 }
50 -app.get('/whale', function (req, res) { 95 +function SetWhaleAlert(){
51 - console.log('cursor', cursor) 96 + fetch(url2, options)
52 - fu = fetch(url2+cursor, options); 97 + .then(res => res.json())
53 - fu.then(res => res.json())
54 .then(json => { 98 .then(json => {
55 - //API usage limit 99 + console.log(json)
56 - console.log(json) 100 + fetch(url2+json.cursor, options)
57 - if(json.result == 'error'){ 101 + .then(res => res.json())
58 - res.send("Please try later") 102 + .then(json => {
59 - } 103 + console.log(json)
60 - else{ 104 + fetch(url2+json.cursor, options)
61 - cursor = json.cursor 105 + .then(res => res.json())
62 - res.send(chk_param(json, req.query.limit, data_collector)) 106 + .then(json => {
63 - } 107 + console.log(json)
64 - }) 108 + if(json.cursor != undefined){
65 - .catch(err => { 109 + cursor = json.cursor
66 - console.error('error: var:fu:' + err) 110 + }
67 - res.send('ERROR') 111 + })
112 + .catch((err) => {
113 + console.log('error : inital_fetch:'+err)
114 + })
115 + })
116 + .catch((err) => {
117 + console.log('error : inital_fetch:'+err)
118 + })
119 + })
120 + .catch((err) => {
121 + console.log('error : inital_fetch:'+err)
68 }) 122 })
69 -})
70 -var server = app.listen(23023)
...\ No newline at end of file ...\ No newline at end of file
123 + return setInterval(() => {
124 + fetch(url2+cursor, options)
125 + .then(res => res.json())
126 + .then(json => {
127 + //API usage limit
128 + console.log(json)
129 + if(json.result == 'error'){
130 + }
131 + else{
132 + cursor = json.cursor
133 + var msg2user = chk_param(json,5000000, data_collector)
134 + console.log('msg',msg2user)
135 + if(msg2user.length){
136 + var i = 0;
137 + for(i; i < msg2user.length; i++){
138 + request.post(
139 + {
140 + url: TARGET_URL,
141 + headers: {
142 + 'Authorization': `Bearer ${TOKEN}`
143 + },
144 + json: {
145 + "to": `${USER_ID}`,
146 + "messages":[msg2user[i]]
147 + }
148 + },(error, response, body) => {
149 + console.log(body, error)
150 + });
151 + }
152 + }
153 + }
154 + })
155 + .catch(err => {
156 + console.error('error: var:fu:' + err)
157 + })
158 + }, 5000);
159 +}
160 +try {
161 + const option = {
162 + ca: fs.readFileSync('/etc/letsencrypt/live/' + domain +'/fullchain.pem'),
163 + key: fs.readFileSync(path.resolve(process.cwd(), '/etc/letsencrypt/live/' + domain +'/privkey.pem'), 'utf8').toString(),
164 + cert: fs.readFileSync(path.resolve(process.cwd(), '/etc/letsencrypt/live/' + domain +'/cert.pem'), 'utf8').toString(),
165 + };
166 +
167 + HTTPS.createServer(option, app).listen(sslport, () => {
168 + console.log(`[HTTPS] Server is started on port ${sslport}`);
169 + });
170 + } catch (error) {
171 + console.log('[HTTPS] HTTPS 오류가 발생하였습니다. HTTPS 서버는 실행되지 않습니다.');
172 + console.log(error);
173 + }
......