Attainu Project

Bitcoin Price Notifications – Project by Varsha Konin

Project titleBitcoin Price Notifications

Introduction: Bitcoin price is a fickle thing. You never really know where it’s going to be at the end of the day. So, instead of constantly checking various sites for the latest updates, let’s make a Python project to do the work for you.

Approach: For this, we’re going to use the popular automation website IFTTT. IFTTT (“if this, then that”) is a web service that bridges the gap between different apps and devices.

We’re going to create two IFTTT applets:

  1. One for emergency notification when Bitcoin price falls under a certain threshold; and
  2. Another for regular Telegram updates on the Bitcoin price.

Both will be triggered by our Python app which will consume the data from the Coinmarketcap API.

An IFTTT applet is composed of two parts: a trigger and an action.

The trigger will be a webhook service provided by IFTTT. You can think of webhooks as “user-defined HTTP callbacks”

Our Python project will make an HTTP request to the webhook URL which will trigger an action.

 

Technical Details:

We can start by getting the latest price from the Coinmarketcap API in the Python console:

First, we have to import the requests module and define the bitcoin_api_url variable which contains the Coinmarketcap API URL for Bitcoin.

Next, we send an HTTP GET request to the URL using the requests.get() function and save the response. Since the API returns a JSON response, we can convert it to a Python object by calling the .json() function on the response

Sending a Test IFTTT Notification

Now we can move onto the IFTTT side of things. To use IFTTT you’ll first need to set up a new account and install their mobile app (if you want to receive phone notifications from your Python app). Once you set that up, we’re going to create a new IFTTT applet for testing purposes.

To create a new test applet follow these steps:

  1. Click on the big “this” button
  2. Search for the “webhooks” service and select the “Receive a web request” trigger
  3. Let’s name the event “bitcoin_daily_updates
  4. Now select the big “that” button
  5. Search for the “notifications” service and select the “Send a notification from the IFTTT app”
  6. Change the message to I just triggered my first IFTTT action! and click on “Create action”
  7. Next up, you’ll need to substitute the {event} part with whatever name you gave our event in step 3, when you created the applet. The {your-IFTTT-key} part is already populated with your IFTTT key.
  8. Now copy the webhook URL and start another Python console. Again we import the requests module and define the webhook URL variable. Now we just have to send an HTTP POST request to the IFTTT webhook URL using the requests.post()

After running the last line you should see a notification on your phone.

Creating IFTTT Applets

One for emergency Bitcoin price notifications and one for regular updates

Emergency bitcoin price notification applet:

  1. Choose the “webhooks” service and select the “Receive a web request” trigger
  2. Name the event bitcoin_price_emergency
  3. For the action select the “Notifications” service and select the “Send a rich notification from the IFTTT app” action
  4. Give it a title, like “Bitcoin price emergency!”
  5. Set the message to Bitcoin price is at ${{Value1}}. Buy or sell now!
  6. Create the action and finish setting up the applet

Regular price updates applet:

  1. Again choose the “webhooks” service and select the “Receive a web request” trigger.
  2. Name the event bitcoin_daily_updates
  3. For the action select the “Telegram” service and select the “Send message” action.
  4. Create the action and finish with the applet.

Translate the Python console sessions into two functions that will return the latest Bitcoin price and post to the IFTTT webhook respectively

The get_latest_bitcoin_price is where we have to convert the price from a string to a floating point number.

The post_ifttt_webhook takes in two parameters: event and value.

The event parameter corresponds to whatever event name we gave to our trigger when setting up the IFTTT applet. Also, the IFTTT webhooks allow us to send additional data along with the request as JSON-formatted data.

That’s why we need the value parameter: When setting up our applets we left a {{Value1}} tag in our message fields. This tag is replaced with the ‘value1’ text from the JSON payload.

The requests.post() function allows us to send additional JSON data just by adding the json keyword.

Now we can move on to the core of our app in the main function. It will consist of a while True loop since we want our app to run forever. In the loop we will call the Coinmarketcap API to get the latest Bitcoin price and record the current date and time.

Based on the current price we will decide if we want to send an emergency notification. For our regular Telegram updates we will also append the current price and date to a bitcoin_history list. Once the list reaches a certain number of items (e.g. 5) we will format the items, send the update to Telegram, and reset the history for future updates.

The  format_bitcoin_history function. It takes the bitcoin_history as an argument and formats it using some of the basic HTML tags allowed by Telegram, like <br>, <b>, <i>, and so on.

This is what the end result should look like on your phone:

 

DEMO:

Leave a Comment

Your email address will not be published.