Hackernoon logoSend Me A Text Message when BTC Hits $30K: A NodeJS Project by@dirk94

Send Me A Text Message when BTC Hits $30K: A NodeJS Project

ScraperBox Hacker Noon profile picture

@dirk94ScraperBox

Undetectable Web Scraping API - Scrape web pages without getting blocked

For a while, nobody in my circle of friends was talking about crypto.

But now, prices are shooting up. And, it was the main conversation topic at my Christmas dinner.

I thought it would be cool to create a NodeJS project that will send an automated text message when BTC reaches $30,000.

Let's dive in!

Getting the current BTC price

First steps first, let's set up a new Node project.

mkdir crypto-notifier
cd crypto-notifier
touch index.js
npm init

Next, I'm going to scrape the current BTC price from coinmarketcap.

Let's install the

axios
package.

npm i axios

Let's update the

index.js
file to get the Coinmarketcap website.

const axios = require('axios')

axios.get('https://coinmarketcap.com/')
.then((response) => {
	console.log(response.data);	
})

For now it just prints the raw html, let's see if it works!

node index.js

// A lot of HTML gibberish

Alright, we are able to get the raw HTML. Now, let's extract the current BTC price from it.

First I install the

jsdom
package.

npm i jsdom

Then, let's update the index.js file to use the jsdom package.

const axios = require('axios')
const jsdom = require("jsdom");
const { JSDOM } = jsdom;

axios.get('https://coinmarketcap.com/')
.then((response) => {
	// Create the JSDom object
	const { document } = (new JSDOM(response.data)).window;

	// The query selector looks complex, but I simply grabbed it using the Chrome dev tools.
	const btcElement = document.querySelector(
		"#__next > div > div.sc-57oli2-0.dEqHl.cmc-body-wrapper > div > div > div.tableWrapper___3utdq > table > tbody > tr:nth-child(1) > td:nth-child(4) > div > a"
	)

	const price = btcElement.textContent.trim();
	console.log("BTC: " + price);
})

And when running this it shows the correct BTC price. πŸ’°

node index.js

BTC: $27,261.65

Checking if the price's greater than 30K

Right now the program just prints the BTC price.

Let's improve it by converting the price to a number and checking if it's greater than 30K.

const priceAsText = btcElement.textContent.trim();

// Convert the price to a number.
const priceAsNumber = parseFloat(
    priceAsText.replace(",", "").replace("$", "")
) 

// Check if it's greater than 30K.
if (priceAsNumber >= 30000) {
    console.log("BTC reached 30K. Sending a text message!")
} else {
    console.log("BTC < 30K. Doing nothing.")
}

And when I run the program it prints out the correct response.

node index.js

BTC < 30K. Doing nothing.

A thing to note is that we are currently scraping Coinmarketcap. If we were to run this program every minute they probably wouldn't be too happy.

They also have an API that you can use, but that's not 100% free. An alternative is to use a scraper API such as scraperbox to avoid getting blocked.

Sending a text message

I'm going to use the messagebird API to send a text message.

First let's install the Messagebird package.

npm install messagebird

Next, let's import the package.

const messagebird = require('messagebird')('YOUR_API_TOKEN');

And, let's send the actual text message when BTC is greater than 30K.

if (priceAsNumber >= 30000) {
    console.log("BTC reached 30K. Sending a text message!")
    messagebird.messages.create({
        originator: '31612345678',
        recipients : [ '31612345678' ],
        body : `FOMO, BTC IS ${priceAsText} Buy Buy Buy!`
    }, function(err, response) {
        if (err) {
            // If something goes wrong, throw the error
            throw err;
        }
        console.log("Text message send")
    })
} else {
    console.log("BTC < 30K. Doing nothing.")
}

Great, now the program will send a text message if BTC hits 30k.

Running the program every minute

There is still one problem. We must run the program every minute to keep checking for price changes.

We can do this a number of ways, but one of the easiest ways is to use

crontabs
!

Simply edit the crontab file with the following command.

crontab -e

And add the crontab to run the program every minute.

* * * * * node /Users/dirk/crypto-notifier/index.js

And that's it, it will now run the program every minute! πŸ”₯

Conclusion

We've set up a program that checks Coinmarketcap every minute. It will send a text message if the BTC price is greater than 30K.

It's not the most practical program, but it was definitely fun to build.

Happy coding! πŸ‘¨β€πŸ’»

ScraperBox Hacker Noon profile picture
by ScraperBox @dirk94. Undetectable Web Scraping API - Scrape web pages without getting blockedRead my stories

Tags

Join Hacker Noon

Create your free account to unlock your custom reading experience.