Showing
1 changed file
with
94 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 = ''//Token, userid 입력후 사용 | ||
| 6 | +const USER_ID = '' | ||
| 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 | +var TimeNow = parseInt((new Date()).getTime() / 1000) | ||
| 18 | +var uts2time = new Date() | ||
| 19 | + | ||
| 20 | +console.log(TimeNow) | ||
| 21 | +fetch(url2, options) //커서값(갱신)을 얻기 위한 fetch | ||
| 22 | +.then(res => res.json()) | ||
| 23 | +.then(json => { | ||
| 24 | + console.log(json) | ||
| 25 | + cursor = json.cursor | ||
| 26 | +}) | ||
| 27 | +.catch((err) => { | ||
| 28 | + console.log('error : inital_fetch:'+err) | ||
| 29 | +}) | ||
| 30 | +function chk_param(data, query_limit, callback){ | ||
| 31 | + //query_limit (non_int) => return 0; | ||
| 32 | + //query_limit (int) => return query_limit; | ||
| 33 | + query_limit = parseInt(query_limit) | ||
| 34 | + if(isNaN(query_limit)){ return callback(data, 0)} | ||
| 35 | + else {return callback(data, query_limit)} | ||
| 36 | +} | ||
| 37 | +function data_collector(data, limit){ | ||
| 38 | + var ret = new Array(); | ||
| 39 | + var str = ''; | ||
| 40 | + | ||
| 41 | + //ret += '# of Transactions : ' + data.count + '\n' | ||
| 42 | + for(var i = 0; i < data.count; i++){ | ||
| 43 | + uts2time.setTime(data.transactions[i].timestamp * 1000) | ||
| 44 | + str = /*'#' + (i+1)+*/'Time: ' + uts2time.toLocaleString() + '\nChainName: '+data.transactions[i].blockchain+'\nAmount: '+data.transactions[i].amount+' USD' | ||
| 45 | + if(data.transactions[i].amount > limit){ | ||
| 46 | + //console.log(str) | ||
| 47 | + // ret += '\n\n' + str; | ||
| 48 | + o ={ 'type' : 'text',//message 형태에 맞춰 저장 | ||
| 49 | + 'text' : str} | ||
| 50 | + ret.push(o) | ||
| 51 | + } | ||
| 52 | + else{ | ||
| 53 | + //console.log(str) | ||
| 54 | + } | ||
| 55 | + } | ||
| 56 | + return ret; | ||
| 57 | +} | ||
| 58 | +//5초 간격으로 fetch후 메시지 전송 | ||
| 59 | +setInterval(() => { | ||
| 60 | + fetch(url2+cursor, options) | ||
| 61 | + .then(res => res.json()) | ||
| 62 | + .then(json => { | ||
| 63 | + //API usage limit | ||
| 64 | + console.log(json) | ||
| 65 | + if(json.result == 'error'){ | ||
| 66 | + } | ||
| 67 | + else{ | ||
| 68 | + cursor = json.cursor | ||
| 69 | + var msg2user = chk_param(json, 0, data_collector) //fetch후 message Object 배열 리턴 | ||
| 70 | + console.log('msg',msg2user) | ||
| 71 | + if(msg2user.length){ | ||
| 72 | + var i = 0; | ||
| 73 | + for(i; i < msg2user.length; i++){ | ||
| 74 | + request.post( | ||
| 75 | + { | ||
| 76 | + url: TARGET_URL, | ||
| 77 | + headers: { | ||
| 78 | + 'Authorization': `Bearer ${TOKEN}` | ||
| 79 | + }, | ||
| 80 | + json: { | ||
| 81 | + "to": `${USER_ID}`, | ||
| 82 | + "messages":[msg2user[i]] //5개 초과하면 err나서 하나씩 넣음 | ||
| 83 | + } | ||
| 84 | + },(error, response, body) => { | ||
| 85 | + console.log(body, error) | ||
| 86 | + }); | ||
| 87 | + } | ||
| 88 | + } | ||
| 89 | + } | ||
| 90 | + }) | ||
| 91 | + .catch(err => { | ||
| 92 | + console.error('error: var:fu:' + err) | ||
| 93 | + }) | ||
| 94 | +}, 5000); | ||
| ... | \ No newline at end of file | ... | \ No newline at end of file |
-
Please register or login to post a comment