Case Study
Project Case Study
Challenges, solutions, outcomes, technical highlights, architecture, and project scale.
⚡
Challenges
- Need to automatically forward messages from multiple source groups to multiple destination groups
- Automatically retrieving group chat_ids from Telegram without manual intervention
- Managing Telegram message rate limits and preventing bot blockage
- Filtering inappropriate content using forbidden words list
- Automatically adding admin signature to the beginning of each message
- Supporting various content types (text, image, file)
- Dynamically managing enabling/disabling destination groups
- Integrating aiogram bot with Django in one project
- Limiting number of source groups to prevent abuse
💡
Solutions
- Using aiogram for async bot management
- Running bot in separate thread with AppsConfig.ready()
- Implementing resolve_chat_id method for automatic chat_id retrieval from Telegram API
- Filtering forbidden words before forwarding with Forbidden_words model
- Automatic signature addition with replace_or_add_id method
- Supporting various content types by detecting message type (text/photo/document)
- Implementing toggle for enabling/disabling destination groups
- Using socks5 proxy to bypass restrictions
- 10 source groups limit with chanels.count() < 10 condition
- Using sync_to_async for safe Django ORM interaction
📚
Outcomes
- Complete Telegram message auto-forward system
- Support for multiple source and destination groups
- Smart forbidden word filtering
- Automatic admin signature addition
- Support for text, image, and file
- Dynamic management with enable/disable
- Secure admin panel
- Successful integration of aiogram bot with Django
- SOCKS5 proxy support
🔧
Technical Highlights
- Django 5.2.6 and Python 3
- MySQL database with pymysql
- aiogram 3.28 for Telegram bot (async)
- Django REST Framework 3.16 for API
- django-filter for data filtering
- pyotp for two-factor authentication
- persiantools for Jalali date
- Running bot in separate thread
- Automatic chat_id retrieval with Telegram API
- SOCKS5 proxy support (aiohttp_socks)
- sync_to_async for safe ORM interaction
- Forbidden word management
- Automatic signature addition to messages
- Support for text, image, and document
🏗️
Architecture
Forward From Group is built on Django's MVT (Model-View-Template) architecture and consists of two main applications:
1. crm: Responsible for link, user, and admin panel management
- Custom CustomUser model
- Target_chanel_link model for destination groups
- Origin_chanel_link model for source groups
- Forbidden_words model for forbidden words
- My_telegram_id model for admin ID
- group_links, origin_group, forbidden_words, set_my_telegram_id views
2. telegram_bot: Responsible for running Telegram bot
- bot.py file with aiogram Dispatcher
- AppsConfig.ready() to run bot in thread
- forward_handler for group messages
- channel_post_handler for channel messages
- forward_to_targets method for forwarding to destination groups
- BroadcastMessage and TrainingVideo models
The project runs in DEBUG mode and uses SQLite for development and MySQL for production.
📐
Project Scale
- 2 main applications (crm, telegram_bot)
- 7 main data models
- Automatic message forward system
- Dedicated admin panel
- Forbidden word filtering
- Source and destination group link management
- SOCKS5 proxy support
- Custom CustomUser
- Async/threading integration
- 10 source groups limit