'use strict'; const apiURL = 'https://min-api.cryptocompare.com/data/price?fsym=ETH&tsyms=BTC,USD,GBP,ETH'; module.exports.definition = { BTC: 0.1387, USD: 378.43, GBP: 302, ETH: 1 }; module.exports.run = ( mock = false ) => { const time = new Date(); this.runCryptoCurrencyChecker(mock, time); console.log(`CryptoTracker ran at ${time}`); }; module.exports.runCryptoCurrencyChecker = ( mock, time ) => { let request = require('request'); return new Promise(( resolve, reject ) => { request(apiURL, ( err, res, body ) => { if (err) { reject(err); return; } let data = JSON.parse(body); console.log(data); if (!mock) this.sendToSlack(data, time); resolve(data); }); }); }; module.exports.sendToSlack = ( data, time ) => { let request = require('request'); let msg = `1 ETH = ${data.BTC} BTC, or £${data.GBP} / $${data.USD} at ${time}`; const options = { method: 'post', body: { "fallback": msg, "channel": "#ether", "username": "cryptoCurrencyBot", "text": msg, "icon_emoji": ":moneybag:" }, json: true, url: 'https://hooks.slack.com/services/' }; request(options, function( err, resp, body ) { if (resp && !err) console.log('Posted to Slack.'); }); };