Last active
March 18, 2018 21:45
-
-
Save MarcL/257f0bb85a816ad18269dc7809b5c454 to your computer and use it in GitHub Desktop.
Express endpoint to perform CryptoCompare request and Chatfuel response
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
const express = require('express'); | |
const app = express(); | |
const requestPromise = require('request-promise'); | |
// Call this from JSON API like this: | |
// | |
// https://myserver.com/crypto?fromCurrency=gbp&toCurrency=btc&amount=32.67 | |
// | |
app.get('/crypto', (request, response) => { | |
// NOTE: There is no error checking for amount existing and from/to currencies being valid symbols | |
// Assumes a single to and from currency | |
const { fromCurrency, toCurrency, amount } = request.query; | |
// Need to convert amount to a number as it's a string | |
const conversionAmount = Number(amount); | |
// Symbols have to be uppercase | |
const fromCurrencyUpperCase = fromCurrency.toUpperCase(); | |
const toCurrencyUpperCase = toCurrency.toUpperCase(); | |
const options = { | |
uri: `https://min-api.cryptocompare.com/data/price?fsym=${fromCurrencyUpperCase}&tsyms=${toCurrencyUpperCase}`, | |
json: true | |
}; | |
return requestPromise(options) | |
.then((cryptoResponse) => { | |
// Can now get the toCurrency using the uppercase symbol as it will be a key in the response | |
const conversionAmount = cryptoResponse[toCurrencyUpperCase]; | |
// Do the multiplication to determine how much in the new currency | |
const maxDecimalPlaces = 8; // BTC allows 8 decimal places but you might want to change this | |
const convertedAmount = Number(amount * conversionAmount).toFixed(maxDecimalPlaces); | |
const outputString = `Converted ${amount} ${fromCurrency} to ${convertedAmount} ${toCurrency}`; | |
return response.json({ | |
messages: [ | |
{ text: outputString } | |
] | |
}); | |
}); | |
}); | |
const port = process.env.PORT || 8080; | |
app.listen(port, () => { | |
console.log('Your app is listening on port ' + port); | |
}); |
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
{ | |
"//1": "Making sure you have request and request-promise in your package.json", | |
"name": "my-glitch-app", | |
"version": "0.0.1", | |
"description": "What am I about?", | |
"main": "server.js", | |
"scripts": { | |
"start": "node server.js" | |
}, | |
"dependencies": { | |
"express": "^4.16.2", | |
"request": "^2.34", | |
"request-promise": "^4.2.2" | |
}, | |
"engines": { | |
"node": "8.x" | |
}, | |
"repository": { | |
"url": "https://glitch.com/edit/#!/welcome-project" | |
}, | |
"license": "MIT", | |
"keywords": [ | |
"node", | |
"glitch", | |
"express" | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment