forked from HSE_team/BetterCallPraskovia
UTF 8 вместо абракадабры
This commit is contained in:
parent
ef71c67683
commit
683f779c31
@ -3,6 +3,7 @@ from aiogram.types import Message, InlineKeyboardMarkup, InlineKeyboardButton, C
|
|||||||
from aiogram.filters import Command, StateFilter
|
from aiogram.filters import Command, StateFilter
|
||||||
from aiogram.fsm.context import FSMContext
|
from aiogram.fsm.context import FSMContext
|
||||||
import aiohttp
|
import aiohttp
|
||||||
|
from urllib.parse import unquote
|
||||||
from tg_bot.config.settings import settings
|
from tg_bot.config.settings import settings
|
||||||
from tg_bot.infrastructure.http_client import create_http_session
|
from tg_bot.infrastructure.http_client import create_http_session
|
||||||
from tg_bot.infrastructure.telegram.states.collection_states import (
|
from tg_bot.infrastructure.telegram.states.collection_states import (
|
||||||
@ -10,6 +11,18 @@ from tg_bot.infrastructure.telegram.states.collection_states import (
|
|||||||
CollectionEditStates
|
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()
|
router = Router()
|
||||||
|
|
||||||
|
|
||||||
@ -243,7 +256,7 @@ async def cmd_search(message: Message):
|
|||||||
|
|
||||||
response = f"<b>Результаты поиска:</b> \"{query}\"\n\n"
|
response = f"<b>Результаты поиска:</b> \"{query}\"\n\n"
|
||||||
for i, doc in enumerate(results[:5], 1):
|
for i, doc in enumerate(results[:5], 1):
|
||||||
title = doc.get("title", "Без названия")
|
title = decode_title(doc.get("title", "Без названия"))
|
||||||
content = doc.get("content", "")[:200]
|
content = doc.get("content", "")[:200]
|
||||||
response += f"{i}. <b>{title}</b>\n"
|
response += f"{i}. <b>{title}</b>\n"
|
||||||
response += f" <i>{content}...</i>\n\n"
|
response += f" <i>{content}...</i>\n\n"
|
||||||
@ -378,7 +391,7 @@ async def show_collection_documents(callback: CallbackQuery):
|
|||||||
|
|
||||||
for i, doc in enumerate(documents[:10], 1):
|
for i, doc in enumerate(documents[:10], 1):
|
||||||
doc_id = doc.get("document_id")
|
doc_id = doc.get("document_id")
|
||||||
title = doc.get("title", "Без названия")
|
title = decode_title(doc.get("title", "Без названия"))
|
||||||
content_preview = doc.get("content", "")[:100]
|
content_preview = doc.get("content", "")[:100]
|
||||||
response += f"{i}. <b>{title}</b>\n"
|
response += f"{i}. <b>{title}</b>\n"
|
||||||
if content_preview:
|
if content_preview:
|
||||||
|
|||||||
@ -6,6 +6,7 @@ from aiogram.types import Message, InlineKeyboardMarkup, InlineKeyboardButton, C
|
|||||||
from aiogram.filters import StateFilter
|
from aiogram.filters import StateFilter
|
||||||
from aiogram.fsm.context import FSMContext
|
from aiogram.fsm.context import FSMContext
|
||||||
import aiohttp
|
import aiohttp
|
||||||
|
from urllib.parse import unquote
|
||||||
from tg_bot.config.settings import settings
|
from tg_bot.config.settings import settings
|
||||||
from tg_bot.infrastructure.http_client import create_http_session
|
from tg_bot.infrastructure.http_client import create_http_session
|
||||||
from tg_bot.infrastructure.telegram.states.collection_states import (
|
from tg_bot.infrastructure.telegram.states.collection_states import (
|
||||||
@ -13,6 +14,18 @@ from tg_bot.infrastructure.telegram.states.collection_states import (
|
|||||||
DocumentUploadStates
|
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()
|
router = Router()
|
||||||
|
|
||||||
|
|
||||||
@ -108,7 +121,7 @@ async def view_document(callback: CallbackQuery):
|
|||||||
)
|
)
|
||||||
return
|
return
|
||||||
|
|
||||||
title = document.get("title", "Без названия")
|
title = decode_title(document.get("title", "Без названия"))
|
||||||
content = document.get("content", "")
|
content = document.get("content", "")
|
||||||
collection_id = document.get("collection_id")
|
collection_id = document.get("collection_id")
|
||||||
|
|
||||||
@ -184,7 +197,7 @@ async def edit_document_prompt(callback: CallbackQuery, state: FSMContext):
|
|||||||
await callback.message.answer(
|
await callback.message.answer(
|
||||||
"<b>Редактирование документа</b>\n\n"
|
"<b>Редактирование документа</b>\n\n"
|
||||||
"Отправьте новое название документа или /skip чтобы оставить текущее.\n\n"
|
"Отправьте новое название документа или /skip чтобы оставить текущее.\n\n"
|
||||||
f"Текущее название: <b>{document.get('title', 'Без названия')}</b>",
|
f"Текущее название: <b>{decode_title(document.get('title', 'Без названия'))}</b>",
|
||||||
parse_mode="HTML"
|
parse_mode="HTML"
|
||||||
)
|
)
|
||||||
await callback.answer()
|
await callback.answer()
|
||||||
@ -361,7 +374,7 @@ async def process_upload_document(message: Message, state: FSMContext):
|
|||||||
if result:
|
if result:
|
||||||
await message.answer(
|
await message.answer(
|
||||||
f"<b>✅ Документ загружен и добавлен в коллекцию</b>\n\n"
|
f"<b>✅ Документ загружен и добавлен в коллекцию</b>\n\n"
|
||||||
f"<b>Название:</b> {result.get('title', filename)}\n\n"
|
f"<b>Название:</b> {decode_title(result.get('title', filename))}\n\n"
|
||||||
f"📄 Документ сейчас индексируется. Вы получите уведомление, когда индексация завершится.\n\n",
|
f"📄 Документ сейчас индексируется. Вы получите уведомление, когда индексация завершится.\n\n",
|
||||||
parse_mode="HTML"
|
parse_mode="HTML"
|
||||||
)
|
)
|
||||||
|
|||||||
@ -83,7 +83,10 @@ async def process_premium_question(message: Message, user: User, question_text:
|
|||||||
title = source.get('title', 'Без названия')
|
title = source.get('title', 'Без названия')
|
||||||
try:
|
try:
|
||||||
from urllib.parse import unquote
|
from urllib.parse import unquote
|
||||||
title = unquote(title)
|
decoded = unquote(title)
|
||||||
|
# Если декодирование изменило строку или исходная содержит %XX
|
||||||
|
if decoded != title or '%' in title:
|
||||||
|
title = decoded
|
||||||
except:
|
except:
|
||||||
pass
|
pass
|
||||||
response += f" {idx}. {title}\n"
|
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', 'Без названия')
|
title = source.get('title', 'Без названия')
|
||||||
try:
|
try:
|
||||||
from urllib.parse import unquote
|
from urllib.parse import unquote
|
||||||
title = unquote(title)
|
decoded = unquote(title)
|
||||||
|
# Если декодирование изменило строку или исходная содержит %XX
|
||||||
|
if decoded != title or '%' in title:
|
||||||
|
title = decoded
|
||||||
except:
|
except:
|
||||||
pass
|
pass
|
||||||
response += f" {idx}. {title}\n"
|
response += f" {idx}. {title}\n"
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user