diff --git a/src/common/config.py b/src/common/config.py index dc4c7ed..2c828b7 100644 --- a/src/common/config.py +++ b/src/common/config.py @@ -43,4 +43,8 @@ def __init__(self): ### Telegram bot telegram_bot_token = os.getenv("telegram_bot_token") or "123456789:ABCDEFGHIJKLMNOPQRSTUVWXYZ" telegram_bot_enabled = bool(os.getenv("telegram_bot_enabled")) or True - telegram_whitelist_ids = [x.strip() for x in os.getenv("telegram_whitelist_ids", "").split(",") if x.strip()] \ No newline at end of file + telegram_whitelist_enabled = bool(os.getenv("telegram_whitelist_enabled")) or False + telegram_blacklist_enabled = bool(os.getenv("telegram_blacklist_enabled")) or False + + telegram_whitelist_ids = [x.strip() for x in os.getenv("telegram_whitelist_ids", "").split(",") if x.strip()] + telegram_blacklist_ids = [x.strip() for x in os.getenv("telegram_blacklist_ids", "").split(",") if x.strip()] diff --git a/src/common/static.py b/src/common/static.py index d27b37b..54c831c 100644 --- a/src/common/static.py +++ b/src/common/static.py @@ -22,6 +22,7 @@ class BotMessageTypes: REGISTRATION_SUCCESS = "registration_success" ACCOUNT_ALREADY_EXISTS = "account_already_exists" ID_NOT_WHITELISTED = "id_not_whitelisted" + ID_BLACKLISTED = "id_blacklisted" INTERNAL_ERROR = "internal_error" INCOMING_CODE = "incoming_code" @@ -97,6 +98,9 @@ class BotMessageTypes: "id_not_whitelisted": """ ❌ Ваш ID не находится в белом списке. """, + "id_blacklisted": """ + ❌ Ваш ID заблокирован. + """, "internal_error": """ ❌ Ошибка при регистрации аккаунта. """, diff --git a/src/telegrambot/bot.py b/src/telegrambot/bot.py index c89891d..dcc280f 100644 --- a/src/telegrambot/bot.py +++ b/src/telegrambot/bot.py @@ -13,6 +13,7 @@ def __init__(self, token, enabled, db_pool, whitelist_ids=None): self.enabled = enabled self.db_pool = db_pool self.whitelist_ids = whitelist_ids if whitelist_ids is not None else [] + self.blacklist_ids = blacklist_ids if blacklist_ids is not None else [] self.logger = logging.getLogger(__name__) self.msg_types = Static().BotMessageTypes() @@ -46,11 +47,16 @@ async def handle_start(message): async def handle_register(message): tg_id = str(message.from_user.id) - # Проверка ID на наличие в белом списке - if tg_id not in self.whitelist_ids: - await self.bot.send_message(message.chat.id, self.get_bot_message(self.msg_types.ID_NOT_WHITELISTED)) - return - + # Проверка ID на наличие в белом списке и в чёрном списке + if whitelist_enabled: + if tg_id not in self.whitelist_ids: + await self.bot.send_message(message.chat.id, self.get_bot_message(self.msg_types.ID_NOT_WHITELISTED)) + return + elif blacklist_enabled: + if tg_id in self.blacklist_ids: + await self.bot.send_message(message.chat.id, self.get_bot_message(self.msg_types.ID_BLACKLISTED)) + return + async with self.db_pool.acquire() as conn: async with conn.cursor() as cursor: # Проверка на существование @@ -127,4 +133,4 @@ async def send_auth_code(self, chat_id, phone, code): chat_id, self.get_bot_message(self.msg_types.INCOMING_CODE).format(phone=phone, code=code) ) except Exception as e: - self.logger.error(f"Ошибка отправки кода в Telegram: {e}") \ No newline at end of file + self.logger.error(f"Ошибка отправки кода в Telegram: {e}")