diff --git a/tg_bot/infrastructure/telegram/handlers/collection_handler.py b/tg_bot/infrastructure/telegram/handlers/collection_handler.py
index c4080de..2166f7e 100644
--- a/tg_bot/infrastructure/telegram/handlers/collection_handler.py
+++ b/tg_bot/infrastructure/telegram/handlers/collection_handler.py
@@ -3,6 +3,7 @@ from aiogram.types import Message, InlineKeyboardMarkup, InlineKeyboardButton, C
from aiogram.filters import Command, StateFilter
from aiogram.fsm.context import FSMContext
import aiohttp
+from urllib.parse import unquote
from tg_bot.config.settings import settings
from tg_bot.infrastructure.http_client import create_http_session
from tg_bot.infrastructure.telegram.states.collection_states import (
@@ -10,6 +11,18 @@ from tg_bot.infrastructure.telegram.states.collection_states import (
CollectionEditStates
)
+
+def decode_title(title: str) -> str:
+ if not title:
+ return "Без названия"
+ try:
+ decoded = unquote(title)
+ if decoded != title or '%' not in title:
+ return decoded
+ return title
+ except Exception:
+ return title
+
router = Router()
@@ -243,7 +256,7 @@ async def cmd_search(message: Message):
response = f"Результаты поиска: \"{query}\"\n\n"
for i, doc in enumerate(results[:5], 1):
- title = doc.get("title", "Без названия")
+ title = decode_title(doc.get("title", "Без названия"))
content = doc.get("content", "")[:200]
response += f"{i}. {title}\n"
response += f" {content}...\n\n"
@@ -378,7 +391,7 @@ async def show_collection_documents(callback: CallbackQuery):
for i, doc in enumerate(documents[:10], 1):
doc_id = doc.get("document_id")
- title = doc.get("title", "Без названия")
+ title = decode_title(doc.get("title", "Без названия"))
content_preview = doc.get("content", "")[:100]
response += f"{i}. {title}\n"
if content_preview:
diff --git a/tg_bot/infrastructure/telegram/handlers/document_handler.py b/tg_bot/infrastructure/telegram/handlers/document_handler.py
index a5f0818..be47366 100644
--- a/tg_bot/infrastructure/telegram/handlers/document_handler.py
+++ b/tg_bot/infrastructure/telegram/handlers/document_handler.py
@@ -6,6 +6,7 @@ from aiogram.types import Message, InlineKeyboardMarkup, InlineKeyboardButton, C
from aiogram.filters import StateFilter
from aiogram.fsm.context import FSMContext
import aiohttp
+from urllib.parse import unquote
from tg_bot.config.settings import settings
from tg_bot.infrastructure.http_client import create_http_session
from tg_bot.infrastructure.telegram.states.collection_states import (
@@ -13,6 +14,18 @@ from tg_bot.infrastructure.telegram.states.collection_states import (
DocumentUploadStates
)
+
+def decode_title(title: str) -> str:
+ """Декодирует URL-encoded название документа"""
+ if not title:
+ return "Без названия"
+ try:
+ decoded = unquote(title)
+ if decoded != title or '%' not in title:
+ return decoded
+ return title
+ except Exception:
+ return title
router = Router()
@@ -108,7 +121,7 @@ async def view_document(callback: CallbackQuery):
)
return
- title = document.get("title", "Без названия")
+ title = decode_title(document.get("title", "Без названия"))
content = document.get("content", "")
collection_id = document.get("collection_id")
@@ -184,7 +197,7 @@ async def edit_document_prompt(callback: CallbackQuery, state: FSMContext):
await callback.message.answer(
"Редактирование документа\n\n"
"Отправьте новое название документа или /skip чтобы оставить текущее.\n\n"
- f"Текущее название: {document.get('title', 'Без названия')}",
+ f"Текущее название: {decode_title(document.get('title', 'Без названия'))}",
parse_mode="HTML"
)
await callback.answer()
@@ -361,7 +374,7 @@ async def process_upload_document(message: Message, state: FSMContext):
if result:
await message.answer(
f"✅ Документ загружен и добавлен в коллекцию\n\n"
- f"Название: {result.get('title', filename)}\n\n"
+ f"Название: {decode_title(result.get('title', filename))}\n\n"
f"📄 Документ сейчас индексируется. Вы получите уведомление, когда индексация завершится.\n\n",
parse_mode="HTML"
)
diff --git a/tg_bot/infrastructure/telegram/handlers/question_handler.py b/tg_bot/infrastructure/telegram/handlers/question_handler.py
index b775e86..1efc778 100644
--- a/tg_bot/infrastructure/telegram/handlers/question_handler.py
+++ b/tg_bot/infrastructure/telegram/handlers/question_handler.py
@@ -83,7 +83,10 @@ async def process_premium_question(message: Message, user: User, question_text:
title = source.get('title', 'Без названия')
try:
from urllib.parse import unquote
- title = unquote(title)
+ decoded = unquote(title)
+ # Если декодирование изменило строку или исходная содержит %XX
+ if decoded != title or '%' in title:
+ title = decoded
except:
pass
response += f" {idx}. {title}\n"
@@ -148,7 +151,10 @@ async def process_free_question(message: Message, user: User, question_text: str
title = source.get('title', 'Без названия')
try:
from urllib.parse import unquote
- title = unquote(title)
+ decoded = unquote(title)
+ # Если декодирование изменило строку или исходная содержит %XX
+ if decoded != title or '%' in title:
+ title = decoded
except:
pass
response += f" {idx}. {title}\n"