-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
4 changed files
with
174 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
module.exports.symbol = { | ||
'eur': '€', | ||
'usd': '$', | ||
'gbp': '£', | ||
} | ||
|
||
module.exports.pair = { | ||
'eur': 'XXBTZEUR', | ||
'usd': 'XXBTZUSD', | ||
'gbp': 'XXBTZGBP', | ||
} | ||
|
||
module.exports.url = { | ||
'coinbase': 'https://api.coinbase.com/v2/prices/', | ||
'kraken': 'https://api.kraken.com/0/public/Ticker', | ||
'coinmarketcap': 'https://api.coinmarketcap.com/v1/ticker/', | ||
'bitcoinaverage': 'https://apiv2.bitcoinaverage.com/indices/global/ticker/' | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
const axios = require('axios') | ||
const { symbol, pair, url } = require('./constants') | ||
|
||
const formatResponse = (value, currency) => `1 btc is equal to ${symbol[currency]} ${parseFloat(value).toFixed(2)}` | ||
|
||
const rawRequest = (exchange, currency) => { | ||
switch(exchange) { | ||
case 'kraken': | ||
return new Promise((resolve, reject) => { | ||
axios.post( | ||
url[exchange], | ||
{ | ||
pair: pair[currency], | ||
headers: { 'Api-Key': config.apiKey, 'Api-Sign': config.apiSecret } | ||
} | ||
) | ||
.then(({ data }) => resolve(data.result ? formatResponse(data.result[pair[currency]].c[0], currency) : 'An error occured, retry')) | ||
.catch(error => { | ||
console.log(error) | ||
reject('Timeout error, please retry') | ||
}) | ||
}) | ||
|
||
case 'coinbase': | ||
return new Promise((resolve, reject) => { | ||
axios.get(`${url[exchange]}BTC-${currency.toUpperCase()}/spot`, { headers: { 'CB-VERSION': '2015-04-08' } }) | ||
.then(({ data }) => resolve(data && data.data && data.data.amount ? formatResponse(data.data.amount, currency) : 'An error occured, retry')) | ||
.catch(error => { | ||
console.log(error) | ||
reject('Timeout error, please retry') | ||
}) | ||
}) | ||
|
||
case 'bitcoinaverage': | ||
return new Promise((resolve, reject) => { | ||
axios.get(`${url[exchange]}BTC${currency.toUpperCase()}`) | ||
.then(({ data }) => resolve(data.last ? formatResponse(data.last, currency) : 'An error occured, retry')) | ||
.catch(error => { | ||
console.log(error) | ||
reject('Timeout error, please retry') | ||
}) | ||
}) | ||
|
||
default: | ||
return new Promise((resolve, reject) => { | ||
axios.get(`${url[exchange]}?convert=${currency.toUpperCase()}`) | ||
.then(({ data }) => { | ||
const rawResponse = data.find(elem => elem.symbol.toLowerCase() === 'btc') | ||
|
||
resolve(rawResponse && rawResponse[`price_${currency}`] ? formatResponse(rawResponse[`price_${currency}`], currency) : 'An error occured, retry') | ||
}) | ||
.catch(error => { | ||
console.log(error) | ||
reject('Timeout error, please retry') | ||
}) | ||
}) | ||
} | ||
} | ||
|
||
module.exports = rawRequest |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
const config = require('./config.json') | ||
const rawRequest = require('./helpers') | ||
const TelegramBot = require('node-telegram-bot-api') | ||
|
||
const bot = new TelegramBot(config.token, { polling: true }) | ||
|
||
let callbackType, | ||
currency = 'eur', | ||
exchange = 'coinmarketcap' | ||
|
||
bot.onText(/\/which_exchanger/, message => { | ||
bot.sendMessage(message.chat.id, exchange) | ||
}) | ||
|
||
bot.onText(/\/btc/, message => { | ||
rawRequest(exchange, currency) | ||
.then(result => bot.sendMessage(message.chat.id, result)) | ||
.catch(error => bot.sendMessage(message.chat.id, error)) | ||
}) | ||
|
||
bot.onText(/\/exchanger/, message => { | ||
callbackType = 'exchanger' | ||
|
||
bot.sendMessage(message.chat.id, "Select exchanger", { | ||
'reply_markup': { | ||
'inline_keyboard': [ | ||
[{ | ||
'text': 'kraken', | ||
'callback_data': 'kraken' | ||
}], | ||
[{ | ||
'text': 'coinmarketcap', | ||
'callback_data': 'coinmarketcap' | ||
}], | ||
[{ | ||
'text': 'coinbase', | ||
'callback_data': 'coinbase' | ||
}], | ||
[{ | ||
'text': 'bitcoinaverage', | ||
'callback_data': 'bitcoinaverage' | ||
}] | ||
] | ||
} | ||
}) | ||
}) | ||
|
||
bot.onText(/\/currency/, message => { | ||
callbackType = 'currency' | ||
|
||
bot.sendMessage(message.chat.id, "Select currency", { | ||
'reply_markup': { | ||
'inline_keyboard': [ | ||
[{ | ||
'text': 'Euro (EUR)', | ||
'callback_data': 'eur' | ||
}], | ||
[{ | ||
'text': 'US Dollar (USD)', | ||
'callback_data': 'usd' | ||
}], | ||
[{ | ||
'text': 'Pound (GBP)', | ||
'callback_data': 'gbp' | ||
}], | ||
] | ||
} | ||
}) | ||
}) | ||
|
||
bot.on('callback_query', query => { | ||
if (callbackType === 'exchanger') { | ||
bot.answerCallbackQuery(query.id, { text: 'Exchanger updated!' }) | ||
exchange = query.data | ||
} | ||
|
||
if (callbackType === 'currency') { | ||
bot.answerCallbackQuery(query.id, { text: 'Currency updated!' }) | ||
currency = query.data | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"name": "test", | ||
"version": "1.0.0", | ||
"description": "", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"author": "", | ||
"license": "ISC", | ||
"dependencies": { | ||
"axios": "^0.17.1", | ||
"node-telegram-bot-api": "^0.30.0" | ||
} | ||
} |