It provides system information and incorporates a simple notification system from other scripts.
-
Clone the project to your server.
git clone https://github.com/javierorp/server-telegram-bot.git && cd server-telegram-bot && mv "[.]env" ".env"
-
Create a virtual environment.
python -m venv .venv && source .venv/bin/activate
-
Install the required modules.
pip install -r requirements.txt
-
Set the environment variables of the ".env" file (see Environment variables).
The environment variables are required to work and are located in the ".env" file.
These are:
- BOT_TOKEN: Bot ID (see Obtain your bot token).
- CHAT_ID: User ID (see Obtain your user id).
- STORAGE_PATHS: Absolute path of the storage devices to be displayed separated by commas.
- NOTI_PATH: Absolute path where the notifications will be stored. These notifications are simple files (UTF-8 encoding) with text inside.
To run the script by command:
~$ python main.py --help
usage: main.py [-h] [--hello] [-l {DEBUG,INFO,WARNING,ERROR,CRITICAL}] [--noti] [--save-noti] [-t TIME]
It provides system information and incorporates a simple notification system from other scripts.
options:
-h, --help show this help message and exit
--hello Send a message when the bot starts.
-l {DEBUG,INFO,WARNING,ERROR,CRITICAL}, --log {DEBUG,INFO,WARNING,ERROR,CRITICAL}
Enable and set the log level. Possible values are DEBUG, INFO, WARNING, ERROR, CRITICAL. Default disabled.
--noti Enable the notification check.
--save-noti Notifications are not deleted after sending. They are stored in a 'old' folder inside NOTI_PATH.
-t TIME, --time TIME Time in seconds to check new notifications to be sent. Default 10.
Run the bot at server startup.
Open the crontab
~$ sudo crontab -e
and add next line modifying with the correct path in your case:
@reboot sleep 40; /home/user/server-telegram-bot/.venv/bin/python3 /home/user/server-telegram-bot/main.py --hello --noti -t 60
A logging is incorporated to the application that, if enabled (--log [DEBUG | INFO | WARNING | ERROR | CRITICAL]
option), stores in the bot.log
file (in the folder where the main.py
file is located) the traceability. A maximum of 2 log files of up to 1 MB each are stored, but this can be modified by rewriting the LOG_MAX
and LOG_BCK_NUM
variables in the main.py
file.
It checks the files in NOTI_PATH, reads their content and sends, for each one of them, a message with the file name (app/script where the message comes from), the creation time and the content.
If the notification file exceeds 1024 bytes (this limit can be modified by rewriting the SIZE_LIMIT
variable in the notifications.py
file), the first and last 250 bytes are sent as text and the full notification is sent as an attachment.
The file, depending on the configuration, would be deleted after the message is sent or moved to the "old" folder inside NOTI_PATH. If there is an error reading the file, the error message is sent and the file is moved to the "errors" folder inside NOTI_PATH.
For example, for the file "app_A" with the content "It's a text", the message would be:
app_A at 14:12:
It's a text
Reboot the server asking for user confirmation (Yes/No)
Rebooting the system... 🔄
Displays system information
Example of the /status
command.
--------------- ℹ️ Info ---------------
Date & time: 2025-02-28 13:32:00
Uptime: 1 days 4 hours 20 min 30 sec
Package(s): 69 upgradable(s)
--------------- 🔲 CPU ---------------
Cores: 8
Frequency: 2969.754 MHz
Scaling governor: powersave
Load:
1 min: 0.68
5 min: 0.78
15 min: 0.78
Temperature: 35.05°C
--------------- 💾 RAM ---------------
RAM: 6.94 GB of 15.35 GB (58.0%) used
--------------- 🌐 Netwotk ---------------
Public IPv4: x.x.x.x
Public IPv6: xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx
Ethernet:
Sent: 0.16 GB
Received: 0.95 GB
--------------- 💽 Disk ---------------
root (/):
Used: 61.35 GB (22.38%)
Free: 198.80 GB
Total: 274.15 GB
--------------- 📂 Storage ---------------
MyDisk:
Used: 0.00 GB (0.00%)
Free: 29.70 GB
Total: 29.70 GB
Contact @BotFather issuing the /newbot
command and following the steps until you're given a new token. You can find a step-by-step guide here.
Contact @userinfobot issuing the /start
command to obtain your ID.
Contact @BotFather issuing the /setcommands
command. Select your bot and send the following message.
status - Displays server information.
reboot - Reboot the server.