Build a Bitcoin Price Alert with Google Cloud and Telegram
In this article, I am going to share how to build a Telegram bot for Bitcoin price alerts. The code is written in Python. I use the free tier Google Cloud to run the Python code 24/7 for free. The Python code will read the Bitcoin price from the Binance API, and then it will send a message alert to our Telegram account.
Setup Free Tier Google Cloud
First, we have to create and setup a free tier Google Cloud. You can follow the following tutorial. With this setup, we can use Google Cloud for free even after the trial period is over.
Install Miniconda
First, we have to download and install Miniconda. To download the Miniconda installer, we can use wget commad. If this command is not installed yet, you can install it with the following command:.
Download Miniconda installer with the following command:
To install Miniconda, execute the following command:
I installed Miniconda in the following location:
After the installation process is finished, we have to restart the Google VM.
Setup Telegram Bot
Talk with the BotFather on Telegram (https://telegram.me/BotFather) and then create a new bot. You will get a token to access the HTTP API of the bot.
Install Telegram library for Python from the Google VM with the following command:
Configure telegram-send with your bot using the token with the following command. Then, you will be asked for the token. After that, you will be asked to add your bot on Telegram and send it the given password. After finising, you are ready to use telegram-send.
To test the connection with your Telegram account, you can execute this command from the Google VM:
Send Telegram Message from Python
Now, our Google VM is connected to our Telegram account. Next, we can try to send message from a Python program. This is the Python code:
Run this Python code with the following command:
Setup CronJob
CronJob creates jobs on a repeating schedule. We can use CronJob to execute our Python code at regular intervals.
To access CronJob setting, use the following command:
Add this setting in order to execute the code every 1 hour.
Details of the CronJob time format can be studied at crontab.guru.
Get Data from REST API
To get data from REST API, we have to install requests
library for Python:
Use the following code to test the library with Binance API:
Bitcoin Price Alert
This code combines all the functionality to get price data from Binance and then send it to the Telegram account.
After that, we can setup the CronJob to automate the Python code execution, for example:
This figure shows the message in Telegram.
References
Google Cloud Platform (Free Tier) WordPress Setup, https://www.youtube.com/watch?v=v46bRHR1Pq0
Python in the Cloud Part I - How to run Python in the Cloud / Conda environment setup on GCP, https://www.youtube.com/watch?v=lIJlhKrP_SI
crontab guru, https://crontab.guru/
Last updated