Implement notifications in your project EASY!

Easy implementation of notifications in an application
Hello Everyone,
This will be a “tutorial” style blog post, on how to create a python application that can notify you on your phone given some circumstances or check that you may want to make.
-
Ideas for an application of this style
- Notificator when you have low storage available
- Notificator when your site or api is not available through a healthcheck request
- Notificator for prices of crypto/stocks
- Notificator for availability on certain product on a site through a basic web-crawling
- And much more...
In my case I built it to follow the prices of a crypto currency I was following very much, and I realized I was losing a lot of time during the day checking the price, so I just made some conditions that will fire up a notification in my phone, giving me the call-to-action basically.
Before starting for this tutorial to work we need to perform the following steps
1) Get a Telegram account (free) using your phone number 📲
Or download it in your phone
2) Go into settings (web or app) and set a username ⚙️
This is needed to obtain an id which your bot will use to send messages to
3) Send a message to RawDataBot to get your id ✉️
Just search for RawDataBot and send any message (hi will do). Take a note of your id.
4) Create your bot (which you'll command with HTTP requests) 🔑
Now search for BotFather and send the message /start. Help is displayed. Send the message /newbot and follow the instructions. Take a note of your token to access the HTTP API
After following this steps we have 2 pieces of important information we need to keep:
- Your ID
- Your Token
After having this, you could install with pip telegram_send, configure it and so on, but we will be more pragmatic and use regular requests library. Also, not going to list or show snippets of other programming languages that could do the same, but basically you could do the same thing with any programming language, even with curl and bash, in this case I am using python. 🐍
#!/usr/bin/python
import requests
token = "😇"
chat_id = "👌🏻"
def notify(msg):
url = f"https://api.telegram.org/bot{token}"
params = {"chat_id": chat_id, "text": msg}
r = requests.get(url + "/sendMessage", params=params)
return r
if __name__ == '__main__':
notify("Hello World!")
notify("What's up?")
This is the code, pretty straightforward, you can use this function and code, and is up to you to create some desired logic whenever you want to be notified, I find this approach much much more useful than dealing with some push notification service and application, dealing with authentication and so on.
I hope this post had helped you, be healthy, happy and productive! ✌🏻