Created
June 14, 2017 10:08
-
-
Save chrisquinnr/c23379ee360a54b92df05ce5ff8b84da to your computer and use it in GitHub Desktop.
Serverless function to grab cryptocurrency price data and post to Slack
This file contains hidden or 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
| '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/<YOUR-WEBHOOK-URL>' | |
| }; | |
| request(options, function( err, resp, body ) { | |
| if (resp && !err) console.log('Posted to Slack.'); | |
| }); | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment