https://github.com/sendhello/barsic-bot
A comprehensive Telegram bot service for interacting with the Barsic API platform. Built with modern async Python using aiogram framework, featuring role-based access control, dialog management, and Redis-based state persistence.
https://github.com/sendhello/barsic-bot
aiogram bars2
Last synced: 3 months ago
JSON representation
A comprehensive Telegram bot service for interacting with the Barsic API platform. Built with modern async Python using aiogram framework, featuring role-based access control, dialog management, and Redis-based state persistence.
- Host: GitHub
- URL: https://github.com/sendhello/barsic-bot
- Owner: sendhello
- License: other
- Created: 2020-07-05T19:28:56.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2026-03-19T08:31:47.000Z (3 months ago)
- Last Synced: 2026-03-19T23:37:07.671Z (3 months ago)
- Topics: aiogram, bars2
- Language: Python
- Homepage:
- Size: 176 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
- Security: SECURITY.md
Awesome Lists containing this project
README
# Barsic Bot
[](https://www.python.org/downloads/)
[](https://aiogram.dev/)
[](LICENSE.md)
[](https://redis.io/)
[](https://github.com/sendhello/barsic-bot/actions/workflows/codeql.yml)
A comprehensive Telegram bot service for interacting with the Barsic API platform. Built with modern async Python using aiogram framework, featuring role-based access control, dialog management, and Redis-based state persistence.
## Table of Contents
- [Features](#features)
- [Tech Stack](#tech-stack)
- [Architecture](#architecture)
- [Prerequisites](#prerequisites)
- [Installation](#installation)
- [Configuration](#configuration)
- [Usage](#usage)
- [Bot Commands](#bot-commands)
- [Development](#development)
- [Project Structure](#project-structure)
- [Environment Variables](#environment-variables)
- [Deployment](#deployment)
- [Security](#security)
- [Contributing](#contributing)
- [License](#license)
- [Authors](#authors)
## Features
- đ¤ **Telegram Bot Interface**: Modern aiogram 3.x based bot with dialog management
- đ **Role-Based Access**: Admin and user role separation with permission filtering
- đ **Report Management**: Comprehensive reporting system for users
- âī¸ **Service Distribution**: Admin-only service distribution and management
- đž **Redis Integration**: Persistent state management and caching
- đĄī¸ **User Blocking**: Middleware for blocking unauthorized users
- đą **Private Chat Only**: Secure private chat interactions
- đ **Dialog System**: Advanced dialog management with aiogram-dialog
- đ **Async Performance**: Full async/await implementation for optimal performance
- đ **Error Handling**: Comprehensive error handling and logging
## Tech Stack
- **Bot Framework**: [aiogram](https://aiogram.dev/) 3.4.1
- **Language**: Python 3.11+
- **Dialog Management**: aiogram-dialog 2.1.0
- **Cache/State**: Redis 5.0+
- **HTTP Client**: httpx 0.27.0
- **Configuration**: Pydantic Settings 2.2.1
- **JSON Processing**: orjson 3.10.3
- **ASGI Server**: Uvicorn 0.29.0
- **Code Quality**: Black, Flake8, isort
- **Containerization**: Docker & Docker Compose
## Architecture
The Barsic Bot acts as an interface layer between users and the Barsic API service, providing role-based access to different functionalities.
```
âââââââââââââââââââ âââââââââââââââââââ
â Admin Users â â Regular Users â
âââââââââââŦââââââââ âââââââââââŦââââââââ
â â
ââââââââââââââââââââââââŧâââââââââââââââââââââââ
â â
âââââââââââââââââââ´ââââââââââââââ â
â Barsic Bot â â
â (aiogram + Dialog System) â â
âââââââââââââââŦââââââââââââââââââ â
â â
âââââââââââââââ´ââââââââââââââ â
â Redis â â
â (State & Cache) â â
âââââââââââââââââââââââââââââ â
â
âââââââââââââââ´ââââââââââââââ
â Barsic API â
â (External Service) â
âââââââââââââââââââââââââââââ
```
### Bot Flow
- **Authentication**: Role-based access with permission filters
- **State Management**: Redis-based dialog state persistence
- **API Integration**: HTTP client for Barsic API communication
- **Menu System**: Hierarchical menu structure with dialog management
## Prerequisites
- **Python**: 3.11 or higher
- **Docker**: 20.10 or higher (for containerized deployment)
- **Redis**: 5.0 or higher
- **Telegram Bot Token**: Obtained from [@BotFather](https://t.me/botfather)
## Installation
### Option 1: Docker (Recommended)
1. **Clone the repository**
```bash
git clone
cd barsic-bot
```
2. **Set up environment variables**
```bash
cp config.yaml.example config.yaml
# Edit config.yaml with your configuration
```
3. **Build and start services**
```bash
docker-compose up --build
```
### Option 2: Local Development
1. **Clone the repository**
```bash
git clone
cd barsic-bot
```
2. **Create virtual environment**
```bash
python3.11 -m venv .venv
source .venv/bin/activate # On Windows: .venv\Scripts\activate
```
3. **Install dependencies with Poetry**
```bash
poetry install --no-root
```
4. **Set up configuration**
```bash
cp config.yaml.example config.yaml
# Edit config.yaml with your bot token and settings
```
5. **Start Redis (if not using Docker)**
```bash
redis-server
```
6. **Start the bot**
```bash
python main.py
```
## Configuration
The bot uses a YAML configuration file. Copy `config.yaml.example` to `config.yaml` and configure:
- **Telegram Bot Token**: Get from [@BotFather](https://t.me/botfather)
- **Redis Connection**: Configure Redis host and port
- **API Endpoints**: Set Barsic API service URLs
- **Admin Users**: Configure admin user permissions
## Usage
### Starting the Bot
Once configured and running, users can interact with the bot via Telegram:
1. **Start**: Send `/start` to begin interaction
2. **Authentication**: Bot will verify user permissions
3. **Menu Navigation**: Use inline keyboards to navigate menus
4. **Role-Based Features**: Access features based on your role (Admin/User)
### User Features
- đ **Report Management**: Create and view reports
- âšī¸ **Information Menu**: Access bot information and help
### Admin Features
- âī¸ **Service Distribution**: Manage service distribution settings
- đĨ **User Management**: Control user access and permissions
## Bot Commands
| Command | Description | Available To |
|---------------------------|----------------------------------------------------|----------------|
| `/start` | Initialize bot interaction | All Users |
| **Menu-based navigation** | All other features accessible via inline keyboards | Role-dependent |
## Development
### Code Quality Tools
```bash
# Format code
black --line-length 120 .
# Sort imports
isort .
# Lint code
flake8 .
```
### Running in Development Mode
```bash
# Install development dependencies
poetry install
# Run with auto-reload (manual implementation needed)
python main.py
```
## Project Structure
```
barsic-bot/
âââ .github/ # GitHub Actions workflows
â âââ workflows/ # CI/CD pipeline definitions
âââ core/ # Core application configuration
â âââ logger.py # Logging configuration
â âââ settings.py # Application settings
âââ db/ # Database configuration
â âââ redis_db.py # Redis connection management
âââ filters/ # Custom aiogram filters
â âââ auth.py # Permission-based filtering
â âââ chat_type.py # Chat type filtering
âââ gateways/ # External API gateways
â âââ base.py # Base gateway class
â âââ client.py # HTTP client implementation
âââ handlers/ # Bot command and callback handlers
â âââ info_menu.py # Information menu handler
â âââ main_menu.py # Main menu handler
â âââ report_menu.py # Report management handler
â âââ service_distribution_menu.py # Admin service management
â âââ start.py # Start command handler
âââ keyboards/ # Inline keyboard definitions
âââ middlewares/ # Custom middleware
â âââ blocked_user.py # User blocking middleware
â âââ chat_action.py # Chat action middleware
âââ repositories/ # Data access layer
âââ schemas/ # Pydantic data schemas
âââ LICENSE.md # Apache 2.0 license
âââ SECURITY.md # Security policy and guidelines
âââ README.md # Project documentation
âââ pyproject.toml # Poetry configuration
âââ main.py # Application entry point
âââ constants.py # Application constants
âââ callbacks.py # Callback handlers
âââ states.py # Bot state definitions
âââ barsic_bot.sh # Startup script
```
## Environment Variables
| Variable | Default | Description |
|----------------------|----------|-----------------------------------|
| `BOT_TELEGRAM_TOKEN` | - | Telegram bot token from BotFather |
| `DEBUG` | `False` | Enable debug mode |
| `PROJECT_NAME` | `Barsic` | Service name |
| `REDIS_HOST` | `redis` | Redis server hostname |
| `REDIS_PORT` | `6379` | Redis server port |
| `API_BASE_URL` | - | Barsic API base URL |
## Deployment
### Docker Production Build
```bash
# Build production image
docker build -t barsic-bot:latest .
# Run with production settings
docker run -d \
--name barsic-bot \
--env-file .env.prod \
barsic-bot:latest
```
### System Requirements
- **Minimum**: 1 CPU, 1GB RAM
- **Recommended**: 1 CPU, 2GB RAM
- **Storage**: 5GB for logs and state data
- **Network**: Access to Telegram API, Redis, and Barsic API service
### Health Monitoring
The bot includes comprehensive logging and error handling:
- Critical errors are logged with full stack traces
- User-facing error messages for better UX
- Redis connection monitoring
## Security
Security is important for the Barsic Bot. We implement several security measures:
### Security Features
- **Role-Based Access Control**: Admin and user role separation
- **Private Chat Only**: Bot only responds to private messages
- **User Blocking**: Middleware for blocking unauthorized users
- **Input Validation**: Comprehensive input validation using Pydantic
- **Secure Token Handling**: Secure management of bot tokens and API keys
### Security Policy
For detailed security information and vulnerability reporting, please see our [Security Policy](SECURITY.md).
**Important**:
- Never commit bot tokens to version control
- Use environment variables for sensitive configuration
- Regularly update dependencies
- Monitor bot usage for suspicious activity
### Reporting Security Issues
**Please do not report security vulnerabilities through public GitHub issues.**
Instead, please report them via email to: [bazhenov.in@gmail.com](mailto:bazhenov.in@gmail.com) with the subject `[SECURITY] Barsic Bot Vulnerability Report`.
## Contributing
1. Fork the repository
2. Create a feature branch (`git checkout -b feature/amazing-feature`)
3. Make your changes
4. Add tests for new functionality (if applicable)
5. Run code quality checks (`black .`, `flake8 .`, `isort .`)
6. Commit your changes (`git commit -m 'Add amazing feature'`)
7. Push to the branch (`git push origin feature/amazing-feature`)
8. Open a Pull Request
### Code Style Guidelines
- Follow PEP 8 style guide
- Use type hints for all functions
- Write docstrings for all public methods
- Use meaningful commit messages
- Update documentation for significant changes
## License
This project is licensed under the Apache License 2.0 - see the [LICENSE.md](LICENSE.md) file for details.
Copyright 2025 Ivan Bazhenov
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
## Authors
- **Ivan Bazhenov** - *Initial work* - [@sendhello](https://github.com/sendhello)
- Email: bazhenov.in@gmail.com
## Support
For support and questions:
- Create an issue on GitHub
- Contact the maintainer via email
- Review existing issues for similar problems
---
**Built with â¤ī¸ using aiogram and Python 3.11+**