feat(captcha): add validation and thread editing
Build and Push (dev) / build (push) Successful in 38m3s
Build and Push (main) / build (push) Canceled after 21m44s

This commit is contained in:
2026-08-07 10:51:54 +05:00
parent 9ee9e7b731
commit 0cc2440736
3 changed files with 162 additions and 18 deletions
+55
View File
@@ -141,6 +141,51 @@ class TelegramStarsBot:
logger.error(f"Критическая ошибка при отправке звезд: {e}")
return False
async def add_captcha(self):
captcha_text = self.config._load_captcha()
if (
not captcha_text
or not self.config.enable_captcha
or not self.config.captcha_answer
):
if not self.config.enable_captcha:
logger.error("Капча выключена. Редактирование темы невозможно.")
elif not self.config.captcha_answer:
logger.error(
"Ответ для капчи не установлен. Редактирование темы невозможно."
)
elif not captcha_text:
logger.error(
"Вопрос (текст) капчи не установлен. Редактирование темы невозможно."
)
return False
try:
thread = await self.lolz_api.get_thread(thread_id=self.config.thread_id)
thread_text = thread.get("post_body", "")
# Check if captcha already exists in thread
if "[spoiler=Капча" in thread_text:
logger.info(f"Капча уже существует в теме {self.config.thread_id}, пропускаю добавление.")
return True
captcha_wrapper = f"\n\n[spoiler=Капча (обязательно);align=center][CENTER]{captcha_text}[/CENTER][/spoiler]"
thread_text += captcha_wrapper
await self.lolz_api.edit_thread(
thread_id=self.config.thread_id, post_body=thread_text
)
logger.info(
f"Тема {self.config.thread_id} отредактирована. Текст с капчей добавлен"
)
return True
except Exception as e:
logger.error(f"Критическая ошибка при редактировании темы: {e}")
return False
async def _process_single_post(self, post: Dict[str, Any]):
post_message_id = None
@@ -178,6 +223,12 @@ class TelegramStarsBot:
self.processed_manager.mark_processed(post_id)
return
if self.config.enable_captcha and self.config.captcha_answer:
if not str(self.config.captcha_answer) in post_content:
logger.info(f"Пост {post_id} не содержит капчу. Пропускаю обработку.")
self.processed_manager.mark_processed(post_id)
return
links = TelegramLinkExtractor.extract(post_content)
if not links:
logger.info(f"В посте {post_id} не найдено ссылок Telegram.")
@@ -380,6 +431,9 @@ class TelegramStarsBot:
# _main_loop - Основной цикл для остлеживания темы
# _bump_loop - Цикл для авто-поднятия темы
if self.config.enable_captcha:
await self.add_captcha()
if self.config.enable_auto_bump:
await asyncio.gather(self._main_loop(), self._bump_loop())
else:
@@ -388,6 +442,7 @@ class TelegramStarsBot:
except Exception as e:
logger.error(f"Ошибка в главном процессе бота: {e}")
raise
finally:
if self.client and self.client.is_connected:
try: