From 2af0edd4ebb447e8b61f3fa32053c9459cde4a76 Mon Sep 17 00:00:00 2001 From: altafan Date: Sun, 7 Jan 2018 00:19:47 +0100 Subject: [PATCH] first commit --- constants.js | 18 ++++++++++++ helpers.js | 60 ++++++++++++++++++++++++++++++++++++++ index.js | 81 ++++++++++++++++++++++++++++++++++++++++++++++++++++ package.json | 15 ++++++++++ 4 files changed, 174 insertions(+) create mode 100644 constants.js create mode 100644 helpers.js create mode 100644 index.js create mode 100644 package.json diff --git a/constants.js b/constants.js new file mode 100644 index 0000000..48b25be --- /dev/null +++ b/constants.js @@ -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/' +} \ No newline at end of file diff --git a/helpers.js b/helpers.js new file mode 100644 index 0000000..c6c1ecc --- /dev/null +++ b/helpers.js @@ -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 \ No newline at end of file diff --git a/index.js b/index.js new file mode 100644 index 0000000..aecbf6d --- /dev/null +++ b/index.js @@ -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 + } +}) \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 0000000..0f99f0e --- /dev/null +++ b/package.json @@ -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" + } +}