Notification system from the console to Telegram
In the past, there were often situations where there is a demanding task that takes a long time, but it is advisable to respond quickly after the end, what to do? Of course, write a program ...
The first version simply used notify-send, but time showed some inconvenience to the process. Since the operations were very demanding of the car, the car became un-usable at that moment and wanted to go and drink coffee. But return when the process is complete. Therefore, the idea of a program appeared, which can be added to the console pipeline or wrapped like a sudo program and at the end sends you a notification to the chat. Thus, you will see the notification both on the desktop and on the phone.
For those who do not want to read, but want to try. (Linux only)
- Register with the bot and get the token with the command / config
- Download client
wget https://ice2heart.com/snitch chmod +x snitch SNITCH_USER_ID=<here_is_your_uuid> ./snitch sleep 10
The idea was simple, we need a simple server that will receive the message and send it to the user.
And since I did not want to bother with registering, storing and managing accounts, I decided to do it as it seemed to me simply (and now it seems, but I'm not sure that this is good).
Using a bot, a person receives a unique uuid v4, with which he then goes to the server. Authorization on the client chat side, but I only keep a couple of uuid-internal IDs.
Therefore, taking node.js as a server and adding a bit of koa, nedb, node-telegram-bot-api, I assembled the server part, the architecture initially meant more gateway to the chats, but there were no cases and therefore there was only one telegram left.
Also, I did not want to put the js application on the Internet directly, so using the magic of the docker, I added a proxy server caddy. Why he? Because I like the format of the configuration files.
The server turned out simple, but sufficient for work.
Therefore, we got such a simple docker-compose file, we also need a key for the bot, which we put in the .env file so as not to save our keys in the github, which is not good.
So to run, we need docker, docker-compose and a key for bot carts to get your own, you have to contact botfather .
After that, docker-compose up and our server is ready for work.
For the client, I decided to take go for the simple reason that it is going into 1 binary file, which can then be easily distributed.
There were two main ideas about the client.
- We build in the pipeline, collect the log (not implemented), send the message to close the pipeline.
- We wrap the application, for example sudo, upon completion we send a log and a message with a return code.
During the implementation, there was no easy way to find out the return code of the previous program in the pipeline, which unfortunately reduces the joy of using the program in the pipeline.
For configuration, I chose environment variables, because they allow you to easily copy the settings from the machine to the machine, plus they are easily replaced.
As a result, the installation becomes as simple as possible.
- Downloading the binary
- Register in PATH
- We set the SNITCH_USER_ID variable into the shell configuration with your uuid
- Done, you're great!
Now you can run long commands in the form
snitch makeand at the end we will receive a message that the application has ended and we will even know, successfully or not.
To build, I made a small script that simply called r. He will build the go app and skip it through upx. And at the output we get a beautiful statically linked binary.
And when everything is ready, you can contact the bot with the command / config
And it will create an entry about our user and return uuid.
Also, you can always be removed from the database, for this there is a command / delete
Thus, we got a fairly simple but effective application that notifies us about the completed task, easily and naturally, even when we drink coffee.