Create a telegram bot to get notifications about price of your favourite Cryptocurrency (using Python)














































Create a telegram bot to get notifications about price of your favourite Cryptocurrency (using Python)




In this article we would make telegram bot for our cryptocurrency needs.

Features of the bot:
  • It will notify you if the price of your cryptocurrency drops below a threshold value.
  • It will also notify you the price of your cryptocurrency every 10 minutes.

All of these values can be easily tweaked from the code as per your need.

What will we need ?
  • A telegram account.
  • Python interpreter.
  • requests module.

To install requests module simply type the below code in your terminal:
       pip install requests

We will be using CoinBase API for getting the value of cryptocurrency.
CoinBase API is a public API atleast for getting exchange rates and hence NO API key is required to use it!


In this article we would be using fiat or normal currency as INR(Indian Rupee) and CryptoCurrency as BTC or Bitcoin as an example.

So let's get started!


What is a telegram bot?

Telegram allows it's users to create a bot for themselves using their API.
In this article I will guide you through everything from creating a telegram bot from scratch using 
the telegram API.
Though if you want you can find more info about that here https://core.telegram.org/bots/api .


How to create a telegram bot?

Simple use BotFather!

  1. Search for BotFather in the telegram app. You will see the BotFather bot with a verified blue tick next to it.
  2. Click on that bot and type the message "/newbot" and send it. /newbot command is used to create a new bot in telegram.
  3. Now the BotFather will ask you for a name and a username enter these as per your wish.
  4. Then it will send you a congratulations message which contains the link to your bot and the authorization token for your bot.

It should look something like this :



Now let's activate your bot. To do that,

Click on the link to your bot which the BotFather gave you:
Click on that link and send a /start command to activate it.

like this:


Now we would also need the Chat Id so that the bot knows whom to send the message.
You can get your chat id from "ID Bot".
Search it on telegram and then send a message "/getid"

like this:





Keep the chat_id and token handy! we will need it for our script now.

Coding the script:

First let's import libraries and define local variables


.


import requests
import time

token = "" # your bot token
chat_id = "" #your id

threshold = 2580770 # just the price of 1 bitcoin in INR at the time of writing this script

# it will send a request to coinbase API every 10 minutes
time_interval = 10*60



send_message()
This function will send a message taking chat_id and message as an input.
It uses requests library to send a get request to telegram's API end point.
So if you call this function with your chat_id  and message.

Your bot will send you that message!

def send_message(chat_id, msg):
url = f"https://api.telegram.org/bot{token}/sendMessage?chat_id={chat_id}&text={msg}"

# send the msg
requests.get(url)



the below crypto_price() will return you the value of 1 bitcoin in INR,

It uses coinbase API to get exchange rates of a given cryptocurrency in to different fiat currencies.
Here we will only use it to get value of 1 "BTC" in "INR".

def crypto_price():
# Available cryptocurrencies are 'BTC', 'ETH', 'ETC', 'BCH', 'LTC', 'ZEC', 'ZRX'
# More info at documentation site of coinbase API.

cryptocurrency = "BTC"
response_obj = requests.get(
'https://api.coinbase.com/v2/exchange-rates?currency='+cryptocurrency)

try:
exchange_dict = response_obj.json()["data"]["rates"]
except KeyError:
return None

# get current_price for cryptocurrency using exhange_rates function
current_price = exchange_dict["INR"]

return float(current_price)

Now we will create a main() which will use all of these functions to send us alerts!

def main():
# infinite loop
while True:
price = crypto_price()
# if the price falls below threshold, send an immediate msg
if price < threshold:
send_message(chat_id=chat_id, msg=f'Price Drop Alert: {price}')

#waits 10 minutes before sending a message
time.sleep(time_interval)
price = crypto_price()
send_message(chat_id=chat_id, msg=f'BTC Price Drop Alert: {price}')


if __name__ == '__main__':
print("running the script")
main()

That's it! now you will get an alert every 10 minutes and when the price drops below a threshold from your telegram bot.
You can modify the code as per your needs like you can change the cryptocurrency variable in "crypto_price()" to "ETH" for ethereum or you can change the threshold value to your need or time_interval to any other value.

An important thing is that this bot will work only until the script runs so if you stop it then you won't get notifications.

You might want to deploy it using GCP,AWS or heroku or some other alternative,
So this script will continue to run.
Most of these provide free alternative.





More Articles of Mohta Rahul Suresh:

Name Views Likes
How to kill a thread in python - part 1 875 0
IMAGE PROCESSING FROM SCRATCH USIGN PIL AND OPENCV SERIES : PART 3 538 0
Automated testing and implementing unit testing using python 896 0
Searching algorithms in RUST Part 1 686 0
Check validity of Email address using SMTP and dnspython library. 3940 0
Blocking and non-blocking threads in python - Simplified 6699 0
IMAGE PROCESSING FROM SCRATCH USIGN PIL AND OPENCV SERIES : PART 7 492 0
How to run C and C++ on Google Colab. 46721 1
Create a telegram bot to get notifications about price of your favourite Cryptocurrency (using Python) 2188 1
Image processing from Scratch usign PIL and OpenCV Series : Part 1 393 0
Searching Algorithms in RUST part 2 317 0
Make a QR-code with your Face on it. 2110 0
RUST CHEATSHEET Part 2 452 0
How to kill a thread in python - part 2 671 0
Hiding an Image inside another Image - Steganography using python. 3222 0
RUST CHEAT SHEET part 1 569 0
PyPy installation, usage and speed comparison with CPython 753 0
IMAGE PROCESSING FROM SCRATCH USIGN PIL AND OPENCV SERIES : PART 6 281 0
Convert text in an image to an audio file using tesseract and gTTS in python. 1825 0
IMAGE PROCESSING FROM SCRATCH USIGN PIL AND OPENCV SERIES : PART 5 344 0
IMAGE PROCESSING FROM SCRATCH USIGN PIL AND OPENCV SERIES : PART 4 305 0
Convert a CSV file to a table in a markdown file 4141 0
pandasgui tutorial 1074 3
IMAGE PROCESSING FROM SCRATCH USIGN PIL AND OPENCV SERIES : PART 2 396 0
Algorithmic trading basics with google stocks using python. 858 0

Comments