정홍주

Add Comparing price function

Showing 1 changed file with 50 additions and 6 deletions
var express = require('express');
const lineToken = require('./apiKey.json');
const Token = require('./apiKey.json');
const request = require('request');const TARGET_URL = 'https://api.line.me/v2/bot/message/reply'
const TOKEN = lineToken.line_token;
const TOKEN = Token.line_token;
const binanceKey = Token.binance_key;
const fs = require('fs');
const path = require('path');
const HTTPS = require('https');
const fetch = require('node-fetch');
const rp = require('request-promise');
const domain = "2018102235.oss2021.tk"
const sslport = 23023;
const bodyParser = require('body-parser');
......@@ -31,8 +33,6 @@ app.post('/hook', function (req, res) {
}
}
res.sendStatus(200);
});
......@@ -54,6 +54,47 @@ function kimp(replyToken,message){
return promise;
}
korean_price().then(function(upbitPrice){
compare_price(replyToken, message, upbitPrice);
});
}
function compare_price(replyToken, message, upbitPrice){
var binancePrice = 0;
function binance_price(){
var promise = new Promise(function(resolve, reject){
const url = 'https://api.binance.com/api/v3/ticker/price?symbol='+message+'USDT';
const options = {method: 'GET', headers: {Accept: 'application/json'}};
const requestOptions = {
method: 'GET',
uri: 'https://pro-api.coinmarketcap.com/v1/cryptocurrency/listings/latest',
qs: {
'start': '1',
'limit': '10',
'convert': 'KRW'
},
headers: {
'X-CMC_PRO_API_KEY': binanceKey
},
json: true,
gzip: true
};
fetch(url, options)
.then(res => res.json())
.then(json =>
rp(requestOptions).then(response =>{
var i=0;
while(response.data[i].id !== 825){
i = i+1;
}
binancePrice = json.price*response.data[i].quote.KRW.price;
resolve(binancePrice);
})
).catch(err => console.error('error:' + err));
});
return promise;
}
binance_price().then(function(binancePrice){
request.post(
{
url: TARGET_URL,
......@@ -65,8 +106,12 @@ function kimp(replyToken,message){
"messages":[
{
"type":"text",
"text":String(upbitPrice)
"text":"binance : "+String(binancePrice.toFixed())+"원"
},
{
"type":"text",
"text":"Upbit : "+String(upbitPrice)+"원"
}
]
}
},(error, response, body) => {
......@@ -76,7 +121,6 @@ function kimp(replyToken,message){
});
}
try {
const option = {
ca: fs.readFileSync('/etc/letsencrypt/live/' + domain +'/fullchain.pem'),
......