Use Telegram and Flask to build personal notification server
Imagine if you are in these situations:
- I want to notify my girl friend whenver I leave company or arrive home
- I want to get notified when there is new visit to my website
- I want get notified when a scraper or job finished
The solution to above situations is a mechanism to send message to your phone programmatically and here is my solution:
Build a Flask app to receive GET or POST request, and then compose different messages according to event type and send to some chat channel such as Telegram or Slack. This Flask app can be running in Google App Engine for free, and use your custom domain.
The steps to build this Flask app can be found below:
- Follow Google App Engine tutorial to build a basic Flask app
- Create a Telegram bot in: https://core.telegram.org/bots#3-how-do-i-create-a-bot
- Talk with your bot and get the chat id:
- Create a route handler in Flask app and make it be able to make GET request to send message like below:
- Upload the Flask app to Google App Engine:
gcloud app deploy
Now you have a Flask app to send you message whenever it gets a HTTP request. The message can be pre-defined or passed to it on the flight.
Back to the situations I talked about at the beginning, how this Flask app can solve the problems?
- Install IFTTT app on your phone and allow it to monitor your location, create an action to make webhook request to your Flask endpoint whenever you leave a place or arrive a place.
- Create a route in Flask app to receive website visit GET request, and then send a message with visitor information (e.g. origin IP) to your Telegram.
- When a script/job finished, it can make a request to your Flask app endpoint and it can send you notification to notify you it's completed.
If you don't have additional logic, messages can be directly sent to Telegram channel without Flask app.
This is my Flask app, so for your reference: