Showing
1 changed file
with
76 additions
and
34 deletions
1 | var express = require('express'); | 1 | var express = require('express'); |
2 | -var app = express(); | 2 | +const request = require('request');const TARGET_URL = 'https://api.line.me/v2/bot/message/reply' |
3 | -var fs = require('fs'); | 3 | +const TOKEN = '6lwsXwectIjTZ1NWp6c1t5l8nXaOjWgfeYfTOWNRRTVftaXMb4eSMj2zeficMr4/FWtyPPBuUB120kxZp58bwDilssdU5Uhc4mWz4kCPfcgiEcVwzXUktbYRE3xKAJU1zdYAsZZvI+87KY8qZvaRngdB04t89/1O/w1cDnyilFU=' |
4 | +const fs = require('fs'); | ||
5 | +const path = require('path'); | ||
6 | +const HTTPS = require('https'); | ||
4 | const fetch = require('node-fetch'); | 7 | const fetch = require('node-fetch'); |
5 | -var bodyParser = require('body-parser'); | 8 | +const domain = "2018102235.oss2021.tk" |
6 | -app.use(bodyParser.urlencoded({ extended: false })); | 9 | +const sslport = 23023; |
10 | +const bodyParser = require('body-parser'); | ||
11 | +var app = express(); | ||
12 | +var command = ''; | ||
7 | app.use(bodyParser.json()); | 13 | app.use(bodyParser.json()); |
14 | +app.post('/hook', function (req, res) { | ||
15 | + | ||
16 | + var eventObj = req.body.events[0]; | ||
17 | + var source = eventObj.source; | ||
18 | + var message = eventObj.message; | ||
19 | + // request log | ||
20 | + console.log('======================', new Date() ,'======================'); | ||
21 | + console.log('[request]', req.body); | ||
22 | + console.log('[request source] ', source); | ||
23 | + console.log('[request message]', message); | ||
24 | + if(message.text === '김프'){ | ||
25 | + command = '김프'; | ||
26 | + } | ||
27 | + else{ | ||
28 | + if(command === '김프'){ | ||
29 | + kimp(eventObj.replyToken, message.text); | ||
30 | + } | ||
31 | + } | ||
8 | 32 | ||
9 | -app.get('/price', function(req, res) { | 33 | + |
10 | - fs.readFile('coin_name.txt', 'utf8', function(err,data){ | 34 | + |
11 | - arr = data.split(","); | 35 | + res.sendStatus(200); |
12 | - // console.log(arr); | 36 | +}); |
13 | - var responseList = new Array(); | 37 | + |
14 | - function print_coin(){ | 38 | +function kimp(replyToken,message){ |
39 | + | ||
40 | + var upbitPrice = 0; | ||
41 | + function korean_price(){ | ||
15 | var promise = new Promise(function(resolve,reject){ | 42 | var promise = new Promise(function(resolve,reject){ |
16 | - for(j=0;j<arr.length/10;j++){ | 43 | + const url = 'https://api.upbit.com/v1/candles/minutes/1?market=KRW-'+message+'&count=1'; |
17 | - for(i=j*10;i<(j+1)*10;i++){ | ||
18 | - (function(i){ | ||
19 | - setTimeout(function(){ | ||
20 | - const url = 'https://api.upbit.com/v1/candles/minutes/1?market='+arr[i]+'&count=1'; | ||
21 | const options = {method: 'GET', headers: {Accept: 'application/json'}}; | 44 | const options = {method: 'GET', headers: {Accept: 'application/json'}}; |
22 | - var coinJson = new Object(); | ||
23 | fetch(url, options) | 45 | fetch(url, options) |
24 | .then(res => res.json()) | 46 | .then(res => res.json()) |
25 | .then(json => { | 47 | .then(json => { |
26 | - coinJson.coin = arr[i]; | 48 | + upbitPrice = json[0].trade_price; |
27 | - coinJson.price = json[0].trade_price; | 49 | + resolve(upbitPrice); |
28 | - responseList.push(coinJson); | ||
29 | - console.log(i) | ||
30 | - if(responseList.length>116){ | ||
31 | - resolve(responseList); | ||
32 | - } | ||
33 | }) | 50 | }) |
34 | .catch(err => console.error('error:' + err)); | 51 | .catch(err => console.error('error:' + err)); |
35 | - },i*110); | ||
36 | - })(i); | ||
37 | - } | ||
38 | - } | ||
39 | - | ||
40 | }); | 52 | }); |
41 | return promise; | 53 | return promise; |
42 | } | 54 | } |
43 | - print_coin().then(function(resList){ | 55 | + korean_price().then(function(upbitPrice){ |
44 | - res.send(resList); | 56 | + request.post( |
57 | + { | ||
58 | + url: TARGET_URL, | ||
59 | + headers: { | ||
60 | + 'Authorization': `Bearer ${TOKEN}` | ||
61 | + }, | ||
62 | + json: { | ||
63 | + "replyToken":replyToken, | ||
64 | + "messages":[ | ||
65 | + { | ||
66 | + "type":"text", | ||
67 | + "text":String(upbitPrice) | ||
68 | + }, | ||
69 | + ] | ||
70 | + } | ||
71 | + },(error, response, body) => { | ||
72 | + console.log(body) | ||
45 | }); | 73 | }); |
46 | - }) | 74 | + res.sendStatus(200); |
47 | -}) | 75 | + }); |
48 | -var server = app.listen(8082); | 76 | +} |
49 | -console.log("Server Created.."); | 77 | + |
78 | + | ||
79 | +try { | ||
80 | + const option = { | ||
81 | + ca: fs.readFileSync('/etc/letsencrypt/live/' + domain +'/fullchain.pem'), | ||
82 | + key: fs.readFileSync(path.resolve(process.cwd(), '/etc/letsencrypt/live/' + domain +'/privkey.pem'), 'utf8').toString(), | ||
83 | + cert: fs.readFileSync(path.resolve(process.cwd(), '/etc/letsencrypt/live/' + domain +'/cert.pem'), 'utf8').toString(), | ||
84 | + }; | ||
50 | 85 | ||
86 | + HTTPS.createServer(option, app).listen(sslport, () => { | ||
87 | + console.log(`[HTTPS] Server is started on port ${sslport}`); | ||
88 | + }); | ||
89 | + } catch (error) { | ||
90 | + console.log('[HTTPS] HTTPS 오류가 발생하였습니다. HTTPS 서버는 실행되지 않습니다.'); | ||
91 | + console.log(error); | ||
92 | + } | ||
... | \ No newline at end of file | ... | \ No newline at end of file | ... | ... |
-
Mentioned in commit 0ca3d2c1
-
Please register or login to post a comment