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!
- Search for BotFather in the telegram app. You will see the BotFather bot with a verified blue tick next to it.
- Click on that bot and type the message "/newbot" and send it. /newbot command is used to create a new bot in telegram.
- Now the BotFather will ask you for a name and a username enter these as per your wish.
- 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.
Comments