How to build an Alexa skill to get the latest bitcoin price
Bitcoin has been all the rage recently so I decided to build an Alexa skill that tells you the latest Bitcoin price. It can tell you the price of Bitcoin, Ethereum and Ethereum Classic — with more crypto currencies possible via the API documentation.
What you’ll need:
- Serverless Framework
- AWS CLI
- Node and NPM
How to get going (you can take a look at the Github Repo here):
git clone git@github.com:phanssens1/alexa_crypto_price.git
cd alexa_crypto_price/lambdayarn add node-fetch alexa-sdk aws-sdk
cd ..
sls deploy
That will deploy you the lambda side of things and then you will need to go to the Alexa Developer Portal to do the configuration — I’ll update this blog post later with details on how to do that.
To take you through the main parts of the code, there’s two things to take note of:
fetch(url).then(res => res.json()).then(json => {console.log(json),this.emit(':ask', `The crypto value is ${json.lastPrice}`, 'you hear me?')}).catch(error => {console.log(error);});
The above fetches the Cryptocurrency price data from BTC Markets and converts that into a JSON document and presents the “lastPrice” value — you can find out more about the BTC Market API here.
The second part of the code which is of interest is the use of a switch — this allows the Alexa skill the flexibility of providing the last price of various Cryptos:
switch(cryptoValue) {case 'ethereum' || 'ETH' || 'Ethereum':console.log('switch - ethereum');url = 'https://api.btcmarkets.net/market/ETH/AUD/tick';break;case 'bitcoin' || 'BTC' || 'Bitcoin':console.log('switch - ethereum');url = 'https://api.btcmarkets.net/market/BTC/AUD/tick';break;default:console.log('switch - default');url = 'https://api.btcmarkets.net/market/ETC/AUD/tick';}
Well that’s about all I have on this — I hope you have enjoyed the read and perhaps learnt some interesting things
Peter Hanssens is a Data Engineer / Data Scientist — when he’s not working on making data great again, he’s trying to learn functional programming or get more cloud computing certs.