Skip to content

A telegram bot built in Python using official Telegram library for collecting public, anonymous questions and other types of feedback.

License

Notifications You must be signed in to change notification settings

kakoyvostorg/question-telegram-bot

Repository files navigation

VPK Telegram Bot

A Telegram bot for collecting questions from users with support for anonymous, public, and admin questions. The bot includes a Flask web application for webhook handling and can be deployed on PythonAnywhere.

Features

  • Anonymous Questions: Users can ask questions without revealing their identity
  • Public Questions: Users can ask questions with their name displayed
  • Admin Questions: Direct questions to organizers with user identification
  • Admin Commands: Send messages, reset start message, view statistics
  • Webhook Support: Flask-based webhook handling for production deployment
  • Error Handling: Comprehensive error handling and retry logic

Setup Instructions

1. Prerequisites

  • Python 3.8+
  • Flask 3.1.2+ (for webhook handling)
  • python-telegram-bot 22.5+ (for Telegram Bot API)
  • Telegram Bot Token (get from @BotFather)
  • Telegram Group/Chat ID for logging
  • Admin user IDs

2. Installation

  1. Clone the repository:
git clone git@github.com:kakoyvostorg/question-telegram-bot.git
  1. Install dependencies:
pip install -r requirements.txt

3. Configuration

  1. Update telegram_bot.py:

    • Replace PUT_YOUR_PROJECT_HOME_PATH_HERE with your actual project path
    • Example: project_home = '/home/username/vpk_bot'
  2. Update flask_app.py:

    • Replace PUT_YOUR_PYTHONANYWHERE_USERNAME_HERE with your PythonAnywhere username
    • Example: username = "yourusername"
  3. Configure data file:

    • Choose either data_ru.json (Russian) or data_eng.json (English)
    • Update telegram_bot.py to use your chosen file (change data_ru.json to data_eng.json if using English)
    • Replace PUT_YOUR_TOKEN_HERE with your Telegram bot token
    • Replace PUT_YOUR_ADMIN_IDS_HERE with your admin user IDs (array of strings)
    • Replace PUT_YOUR_LOGGING_GROUP_ID_HERE with your logging group/chat ID
    • Customize messages and responses as needed

4. Running the Bot

Local Development (Polling)

python telegram_bot.py

Production (Webhook with Flask)

python flask_app.py

5. Webhook Setup

  1. Deploy your Flask app to PythonAnywhere or another hosting service
  2. Visit /setwebhook endpoint to automatically set the webhook
  3. If automatic setup fails, manually set the webhook using the provided URL

Bot Commands

User Commands

  • /start - Start the bot and show question options
  • /cancel - Cancel current conversation

Admin Commands

  • /send <user_id> <message> - Send a message to a specific user
  • /reset <new_start_message> - Update the start message
  • /stats - View interaction statistics

File Structure

your_bot/
├── telegram_bot.py      # Main bot logic
├── flask_app.py         # Flask webhook handler
├── data_ru.json         # Configuration and responses (Russian)
├── data_eng.json        # Configuration and responses (English)
├── requirements.txt     # Python dependencies
├── .gitignore          # Git ignore file
└── README.md           # This file

Configuration Details

Data File Structure

Both data_ru.json and data_eng.json have the same structure:

  • metadata: Bot configuration (token, admin IDs, etc.)
  • base_responses: Default bot responses
  • question_responses: Question flow responses
  • admin_responses: Admin command responses
  • buttons: Button text for inline keyboards
  • notifications: Message templates for group notifications
  • analytics: Statistics message templates

Security Notes

  • Never commit real tokens or admin IDs to version control
  • Use environment variables for sensitive data in production
  • The .gitignore file is configured to exclude sensitive files
  • All user input is properly escaped to prevent HTML injection

Deployment

PythonAnywhere

  1. Upload your code to PythonAnywhere
  2. Install dependencies in a virtual environment
  3. Configure the webhook URL
  4. Set up a web app with your Flask application

Other Platforms

The bot can be deployed on any platform that supports Python and Flask, such as:

  • Heroku
  • DigitalOcean
  • AWS
  • Google Cloud Platform

Troubleshooting

Common Issues

  1. Bot not responding: Check if the token is correct and the bot is running
  2. Webhook not working: Verify the webhook URL is accessible and properly set
  3. Permission errors: Ensure admin IDs are correctly configured
  4. File not found: Check if the project path is correctly set in telegram_bot.py

Error Handling

The bot includes comprehensive error handling:

  • Retry logic for failed message sends
  • Timeout handling for API calls
  • Fallback responses for critical failures
  • Error logging and admin notifications

Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Test thoroughly
  5. Submit a pull request

Or just contact us at 862andfurther@gmail.com.

MIT License

About

A telegram bot built in Python using official Telegram library for collecting public, anonymous questions and other types of feedback.

Resources

License

Stars

Watchers

Forks

Languages