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.
- 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
- 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
- Clone the repository:
git clone git@github.com:kakoyvostorg/question-telegram-bot.git- Install dependencies:
pip install -r requirements.txt-
Update
telegram_bot.py:- Replace
PUT_YOUR_PROJECT_HOME_PATH_HEREwith your actual project path - Example:
project_home = '/home/username/vpk_bot'
- Replace
-
Update
flask_app.py:- Replace
PUT_YOUR_PYTHONANYWHERE_USERNAME_HEREwith your PythonAnywhere username - Example:
username = "yourusername"
- Replace
-
Configure data file:
- Choose either
data_ru.json(Russian) ordata_eng.json(English) - Update
telegram_bot.pyto use your chosen file (changedata_ru.jsontodata_eng.jsonif using English) - Replace
PUT_YOUR_TOKEN_HEREwith your Telegram bot token - Replace
PUT_YOUR_ADMIN_IDS_HEREwith your admin user IDs (array of strings) - Replace
PUT_YOUR_LOGGING_GROUP_ID_HEREwith your logging group/chat ID - Customize messages and responses as needed
- Choose either
python telegram_bot.pypython flask_app.py- Deploy your Flask app to PythonAnywhere or another hosting service
- Visit
/setwebhookendpoint to automatically set the webhook - If automatic setup fails, manually set the webhook using the provided URL
/start- Start the bot and show question options/cancel- Cancel current conversation
/send <user_id> <message>- Send a message to a specific user/reset <new_start_message>- Update the start message/stats- View interaction statistics
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
Both data_ru.json and data_eng.json have the same structure:
metadata: Bot configuration (token, admin IDs, etc.)base_responses: Default bot responsesquestion_responses: Question flow responsesadmin_responses: Admin command responsesbuttons: Button text for inline keyboardsnotifications: Message templates for group notificationsanalytics: Statistics message templates
- Never commit real tokens or admin IDs to version control
- Use environment variables for sensitive data in production
- The
.gitignorefile is configured to exclude sensitive files - All user input is properly escaped to prevent HTML injection
- Upload your code to PythonAnywhere
- Install dependencies in a virtual environment
- Configure the webhook URL
- Set up a web app with your Flask application
The bot can be deployed on any platform that supports Python and Flask, such as:
- Heroku
- DigitalOcean
- AWS
- Google Cloud Platform
- Bot not responding: Check if the token is correct and the bot is running
- Webhook not working: Verify the webhook URL is accessible and properly set
- Permission errors: Ensure admin IDs are correctly configured
- File not found: Check if the project path is correctly set in
telegram_bot.py
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
- Fork the repository
- Create a feature branch
- Make your changes
- Test thoroughly
- Submit a pull request
Or just contact us at 862andfurther@gmail.com.