Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
altafan committed Jan 6, 2018
1 parent b44bedd commit 2af0edd
Showing 4 changed files with 174 additions and 0 deletions.
18 changes: 18 additions & 0 deletions constants.js
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/'
}
60 changes: 60 additions & 0 deletions helpers.js
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
81 changes: 81 additions & 0 deletions index.js
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
}
})
15 changes: 15 additions & 0 deletions package.json
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"
}
}

0 comments on commit 2af0edd

Please sign in to comment.