Telezab/app/bot/middlewares/user_access.py
UdoChudo 52e31864b3 feat: Develop web interface
- Implemented the initial version of the web interface.
refactor: Begin Telegram bot refactoring
- Started restructuring the bot’s code for better maintainability.
chore: Migrate to Flask project structure
- Reorganized the application to follow Flask's project structure.
cleanup: Extensive code cleanup
- Removed redundant code and improved readability.

Signed-off-by: UdoChudo <stream@udochudo.ru>
2025-06-10 14:39:11 +05:00

50 lines
1.7 KiB
Python

# app/bot/middlewares/user_middleware.py
from telebot.handler_backends import BaseMiddleware
from app.models.users import Users
from app.extensions.db import db
class UserVerificationMiddleware(BaseMiddleware):
"""
Middleware: проверяет наличие пользователя и флаги, работает в контексте Flask-приложения
"""
def __init__(self, bot, flask_app):
super().__init__()
self.update_types = ['message', 'callback_query']
self.bot = bot
self.app = flask_app # Сохраняем ссылку на Flask app
def pre_process(self, message, data):
if hasattr(message, 'chat'):
chat_id = message.chat.id
elif hasattr(message, 'message') and hasattr(message.message, 'chat'):
chat_id = message.message.chat.id
else:
return
try:
with self.app.app_context():
user = db.session.query(Users).filter_by(chat_id=chat_id).first()
if user is None:
data['user_not_found'] = True
return
if user.is_blocked:
data['user_blocked'] = True
return
data['user'] = user
data['user_verified'] = True
except Exception as e:
print(f"Ошибка при проверке пользователя: {e}")
def post_process(self, message, data, exception=None):
if exception:
print(f"Exception in handler: {exception}")
elif data.get('user_verified'):
user = data.get('user')
print(f"✅ Пользователь chat_id={user.chat_id} прошёл проверку")