Crypto personalised alerts to telegram using python and graphs with docker, influx and grafana.

Prototype0
6 min readDec 12, 2020

Well, i was on a telegram group and someone asked if there was an APP that could do this and i was like, “mmmm… i think not, at least not free”, don’t get me wrong probably there are some out there but the main concern here is that it must be personalised and by that i mean (in crypto at least) the price I bought in and also the time or percentage of that coin movement from where i bough.

So, where to begin…? At first i was like

“oh, this is easy… just a API query, compare, and done!”

but as always in my personal projects i start first and then i plan and because of that the following disclaimer.

DISCLAIMER, there is a lot that can be changed, improved, implement, fails, bugs, etc but this is what i did. Prices are all fakes except for the red one (i know, STAKE you …)

Code Repository

The code can be found at https://github.com/Virmagnus/crpto-alerts/ but haven’t done a readme, thus this post (mainly).

Just clone it with:

$ git clone https://github.com/Virmagnus/crpto-alerts.git

Python (config.json)

The only thing to config is the config.json file.

  • The wait parameter is in seconds to query the CoinGecko Api.
  • Currency, (as i’m in EU, is EUR), but can be changed to USD, BTC, ETH, LINK, PHP (lol… ok ok its not THAT PHP, just Philippine peso) or another as long as its on the list of enabled currencies from coingecko)

Using this API could check the available currencies https://www.coingecko.com/api/documentations/v3#/simple/get_simple_supported_vs_currencies

  • Alerts “global”. This is just 3 range in percentage (both positive and negative) of alert you can modify and it applies to all coins, sorry. I’ll probably change that part to be coin specific…

So, having this values
lvl1: 2
lvl2: 5
lvl3: 10

Example 1
If you get a coin a 100$ and the coin is at 106$, you will get the alert “level2”, since its between 5% and 10%.
The alert will send a telegram message only once if it’s the same level and coin, that way i don’t get alerted every time.

Example 2
If the same coin then goes from 106$ to 99$ it wont alert you. Remember that the percentages and alerts are calculated from your price when it was bought, so even if the price was from 106$ to 99$, the script is only check percentage from 100$ (your entry price) to 99$ which is 1% down.

Example 3
So, the same as before, if the price of that coin is 106$ and goes to 97$ it will alert you but just for the level 1 alert (2%), since 100$ to 97$ is a -3%.

The negative and positive are just absolutes that i compare.

Python code

Well, not much that i can say.
Missing requirements but if remember it was only python-telegram-bot, influxdb and pycoingecko (links below).
In case you don’t know much about how to install them, just execute:

python3 -m pip install python-telegram-bot influxdb pycoingecko

https://github.com/python-telegram-bot/python-telegram-bot
https://github.com/man-c/pycoingecko

There is a function called show() which will display your currencies on screen but if you don’t want that just commented out.

Line 433 from main.py commented adding at the beginning of line #. Like this:

#show(now_pretty,result_percentage,currency)

Output from console

2020-12-12 20:06:45eth: -3.27% | price mov: -15.740 eur | current: 466.27 eur | mine: 482.01 eurbtc: 2.97% | price mov: 446.230 eur | current: 15446.23 eur | mine: 15000 eurdia: -1.71% | price mov: -0.020 eur | current: 1.15 eur | mine: 1.17 eur

Telegram (for config.json)

The telegram tokens and chat ids are from father_bot from telegram. This link is pretty straightforward on how to create. https://docs.microsoft.com/en-us/azure/bot-service/bot-service-channel-connect-telegram?view=azure-bot-service-4.0

After that you will have the TOKEN and now we missing the chat_id.

  1. Get basic bot info
curl -X GET https://api.telegram.org/bot[TOKEN]/getMe

2. Send a message from telegram to the chat with the bot

3. Get the chat_id

curl -X GET https://api.telegram.org/bot[TOKEN]/getUpdates

Which will return json and a key as chat_id to replace in the config.json. Something like this “chat”:{“id”:12313425

And that part is pretty much done.
The format of the message is inside the trigger_alarm() function but an alert is like this.

Not needed.

Now, this part is not necessary, using influx and grafana in a docker, but i like it. Just be aware that there is a function that saves data to influx but as before, just comment it out.
Line 434 from main.py:

#to_influx(result_percentage,currency)

Docker

Well, you need to have docker installed first. I did it in Ubuntu 18
Can follow the steps in the official link https://docs.docker.com/engine/install/ubuntu/

Also, docker-compose https://docs.docker.com/compose/install/

Once all is installed (probably will have to run as root or if you have perm with sudo), just execute (in the same directory from

$ docker-compose up -d — build

There is a docker-compose.yml file (yes yes version 2, i’m lazy) which will start the docker images, of grafana and influxdb.

Influxdb

From influxdb just need to create the database, so execute:

$ docker exec -it  influxdb /bin/bash

And inside the shell, we will connect to influxdb client.

$ influx

Now create the database called crypto and exit

> CREATE DATABASE "crypto"
> exit

You could setup a retention policy, but again i’m lazy and i’m expecting the script to be killed before uses all the space.
https://docs.influxdata.com/influxdb/v1.8/query_language/manage-database/

But just execute:

> CREATE DATABASE "crypto" WITH DURATION 7d REPLICATION 1 SHARD DURATION 1h NAME "crypto_7days"

Grafana

This is the last step, all of this just to see it pretty.
Depending from where you have launched your docker images, just connect to port :3000 (probably http://localhost:3000 or change for the IP) and it will launch a login screen.

Grafana is always admin:admin for the first login and then change it to your liking.

After you enter, need to configure your DB connection.
Go to Configuration> Data Sources and configure it like this.

Just hit “Test & Save” at the bottom.

Now got to Create (the + sign) and import this file https://github.com/Virmagnus/crpto-alerts/blob/main/CRYPTO-1607799004766.json

and if everything worked flawlessly which could not be the case, you will end up with something like this.

Well i dont see much there but there are more colors depending how good/bad you are in.
Let’s see another with a bit of zoom.

Final thoughts

Well, that is it.
Probably will fix all the possible errors, for calling the APIs, functions, return data etc, since i’m pretty sure it will get a lot of tracebacks but i have it for a day and for my wonder it hasn’t.

This as always is free and you can do whatever with it.

Bye

--

--