Skip to content

orenlab/pytmbot

Repository files navigation

pytmbot

pyTMbot

A simple Telegram bot designed to gather basic information about the status of your local servers. The bot operates synchronously. It does not use webhooks.

Security Rating Vulnerabilities Quality Gate Status Bugs Maintainability Rating Code Smells Duplicated Lines (%) Codacy Badge Docker Image Build CI/CD Docker Pulls

The bot was written using the pyTelegramBotAPI. Use psutil and docker-py libraries for gather information.

πŸ’‘ Features

  • Load average information
  • Summary memory usage information (with swap)
  • Sensors information
  • Summary process information
  • Uptime information
  • File system base information
  • Containers (only docker and only on Linux) base information
  • Use Jinja2 for answers template
  • Use docker logs collector (sudo docker logs container_id)
  • Use emoji :)

Screenshots are available here: screenshots.md. Video demo see in YouTube Shorts here

πŸͺ€ Requirements

Initially, the bot was designed to ensure its correct operation only within the Docker container. I have not tested it running on a local system, either inside or outside a virtual environment. Therefore, please make sure that Docker is installed on your system.

Full list of Python dependencies see in requirements.txt

πŸ”Œ Installation, setup and run bot

πŸ›‘ Secure

The bot has an authorization mechanism. That is based on a unique value we can get from the message's variable from_user.id, which is the Telegram user ID. By comparing this value with the user.id values specified in the bot's settings (which is done at the initial stage of configuring the bot), we can determine the behavior of the bot.

All failed attempts to authorize are logged with an ERROR flag.

🌲 Bot tree

β”œβ”€β”€ Dockerfile                              - Main Dockerfile
β”œβ”€β”€ LICENSE                                 - Licence file
β”œβ”€β”€ README.md                               - Main README
β”œβ”€β”€ SECURITY.md                             - Security police
β”œβ”€β”€ app
β”‚   β”œβ”€β”€ __init__.py                         
β”‚   β”œβ”€β”€ core
β”‚   β”‚   β”œβ”€β”€ __init__.py                     - Bot core
β”‚   β”‚   β”œβ”€β”€ adapters
β”‚   β”‚   β”‚   β”œβ”€β”€ __init__.py
β”‚   β”‚   β”‚   β”œβ”€β”€ docker_adapter.py           - Docker adapter
β”‚   β”‚   β”‚   β”œβ”€β”€ podman_adapter.py           - Podman adapter (in development)
β”‚   β”‚   β”‚   └── psutil_adapter.py           - Psutil adapter
β”‚   β”‚   β”œβ”€β”€ exceptions.py                   - Custom exceptions
β”‚   β”‚   β”œβ”€β”€ handlers
β”‚   β”‚   β”‚   β”œβ”€β”€ __init__.py
β”‚   β”‚   β”‚   β”œβ”€β”€ default_handlers
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ __init__.py             - Import all defaults handlers
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ containers_handler.py   - Container handler
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ fs_handler.py           - Filesystem handler
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ load_avg_handler.py     - Load average handler
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ memory_handler.py       - Memory handler
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ process_handler.py      - Process handler
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ sensors_handler.py      - Sensors handler
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ start_handler.py        - Main, start handler
β”‚   β”‚   β”‚   β”‚   └── uptime_handlers.py      - Uptime handler
β”‚   β”‚   β”‚   β”œβ”€β”€ handler.py                  - Base handler class (abc)
β”‚   β”‚   β”‚   β”œβ”€β”€ handlers_aggregator.py      - Main handlers aggregator
β”‚   β”‚   β”‚   └── inline_handlers
β”‚   β”‚   β”‚       β”œβ”€β”€ __init__.py
β”‚   β”‚   β”‚       └── swap_handler.py         - Swap inline handler
β”‚   β”‚   β”œβ”€β”€ jinja2
β”‚   β”‚   β”‚   β”œβ”€β”€ __init__.py
β”‚   β”‚   β”‚   └── jinja2.py                   - Main jinja2 class
β”‚   β”‚   β”œβ”€β”€ keyboards
β”‚   β”‚   β”‚   β”œβ”€β”€ __init__.py
β”‚   β”‚   β”‚   └── keyboards.py                - Main keyboards class  
β”‚   β”‚   β”œβ”€β”€ middleware
β”‚   β”‚   β”‚   β”œβ”€β”€ __init__.py
β”‚   β”‚   β”‚   └── auth.py                     - Auth middleware class
β”‚   β”‚   └── settings
β”‚   β”‚       β”œβ”€β”€ __init__.py
β”‚   β”‚       β”œβ”€β”€ bot_settings.py             - Class to load configuration from .pytmbotenv
β”‚   β”‚       β”œβ”€β”€ keyboards.py                - Keyboards settings
β”‚   β”‚       └── loggers.py                  - Logger templates
β”‚   β”œβ”€β”€ main.py                             - Main bot class
β”‚   β”œβ”€β”€ templates
β”‚   β”‚   β”œβ”€β”€ containers.jinja2               - Containers jinja2 template 
β”‚   β”‚   β”œβ”€β”€ fs.jinja2                       - Filesystem jinja2 template
β”‚   β”‚   β”œβ”€β”€ index.jinja2                    - Start jinja2 template
β”‚   β”‚   β”œβ”€β”€ load_average.jinja2             - Load average jinja2 template
β”‚   β”‚   β”œβ”€β”€ memory.jinja2                   - Memory jinja2 template
β”‚   β”‚   β”œβ”€β”€ none.jinja2                     - Docker jinja2 template
β”‚   β”‚   β”œβ”€β”€ process.jinja2                  - Process jinja2 template
β”‚   β”‚   β”œβ”€β”€ sensors.jinja2                  - Sensors jinja2 template
β”‚   β”‚   β”œβ”€β”€ swap.jinja2                     - Swap jinja2 template
β”‚   β”‚   └── uptime.jinja2                   - Uptime jinja2 template
β”‚   └── utilities
β”‚       β”œβ”€β”€ __init__.py
β”‚       └── utilities.py                    - Some utility
β”œβ”€β”€ bot_cli
β”‚   β”œβ”€β”€ cfg_templates
β”‚   β”‚   └── env.py                          - Template for initial setup
β”‚   └── fs.py                               - Filesystem utility
β”œβ”€β”€ docker-compose.yml                      - Docker Compose file (used main Dockerfile)
β”œβ”€β”€ docs
β”‚   β”œβ”€β”€ docker.md                           - README for hub.docker.com
β”‚   β”œβ”€β”€ installation.md                     - Installation guide
β”‚   β”œβ”€β”€ roadmap.md                          - Roadmap guide
β”‚   └── screenshots.md                      - Bots screenshot
β”œβ”€β”€ hub.Dockerfile                          - Dockerfile for Docker CI/CD based on Alpine
β”œβ”€β”€ logs
β”‚   └── pytmbot.log                         - Main logs file
β”œβ”€β”€ poetry.lock                             - Poetry file
β”œβ”€β”€ pyproject.toml                          - Poetry file
β”œβ”€β”€ requirements.txt                        - Requirements for build Docker image
β”œβ”€β”€ setup_bot.py                            - Initial setup bot script
β”œβ”€β”€ setup_req.txt                           - Setup requirements
β”œβ”€β”€ tests
β”‚   └── bot_tests.py                        - Bots tests

πŸ“ˆ Roadmap

πŸ‘Ύ Known issues

  • You tell me :)

πŸ‹ pyTMBot on Docker Hub

🧬 Authors

πŸš€ About Me

I am a novice Python developer. This is my first publicly available project in this great programming language.

πŸ“œ License

MIT License