refactor: review fixes and code improvements

- Add shared proxy module (bot/utils/proxy.py) to eliminate duplicate SOCKS5 parsing
- Fix AI client: escape HTML before Markdown→HTML conversion, unify timeouts,
  make health check optional and disabled by default, handle 429 retries
- Fix ai.py: correct forwarded message handling (aiogram 3.x forward_origin),
  pass relevant summaries via extra_system_content
- Fix dialogue.py: only respond to Astra's messages, use system context instead
  of prompt injection, answer on the limit message before phase transition
- Fix voice.py: load Whisper model in thread pool, safe WAV path generation
- Improve database.py: composite indexes, Boolean is_active, upsert file_id cache,
  add context cleanup helper
- Update weather.py and yadisk_download.py to use shared proxy connector
- Update yadisk.py: validate URL before cache clear, add download size limit,
  wrap sync file ops in to_thread
- Reuse S3 client via lru_cache
- Update setup_commands with /aiclear and /aiuser
- Update README, Dockerfile (Python 3.11), docker-compose (mount models)
- Pin dependency versions, remove unused httpx[socks]
- Add basic pytest tests for layout converter and voice normalization
This commit is contained in:
Галингер Р.С.
2026-07-07 18:42:10 +07:00
parent 76e5701eba
commit 0f674f8832
23 changed files with 894 additions and 578 deletions
+21 -10
View File
@@ -19,6 +19,7 @@ from config import PROXY_ENABLED
setup_logging()
logger = logging.getLogger(__name__)
# Router order matters: moderation first so it can delete messages before others process them.
dp.include_router(moderation.router)
dp.include_router(layout.router)
dp.include_router(weather.router)
@@ -37,9 +38,9 @@ async def save_user_info(message: Message):
message.from_user.username,
message.from_user.full_name or "",
)
except Exception:
pass
raise SkipHandler()
except Exception as exc:
logger.warning("Failed to save chat user: %s", exc)
raise SkipHandler
async def main():
@@ -47,31 +48,41 @@ async def main():
logging.info("База данных инициализирована")
await setup_bot_commands()
asyncio.create_task(start_model_health_check())
health_check_task = asyncio.create_task(start_model_health_check())
try:
if PROXY_ENABLED:
await setup_proxy()
else:
me = await bot.me()
logging.info(f"Бот запущен без прокси: @{me.username}")
except TelegramNetworkError as e:
logging.info("Бот запущен без прокси: @%s", me.username)
except TelegramNetworkError:
if PROXY_ENABLED:
logging.error("Не могу соединиться с прокси сервером. Попробуй другой прокси.")
else:
logging.error("Не могу соединиться с Telegram. Проверь интернет-соединение.")
health_check_task.cancel()
return
except ClientConnectionError as e:
except ClientConnectionError:
if PROXY_ENABLED:
logging.error("Не могу соединиться с прокси сервером. Попробуй другой прокси.")
else:
logging.error("Не могу соединиться с Telegram. Проверь интернет-соединение.")
health_check_task.cancel()
return
except Exception as e:
logging.error(f"Ошибка при запуске бота: {e}")
except Exception as exc:
logging.error("Ошибка при запуске бота: %s", exc)
health_check_task.cancel()
return
await dp.start_polling(bot)
try:
await dp.start_polling(bot)
finally:
health_check_task.cancel()
try:
await health_check_task
except asyncio.CancelledError:
pass
if __name__ == "__main__":