Showing
2 changed files
with
288 additions
and
0 deletions
app_chatbot.js
0 → 100644
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 express = require('express'); | ||
9 | +const app = express(); | ||
10 | +const fetch = require('node-fetch'); | ||
11 | +const fs_apikey = require('fs'); | ||
12 | +const api_key = fs_apikey.readFileSync('apikey', 'ascii') | ||
13 | +var cursor = '' | ||
14 | +url2 = 'https://api.whale-alert.io/v1/transactions?api_key=' + api_key + '&cursor=' + cursor; | ||
15 | +const options = {method: 'GET', headers: {Accept: 'application/json'}}; | ||
16 | + | ||
17 | +const KR_TIME_DIFF = 9 * 60 * 60 * 1000; | ||
18 | +var TimeNow = parseInt((new Date()).getTime() / 1000) | ||
19 | +var uts2time = new Date() | ||
20 | + | ||
21 | +console.log(TimeNow) | ||
22 | + | ||
23 | +fetch(url2, options) | ||
24 | +.then(res => res.json()) | ||
25 | +.then(json => { | ||
26 | + console.log(json) | ||
27 | + fetch(url2+json.cursor, options) | ||
28 | + .then(res => res.json()) | ||
29 | + .then(json => { | ||
30 | + console.log(json) | ||
31 | + fetch(url2+json.cursor, options) | ||
32 | + .then(res => res.json()) | ||
33 | + .then(json => { | ||
34 | + console.log(json) | ||
35 | + if(json.cursor != undefined){ | ||
36 | + cursor = json.cursor | ||
37 | + } | ||
38 | + }) | ||
39 | + .catch((err) => { | ||
40 | + console.log('error : inital_fetch:'+err) | ||
41 | + }) | ||
42 | + }) | ||
43 | + .catch((err) => { | ||
44 | + console.log('error : inital_fetch:'+err) | ||
45 | + }) | ||
46 | + }) | ||
47 | +.catch((err) => { | ||
48 | + console.log('error : inital_fetch:'+err) | ||
49 | +}) | ||
50 | + | ||
51 | + | ||
52 | +function chk_param(data, query_limit, callback){ | ||
53 | + //query_limit (non_int) => return 0; | ||
54 | + //query_limit (int) => return query_limit; | ||
55 | + query_limit = parseInt(query_limit) | ||
56 | + if(isNaN(query_limit)){ return callback(data, 0)} | ||
57 | + else {return callback(data, query_limit)} | ||
58 | +} | ||
59 | +function data_collector(data, limit){ | ||
60 | + var ret = new Array(); | ||
61 | + var str = ''; | ||
62 | + | ||
63 | + //ret += '# of Transactions : ' + data.count + '\n' | ||
64 | + for(var i = 0; i < data.count; i++){ | ||
65 | + uts2time.setTime(data.transactions[i].timestamp * 1000 + KR_TIME_DIFF) | ||
66 | + str = /*'#' + (i+1)+*/'Time: ' + uts2time.toLocaleString() + '\nChainName: '+data.transactions[i].blockchain+'\nAmount: '+data.transactions[i].amount+' USD' | ||
67 | + if(data.transactions[i].amount > limit){ | ||
68 | + //console.log(str) | ||
69 | + // ret += '\n\n' + str; | ||
70 | + o ={ 'type' : 'text', | ||
71 | + 'text' : str} | ||
72 | + ret.push(o) | ||
73 | + } | ||
74 | + else{ | ||
75 | + //console.log(str) | ||
76 | + } | ||
77 | + } | ||
78 | + return ret; | ||
79 | +} | ||
80 | +setInterval(() => { | ||
81 | + fetch(url2+cursor, options) | ||
82 | + .then(res => res.json()) | ||
83 | + .then(json => { | ||
84 | + //API usage limit | ||
85 | + console.log(json) | ||
86 | + if(json.result == 'error'){ | ||
87 | + } | ||
88 | + else{ | ||
89 | + cursor = json.cursor | ||
90 | + var msg2user = chk_param(json, 1000000, data_collector) | ||
91 | + console.log('msg',msg2user) | ||
92 | + if(msg2user.length){ | ||
93 | + var i = 0; | ||
94 | + for(i; i < msg2user.length; i++){ | ||
95 | + request.post( | ||
96 | + { | ||
97 | + url: TARGET_URL, | ||
98 | + headers: { | ||
99 | + 'Authorization': `Bearer ${TOKEN}` | ||
100 | + }, | ||
101 | + json: { | ||
102 | + "to": `${USER_ID}`, | ||
103 | + "messages":[msg2user[i]] | ||
104 | + } | ||
105 | + },(error, response, body) => { | ||
106 | + console.log(body, error) | ||
107 | + }); | ||
108 | + } | ||
109 | + } | ||
110 | + } | ||
111 | + }) | ||
112 | + .catch(err => { | ||
113 | + console.error('error: var:fu:' + err) | ||
114 | + }) | ||
115 | +}, 5000); | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
app_whale.js
0 → 100644
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 | + | ||
16 | +const express = require('express'); | ||
17 | +const app = express(); | ||
18 | +const fetch = require('node-fetch'); | ||
19 | +const fs_apikey = require('fs'); | ||
20 | +const api_key = fs_apikey.readFileSync('apikey', 'ascii') | ||
21 | +var cursor = '' | ||
22 | +url2 = 'https://api.whale-alert.io/v1/transactions?api_key=' + api_key + '&cursor=' + cursor; | ||
23 | +const options = {method: 'GET', headers: {Accept: 'application/json'}}; | ||
24 | + | ||
25 | +const KR_TIME_DIFF = 9 * 60 * 60 * 1000; | ||
26 | +var TimeNow = parseInt((new Date()).getTime() / 1000) | ||
27 | +var uts2time = new Date() | ||
28 | + | ||
29 | +var PushTimeId; | ||
30 | +console.log(TimeNow) | ||
31 | + | ||
32 | +app.use(bodyParser.json()); | ||
33 | +app.post('/hook', function (req, res) { | ||
34 | + var eventObj = req.body.events[0]; | ||
35 | + var source = eventObj.source; | ||
36 | + var message = eventObj.message; | ||
37 | + // request log | ||
38 | + console.log('======================', new Date() ,'======================'); | ||
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 | + | ||
66 | + | ||
67 | +function chk_param(data, query_limit, callback){ | ||
68 | + //query_limit (non_int) => return 0; | ||
69 | + //query_limit (int) => return query_limit; | ||
70 | + query_limit = parseInt(query_limit) | ||
71 | + if(isNaN(query_limit)){ return callback(data, 0)} | ||
72 | + else {return callback(data, query_limit)} | ||
73 | +} | ||
74 | +function data_collector(data, limit){ | ||
75 | + var ret = new Array(); | ||
76 | + var str = ''; | ||
77 | + | ||
78 | + //ret += '# of Transactions : ' + data.count + '\n' | ||
79 | + for(var i = 0; i < data.count; i++){ | ||
80 | + uts2time.setTime(data.transactions[i].timestamp * 1000 + KR_TIME_DIFF) | ||
81 | + str = /*'#' + (i+1)+*/'Time: ' + uts2time.toLocaleString() + '\nChainName: '+data.transactions[i].blockchain+'\nAmount: '+data.transactions[i].amount+' USD' | ||
82 | + if(data.transactions[i].amount > limit){ | ||
83 | + //console.log(str) | ||
84 | + // ret += '\n\n' + str; | ||
85 | + o ={ 'type' : 'text', | ||
86 | + 'text' : str} | ||
87 | + ret.push(o) | ||
88 | + } | ||
89 | + else{ | ||
90 | + //console.log(str) | ||
91 | + } | ||
92 | + } | ||
93 | + return ret; | ||
94 | +} | ||
95 | +function SetWhaleAlert(){ | ||
96 | + fetch(url2, options) | ||
97 | + .then(res => res.json()) | ||
98 | + .then(json => { | ||
99 | + console.log(json) | ||
100 | + fetch(url2+json.cursor, options) | ||
101 | + .then(res => res.json()) | ||
102 | + .then(json => { | ||
103 | + console.log(json) | ||
104 | + fetch(url2+json.cursor, options) | ||
105 | + .then(res => res.json()) | ||
106 | + .then(json => { | ||
107 | + console.log(json) | ||
108 | + if(json.cursor != undefined){ | ||
109 | + cursor = json.cursor | ||
110 | + } | ||
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) | ||
122 | + }) | ||
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 | + } |
-
Please register or login to post a comment