Skip to content

arhkypGitProject/Feedback-Bot-Telegram

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Suggestion Collection Telegram Bot

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.

Логотип проекта

Project Structure

├── 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

Installation and Setup

1. Clone the Repository

git clone https://github.com/arhkypGitProject/Feedback-Bot-Telegram.git
cd Feedback Bot

2. Create Virtual Environment (Recommended)

python -m venv venv

# For Linux/Mac:
source venv/bin/activate

# For Windows:
venv\Scripts\activate

3. Install Dependencies

pip install aiogram

4. Configuration Setup

Edit the handlers/config.py file:

TOKEN = 'YOUR_TELEGRAM_BOT_TOKEN'

Where to get the token:

5. Configure Admins

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 ID
  • admins: Array of additional admin IDs

To find your Telegram user ID, you can use bots like @userinfobot or @getidsbot.

Running the Bot

python main.py

Once running, send the /start command to begin using the bot.

Data Storage Implementation

For simplicity, this bot uses a JSON file for storing administrator data and Python dictionaries for temporary storage of suggestions during runtime.

JSON Database

The admins.json file stores administrator user IDs in a simple JSON format. This approach was chosen for:

  1. Simplicity: No database setup required
  2. Easy configuration: Direct editing of JSON file
  3. Portability: Single file that can be easily backed up or migrated

In-Memory Storage

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

Alternative Storage Options

Option 1: SQL Database (Recommended for Production)

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()

Option 2: Python List for Admins

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]

Option 3: Environment Variables

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]

Bot Features

User Commands:

  • /start - Display main menu with options
  • /help - Show usage instructions
  • /about - Display bot information

Main Menu Options:

  1. Send a Suggestion - Submit feedback or suggestions
  2. Help - Get usage instructions
  3. About - Learn about the bot and developer

Workflow:

  1. User clicks "Send a Suggestion"
  2. User types their feedback
  3. Feedback is sent to all administrators
  4. Admins review and choose "Accept" or "Reject"
  5. If accepted, admin can send a reply to the user
  6. User receives the admin's response

Technical Implementation

FSM (Finite State Machine)

The bot uses aiogram's FSM for managing user states:

  • FeedbackStates.waiting_for_feedback - Waiting for user's suggestion
  • FeedbackReply.waiting_for_reply - Waiting for admin's reply to accepted suggestion

Data Flow

  1. User submission: User feedback is stored in the suggestions dictionary with message ID as key
  2. Admin review: All admins receive the suggestion with Accept/Reject buttons
  3. Decision tracking: When an admin responds, the suggestion status is updated
  4. User notification: If accepted, admin's reply is forwarded to the user

Security Considerations

  • Only users listed in admins.json can review suggestions
  • Each suggestion can only be handled once
  • State management prevents unauthorized interactions

Limitations of Current Implementation

JSON Database Limitations:

  1. Manual editing required: Admins must be added/removed by editing the JSON file
  2. No validation: No type checking or validation of user IDs
  3. File locking issues: Potential concurrency problems if multiple instances run

In-Memory Storage Limitations:

  1. Data persistence: All suggestions are lost when the bot restarts
  2. Memory usage: Large numbers of suggestions could consume significant memory
  3. No history: Cannot track past suggestions after restart

Production Recommendations

For a production environment, consider these improvements:

1. Database Migration

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

2. Persistent Suggestion Storage

Implement database storage for suggestions to:

  • Maintain history across restarts
  • Enable analytics and reporting
  • Allow search and filtering of past suggestions

3. Admin Management Commands

Add commands for managing admins without editing files:

/addadmin <user_id> - Add new admin
/removeadmin <user_id> - Remove admin
/listadmins - Show all admins

4. Enhanced Error Handling

Add more robust error handling for:

  • Invalid user IDs in JSON file
  • File permission issues
  • Network connectivity problems

5. Logging

Implement comprehensive logging for:

  • User submissions
  • Admin actions
  • System errors
  • Performance metrics

Dependencies

  • aiogram==3.X - Telegram Bot Framework

Troubleshooting

Common Issues:

  1. Bot not starting: Check if the bot token in config.py is correct
  2. Admins not receiving messages: Verify admin IDs in admins.json are correct
  3. Suggestions lost after restart: This is expected with current in-memory storage
  4. JSON parsing errors: Ensure admins.json has valid JSON syntax

Testing Admin Features:

To test admin functionality:

  1. Add your Telegram user ID to admins.json
  2. Restart the bot
  3. Submit a suggestion from another account
  4. Check if you receive the suggestion with Accept/Reject buttons

Disclaimer

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.

About

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!

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages