Case Study
Project Case Study
Challenges, solutions, outcomes, technical highlights, architecture, and project scale.
⚡
Challenges
- Need to broadcast messages to many Telegram users without bot being blocked
- Managing Telegram message rate limits
- Running Telegram bot and Django server simultaneously in one project
- Automatically saving user information on bot start
- Need for two-factor authentication for admin panel
- Dynamic management of channel and group links
- Sending image and text simultaneously to users and groups
- Integrating CRM system with Telegram bot
💡
Solutions
- Using aiogram for async Telegram bot management
- Running bot in a separate thread with AppsConfig.ready()
- Saving chat_id and username in Users_link model with get_or_create
- Implementing background sending with asyncio and threading
- Using 5-second intervals between sends to avoid rate limits
- Implementing 2FA with pyotp for enhanced security
- Using custom CustomUser to store Telegram info
- Implementing admin panel with simple HTML forms
- Using persiantools for Jalali date display
📚
Outcomes
- Complete Telegram message management system
- Broadcasting to users and groups with rate limit management
- Automatic user saving on first bot interaction
- Secure admin panel with 2FA
- Successful integration of Telegram bot with Django
- Support for image and text sending
- Complete CRM system for user management
🔧
Technical Highlights
- Django 5.2.6 and Python 3
- MySQL database with pymysql
- aiogram 3.28 for Telegram bot (async)
- python-telegram-bot 22.3 as backup
- Django REST Framework 3.16 for API
- django-filter for data filtering
- django-environ and python-decouple for settings management
- pyotp for two-factor authentication
- persiantools for Jalali date
- Running bot in separate thread with AppsConfig
- Managing Telegram rate limits with 5-second intervals
- Custom CustomUser for storing telegram_id and chat_id
- Implementing 2FA with TOTP
🏗️
Architecture
Forward Bot is built on Django's MVT (Model-View-Template) architecture and consists of two main applications:
1. crm: Responsible for user management, links, and admin panel
- CustomUser model with telegram_id, telegram_username, phone_number
- Chanel_link model for storing channel links
- Users_link model for storing user chat_id
- login_view, sendmassage, sendgroupmessage views
- group_links, users_link views for link management
2. telegram_bot: Responsible for running Telegram bot
- bot.py file with aiogram Dispatcher
- AppsConfig.ready() to run bot in separate thread
- BroadcastMessage model for storing broadcast messages
- TrainingVideo model for training videos
- /start handler for automatic user saving
The project runs in DEBUG mode and uses SQLite for development and MySQL for production.
📐
Project Scale
- 2 main applications (crm, telegram_bot)
- 5 main data models
- Telegram bot system with aiogram
- Dedicated admin panel
- 2FA system
- Broadcasting to users and groups
- Channel link management
- Custom CustomUser
- Async/threading integration