refactor(captcha): load answer from env dynamically
This commit is contained in:
@@ -63,6 +63,11 @@ class Config:
|
|||||||
except FileNotFoundError:
|
except FileNotFoundError:
|
||||||
return default_captcha
|
return default_captcha
|
||||||
|
|
||||||
|
def _load_captcha_answer(self):
|
||||||
|
"""Load captcha answer from environment variable (supports live reload)"""
|
||||||
|
load_dotenv(override=True) # Reload .env file
|
||||||
|
return os.getenv(key="CAPTCHA_ANSWER", default=None)
|
||||||
|
|
||||||
def _load(self):
|
def _load(self):
|
||||||
load_dotenv()
|
load_dotenv()
|
||||||
|
|
||||||
|
|||||||
@@ -144,14 +144,16 @@ class TelegramStarsBot:
|
|||||||
async def update_captcha(self):
|
async def update_captcha(self):
|
||||||
"""Update or add captcha text in the thread spoiler block"""
|
"""Update or add captcha text in the thread spoiler block"""
|
||||||
captcha_text = self.config._load_captcha()
|
captcha_text = self.config._load_captcha()
|
||||||
|
captcha_answer = self.config._load_captcha_answer()
|
||||||
|
|
||||||
if (
|
if (
|
||||||
not captcha_text
|
not captcha_text
|
||||||
or not self.config.enable_captcha
|
or not self.config.enable_captcha
|
||||||
or not self.config.captcha_answer
|
or not captcha_answer
|
||||||
):
|
):
|
||||||
if not self.config.enable_captcha:
|
if not self.config.enable_captcha:
|
||||||
logger.error("Капча выключена. Редактирование темы невозможно.")
|
logger.error("Капча выключена. Редактирование темы невозможно.")
|
||||||
elif not self.config.captcha_answer:
|
elif not captcha_answer:
|
||||||
logger.error(
|
logger.error(
|
||||||
"Ответ для капчи не установлен. Редактирование темы невозможно."
|
"Ответ для капчи не установлен. Редактирование темы невозможно."
|
||||||
)
|
)
|
||||||
@@ -245,8 +247,9 @@ class TelegramStarsBot:
|
|||||||
self.processed_manager.mark_processed(post_id)
|
self.processed_manager.mark_processed(post_id)
|
||||||
return
|
return
|
||||||
|
|
||||||
if self.config.enable_captcha and self.config.captcha_answer:
|
if self.config.enable_captcha:
|
||||||
if not str(self.config.captcha_answer) in post_content:
|
captcha_answer = self.config._load_captcha_answer()
|
||||||
|
if captcha_answer and str(captcha_answer) not in post_content:
|
||||||
logger.info(f"Пост {post_id} не содержит капчу. Пропускаю обработку.")
|
logger.info(f"Пост {post_id} не содержит капчу. Пропускаю обработку.")
|
||||||
self.processed_manager.mark_processed(post_id)
|
self.processed_manager.mark_processed(post_id)
|
||||||
return
|
return
|
||||||
|
|||||||
Reference in New Issue
Block a user