Creating a Crypto Price Alert Bot on Telegram with Python
Introduction
Cryptocurrencies are rising in popularity by the minute, as more and more people are investing in them for short-term and long-term gains. Even I have bought some. But one thing which is hard to do is keeping track of the price of a certain cryptocurrency continually. Today I will show you how to create a Telegram bot which can alert you if a cryptocurrency surpasses a target price.
To interface with telegram bots, you can send them a message that starts with a command, and any arguments if required.

You must make sure that you tell the bot which cryptocurrency to watch via its symbol. You can see a list of cryptos and their symbols here. The operation will be denoted by a less than or greater than symbol and the target price will be in your own local fiat currency, for eg., for me it is GBP (wherever you see GBP, change it to your local fiat currency if necessary). To get the current price of the cryptocurrency, I will be using the Coinbase API . There is more information about this further down if you want to use another API. If you do not have a Coinbase account, then you can create one here (this is a referral link, which would be cool if you used it).
The two things we need to do before we can run our bot are:
- Setting up the prerequisites
- Programming the bot
Setting up the prerequisites
First of all, we need get an official bot token from telegram, without which it would not be possible to create a bot. To start, you need to message the BotFather and from there you can use the /newbot
command to create a bot. You will soon get a bot token, make sure you note this down somewhere and do not share it with anyone.
Now we need to get our Coinbase API token. For this you can head over to their website and create a new API key. You can add any basic permissions, but if you want your bot to be able to tell you your account balance then you can add the wallet reading privileges (which I won’t go over today). Do not worry if you made a mistake, you can always create a new API key if needed. Once you create a key your API secret and key will appear on screen. Make sure you note down both of these somewhere safe and do not tell anyone your API secret, as otherwise they could have access to your entire account if the permissions allow it.
If you do not have a Coinbase account, then you can get an API key from another service which allows you to get the current price of any cryptocurrency. Make sure you check the call limits, since we will be pinging the server every 15 seconds or so and you do not want to get temporarily banned.
Programming the bot
Before we can begin coding, we need to install some new libraries which will allow us to interface with the Coinbase API and to create the telegram bot. You can run the following command to install both:
pip install python-telegram-bot coinbase
Note: If you are not using Coinbase, then you may need to search how to request data from your API.
First, we need to import our libraries, create our token constants, and create the Coinbase client object which we access later:
Make sure to replace your API keys with the mock ones.
Telegram bots work by adding a handler which checks each incoming message, it will check to see if that specific handler can, well, handle that message. If it can, then the function supplied as an argument in the creation of the handler will be called. To start using the handlers we need to access the dispatcher object via the updater object (yes it is confusing but I will show you the code and it will simplify things a lot):
We use the Updater object which we imported and supply in our token, as well as a default parse mode of HTML. This means that we can use some basic HTML elements within our response messages for making text bold, italic or underlined etc. From there we can access the dispatcher object and add two handles to it. The first will be the basic start handler and when that is met, the startCommand function, which we will create now, will be called. The other one is the alert handler which calls the priceAlert function.
After that we then tell the updater to start polling. This will continuously fetch any new messages sent to the bot. The idle method tells the updater object to shutdown if the script is stopped. We need this because the telegram bot runs as another thread, which if not shutdown properly, will continue to run endlessly in the background.
The startCommand function is actually very simple:
It takes in two arguments which both hold information about the message which activated this function. Here we send a message back to the same chat where we messaged the bot from. This is needed if the bot is in multiple chats with different users, otherwise it would not know which chat/user to reply to.
Now we need to create the priceAlert function:
Here you can see that we now access the args attribute from the context object. This holds the text which we supply after the command call (/alert). We can check if the number of arguments is greater than 2, if not an error message is sent back, also showing the proper syntax usage of the command in italics (using the HTML italics element). Otherwise, we can assign three variables their specific values.
Next, we need to add a job to the job queue, the telegram bot will repeatedly run this job until it is removed from the queue. The job we will add is to call another function which will check to see if the price of our desired crypto has met the price which we want it to, if so then a response is sent, and the job is removed from the queue. Here we create a repeating job, that starts in 15 seconds, which calls the priceAlertCallback function every 15 seconds. We also can supply a context argument with specific data such as which crypto we want to query, the price, type of operation and the chat id. After that we can send a message which tells us the current price of the cryptocurrency we are querying.
The priceAlertCallback function is actually very simple, it will first check the current price of our crypto, then according to our operation, will check how the price we supplied compares with the current price. If they satisfy our operation, then we will send a message indicating that our target price has been surpassed/reached. After this we remove this job from the queue, this is extremely important as we do not want this function being called every 15 seconds even after our price target has been reached.
After running the bot and sending the command through you can wait and check to see if it works, it should hopefully look something like this:

Final thoughts
If you wanted you could even expand this bot to do a bunch more things and even to help you trade automatically to buy when the price is at a low (buying the dip) or to buy depending on the current trend (looking at the moving average). All of that is algorithmic trading and is all very complex, maybe I’ll make a future article on that too.
I hope you enjoyed this article, if you have any queries or feedback then be sure to comment down below.
Thank you for reading! 💖