정홍주

Add Comparing price function

Showing 1 changed file with 51 additions and 7 deletions
1 var express = require('express'); 1 var express = require('express');
2 -const lineToken = require('./apiKey.json'); 2 +const Token = require('./apiKey.json');
3 const request = require('request');const TARGET_URL = 'https://api.line.me/v2/bot/message/reply' 3 const request = require('request');const TARGET_URL = 'https://api.line.me/v2/bot/message/reply'
4 -const TOKEN = lineToken.line_token; 4 +const TOKEN = Token.line_token;
5 +const binanceKey = Token.binance_key;
5 const fs = require('fs'); 6 const fs = require('fs');
6 const path = require('path'); 7 const path = require('path');
7 const HTTPS = require('https'); 8 const HTTPS = require('https');
8 const fetch = require('node-fetch'); 9 const fetch = require('node-fetch');
10 +const rp = require('request-promise');
9 const domain = "2018102235.oss2021.tk" 11 const domain = "2018102235.oss2021.tk"
10 const sslport = 23023; 12 const sslport = 23023;
11 const bodyParser = require('body-parser'); 13 const bodyParser = require('body-parser');
...@@ -31,8 +33,6 @@ app.post('/hook', function (req, res) { ...@@ -31,8 +33,6 @@ app.post('/hook', function (req, res) {
31 } 33 }
32 } 34 }
33 35
34 -
35 -
36 res.sendStatus(200); 36 res.sendStatus(200);
37 }); 37 });
38 38
...@@ -54,6 +54,47 @@ function kimp(replyToken,message){ ...@@ -54,6 +54,47 @@ function kimp(replyToken,message){
54 return promise; 54 return promise;
55 } 55 }
56 korean_price().then(function(upbitPrice){ 56 korean_price().then(function(upbitPrice){
57 + compare_price(replyToken, message, upbitPrice);
58 + });
59 +}
60 +
61 +function compare_price(replyToken, message, upbitPrice){
62 + var binancePrice = 0;
63 + function binance_price(){
64 + var promise = new Promise(function(resolve, reject){
65 + const url = 'https://api.binance.com/api/v3/ticker/price?symbol='+message+'USDT';
66 + const options = {method: 'GET', headers: {Accept: 'application/json'}};
67 + const requestOptions = {
68 + method: 'GET',
69 + uri: 'https://pro-api.coinmarketcap.com/v1/cryptocurrency/listings/latest',
70 + qs: {
71 + 'start': '1',
72 + 'limit': '10',
73 + 'convert': 'KRW'
74 + },
75 + headers: {
76 + 'X-CMC_PRO_API_KEY': binanceKey
77 + },
78 + json: true,
79 + gzip: true
80 + };
81 + fetch(url, options)
82 + .then(res => res.json())
83 + .then(json =>
84 + rp(requestOptions).then(response =>{
85 + var i=0;
86 + while(response.data[i].id !== 825){
87 + i = i+1;
88 + }
89 + binancePrice = json.price*response.data[i].quote.KRW.price;
90 + resolve(binancePrice);
91 + })
92 + ).catch(err => console.error('error:' + err));
93 + });
94 + return promise;
95 + }
96 +
97 + binance_price().then(function(binancePrice){
57 request.post( 98 request.post(
58 { 99 {
59 url: TARGET_URL, 100 url: TARGET_URL,
...@@ -65,18 +106,21 @@ function kimp(replyToken,message){ ...@@ -65,18 +106,21 @@ function kimp(replyToken,message){
65 "messages":[ 106 "messages":[
66 { 107 {
67 "type":"text", 108 "type":"text",
68 - "text":String(upbitPrice) 109 + "text":"binance : "+String(binancePrice.toFixed())+"원"
69 }, 110 },
111 + {
112 + "type":"text",
113 + "text":"Upbit : "+String(upbitPrice)+"원"
114 + }
70 ] 115 ]
71 } 116 }
72 },(error, response, body) => { 117 },(error, response, body) => {
73 console.log(body) 118 console.log(body)
74 }); 119 });
75 res.sendStatus(200); 120 res.sendStatus(200);
76 - }); 121 + });
77 } 122 }
78 123
79 -
80 try { 124 try {
81 const option = { 125 const option = {
82 ca: fs.readFileSync('/etc/letsencrypt/live/' + domain +'/fullchain.pem'), 126 ca: fs.readFileSync('/etc/letsencrypt/live/' + domain +'/fullchain.pem'),
......