This Telegram bot allows users to submit suggestions and feedback, which are then reviewed by administrators who can accept or reject them and provide responses.
├── main.py # Main bot startup file
├── handlers/ # Handler modules
│ ├── commands.py # Command and callback handlers
│ ├── FSM.py # FSM (Finite State Machine) states
│ ├── config.py # Configuration data (bot token)
│ └── admins.json # JSON database for admin user IDs
git clone https://github.com/arhkypGitProject/Feedback-Bot-Telegram.git
cd Feedback Botpython -m venv venv
# For Linux/Mac:
source venv/bin/activate
# For Windows:
venv\Scripts\activatepip install aiogramEdit the handlers/config.py file:
TOKEN = 'YOUR_TELEGRAM_BOT_TOKEN'Where to get the token:
- Telegram Bot Token: Get it from @BotFather
Edit the handlers/admins.json file:
{
"_comment": "This file stores admin IDs",
"owner": 1234567890,
"admins": [5555555555, 7777777777]
}Replace the numbers with actual Telegram user IDs:
owner: The main administrator IDadmins: Array of additional admin IDs
To find your Telegram user ID, you can use bots like @userinfobot or @getidsbot.
python main.pyOnce running, send the /start command to begin using the bot.
For simplicity, this bot uses a JSON file for storing administrator data and Python dictionaries for temporary storage of suggestions during runtime.
The admins.json file stores administrator user IDs in a simple JSON format. This approach was chosen for:
- Simplicity: No database setup required
- Easy configuration: Direct editing of JSON file
- Portability: Single file that can be easily backed up or migrated
Suggestions are stored temporarily in a Python dictionary (suggestions variable in commands.py):
- Pros: Fast access, simple implementation
- Cons: Data is lost when the bot restarts
Replace the JSON and dictionary approach with SQLite or PostgreSQL:
# Example SQLite implementation
import sqlite3
def setup_database():
conn = sqlite3.connect('bot_data.db')
cursor = conn.cursor()
# Create admins table
cursor.execute('''
CREATE TABLE IF NOT EXISTS admins (
id INTEGER PRIMARY KEY,
user_id INTEGER UNIQUE NOT NULL,
is_owner BOOLEAN DEFAULT 0
)
''')
# Create suggestions table
cursor.execute('''
CREATE TABLE IF NOT EXISTS suggestions (
id INTEGER PRIMARY KEY AUTOINCREMENT,
user_id INTEGER NOT NULL,
text TEXT NOT NULL,
status TEXT,
admin_reply TEXT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
reviewed_at TIMESTAMP
)
''')
conn.commit()
conn.close()For even simpler deployment, you could hardcode admin IDs directly in the code:
# Replace JSON loading with hardcoded list
OWNER_ID = 1234567890
ADMINS = [OWNER_ID, 5555555555, 7777777777]Store admin IDs as environment variables for better security:
import os
OWNER_ID = int(os.getenv('BOT_OWNER_ID', '1234567890'))
ADMINS = [OWNER_ID] + [int(x) for x in os.getenv('BOT_ADMIN_IDS', '').split(',') if x]/start- Display main menu with options/help- Show usage instructions/about- Display bot information
- Send a Suggestion - Submit feedback or suggestions
- Help - Get usage instructions
- About - Learn about the bot and developer
- User clicks "Send a Suggestion"
- User types their feedback
- Feedback is sent to all administrators
- Admins review and choose "Accept" or "Reject"
- If accepted, admin can send a reply to the user
- User receives the admin's response
The bot uses aiogram's FSM for managing user states:
FeedbackStates.waiting_for_feedback- Waiting for user's suggestionFeedbackReply.waiting_for_reply- Waiting for admin's reply to accepted suggestion
- User submission: User feedback is stored in the
suggestionsdictionary with message ID as key - Admin review: All admins receive the suggestion with Accept/Reject buttons
- Decision tracking: When an admin responds, the suggestion status is updated
- User notification: If accepted, admin's reply is forwarded to the user
- Only users listed in
admins.jsoncan review suggestions - Each suggestion can only be handled once
- State management prevents unauthorized interactions
- Manual editing required: Admins must be added/removed by editing the JSON file
- No validation: No type checking or validation of user IDs
- File locking issues: Potential concurrency problems if multiple instances run
- Data persistence: All suggestions are lost when the bot restarts
- Memory usage: Large numbers of suggestions could consume significant memory
- No history: Cannot track past suggestions after restart
For a production environment, consider these improvements:
Switch to a proper database system:
- SQLite: Good for small to medium bots
- PostgreSQL: Better for larger scale with multiple instances
- Redis: Excellent for high-performance, in-memory storage
Implement database storage for suggestions to:
- Maintain history across restarts
- Enable analytics and reporting
- Allow search and filtering of past suggestions
Add commands for managing admins without editing files:
/addadmin <user_id> - Add new admin
/removeadmin <user_id> - Remove admin
/listadmins - Show all admins
Add more robust error handling for:
- Invalid user IDs in JSON file
- File permission issues
- Network connectivity problems
Implement comprehensive logging for:
- User submissions
- Admin actions
- System errors
- Performance metrics
aiogram==3.X- Telegram Bot Framework
- Bot not starting: Check if the bot token in
config.pyis correct - Admins not receiving messages: Verify admin IDs in
admins.jsonare correct - Suggestions lost after restart: This is expected with current in-memory storage
- JSON parsing errors: Ensure
admins.jsonhas valid JSON syntax
To test admin functionality:
- Add your Telegram user ID to
admins.json - Restart the bot
- Submit a suggestion from another account
- Check if you receive the suggestion with Accept/Reject buttons
This bot is designed to collect and manage user feedback (This is a demonstration of functionality, not a fully-featured product!). It stores minimal user data (only the user's ID and message content). Ensure compliance with the data protection regulations applicable to your region.
