add fix
This commit is contained in:
parent
31c4eda0d5
commit
340ecb4b39
@ -31,14 +31,25 @@ def create_and_format_message(
|
|||||||
user_model: User,
|
user_model: User,
|
||||||
) -> str:
|
) -> str:
|
||||||
def escape_markdown_v2(text: str) -> str:
|
def escape_markdown_v2(text: str) -> str:
|
||||||
|
if not text:
|
||||||
|
return ""
|
||||||
escape_chars = '_*[]()~`>#+-=|{}.!'
|
escape_chars = '_*[]()~`>#+-=|{}.!'
|
||||||
return ''.join('\\' + char if char in escape_chars else char for char in text)
|
return ''.join('\\' + char if char in escape_chars else char for char in text)
|
||||||
|
|
||||||
# Формирование информации о пользователе
|
def validate_markdown(text: str) -> str:
|
||||||
|
"""Ensure all markdown entities are properly closed"""
|
||||||
|
# Count backticks to ensure pairs
|
||||||
|
backtick_count = text.count('`')
|
||||||
|
if backtick_count % 3 != 0: # Code blocks use triple backticks
|
||||||
|
# Remove unpaired backticks
|
||||||
|
text = text.replace('`', 'ʻ') # Replace with similar-looking character
|
||||||
|
return text
|
||||||
|
|
||||||
|
# User info
|
||||||
username = escape_markdown_v2(user_model.username) if user_model.username else f"ID: {user_model.id}"
|
username = escape_markdown_v2(user_model.username) if user_model.username else f"ID: {user_model.id}"
|
||||||
user_link = f"[{username}](tg://user?id={user_model.id})" if user_model.username else f"ID: {user_model.id}"
|
user_link = f"[{username}](tg://user?id={user_model.id})" if user_model.username else f"ID: {user_model.id}"
|
||||||
|
|
||||||
# Формирование информации о чате
|
# Chat info
|
||||||
chat_title = escape_markdown_v2(chat.title)
|
chat_title = escape_markdown_v2(chat.title)
|
||||||
chat_link = (
|
chat_link = (
|
||||||
f"https://t.me/c/{str(chat.id)[4:]}"
|
f"https://t.me/c/{str(chat.id)[4:]}"
|
||||||
@ -47,10 +58,10 @@ def create_and_format_message(
|
|||||||
else f"Chat ID: {chat.id}"
|
else f"Chat ID: {chat.id}"
|
||||||
)
|
)
|
||||||
|
|
||||||
# Экранирование причины
|
# Escape reason
|
||||||
reason_escaped = escape_markdown_v2(reason)
|
reason_escaped = escape_markdown_v2(reason)
|
||||||
|
|
||||||
# Базовый заголовок сообщения
|
# Header
|
||||||
header = (
|
header = (
|
||||||
f"🔥 *Найдена успешка!*\n"
|
f"🔥 *Найдена успешка!*\n"
|
||||||
f"👤 *Пользователь:* {user_link}\n"
|
f"👤 *Пользователь:* {user_link}\n"
|
||||||
@ -59,24 +70,27 @@ def create_and_format_message(
|
|||||||
f"📝 *Диалог:*\n"
|
f"📝 *Диалог:*\n"
|
||||||
)
|
)
|
||||||
|
|
||||||
# Расчет доступной длины для контента
|
|
||||||
MAX_LENGTH = 4096
|
MAX_LENGTH = 4096
|
||||||
header_length = len(header)
|
header_length = len(header)
|
||||||
available_length = MAX_LENGTH - header_length
|
available_length = MAX_LENGTH - header_length - 50 # Extra buffer for safety
|
||||||
|
|
||||||
# Формирование блоков сообщений
|
|
||||||
message_blocks = []
|
message_blocks = []
|
||||||
current_length = 0
|
current_length = 0
|
||||||
|
|
||||||
for msg in messages:
|
for msg in messages:
|
||||||
# Очистка и подготовка текста
|
if not msg.text:
|
||||||
clean_text = msg.text.replace('```', 'ʻʻʻ') # Заменяем опасные символы
|
continue
|
||||||
|
|
||||||
|
# Clean and escape text
|
||||||
|
clean_text = msg.text.replace('```', 'ʻʻʻ') # Replace triple backticks
|
||||||
escaped_text = escape_markdown_v2(clean_text)
|
escaped_text = escape_markdown_v2(clean_text)
|
||||||
|
escaped_text = validate_markdown(escaped_text)
|
||||||
|
|
||||||
sender_username = escape_markdown_v2(
|
sender_username = escape_markdown_v2(
|
||||||
msg.user_relationship.username or str(msg.user_id)
|
msg.user_relationship.username or f"ID:{msg.user_id}"
|
||||||
)
|
)
|
||||||
# Формирование блока сообщения
|
|
||||||
|
# Create message block with proper code block formatting
|
||||||
block = (
|
block = (
|
||||||
f"**{sender_username}:**\n"
|
f"**{sender_username}:**\n"
|
||||||
f"```\n"
|
f"```\n"
|
||||||
@ -85,17 +99,16 @@ def create_and_format_message(
|
|||||||
)
|
)
|
||||||
block_length = len(block)
|
block_length = len(block)
|
||||||
|
|
||||||
# Проверка на превышение длины
|
|
||||||
if current_length + block_length > available_length:
|
if current_length + block_length > available_length:
|
||||||
remaining_space = available_length - current_length
|
remaining_space = available_length - current_length
|
||||||
if remaining_space > 20: # Минимальный значимый блок
|
if remaining_space > 30: # Enough space for a truncated message
|
||||||
truncated_text = escaped_text[:remaining_space - 20] + "..."
|
truncated_text = escaped_text[:remaining_space - 30].rsplit(' ', 1)[0] + "..."
|
||||||
block = (
|
block = (
|
||||||
f"**Пользователь:**\n"
|
f"**{sender_username}:**\n"
|
||||||
f"```\n"
|
f"```\n"
|
||||||
f"{truncated_text}\n"
|
f"{truncated_text}\n"
|
||||||
f"```\n\n"
|
f"```\n\n"
|
||||||
f"_... сообщение обрезано ..._"
|
f"_... сообщение обрезано ..._\n"
|
||||||
)
|
)
|
||||||
message_blocks.append(block)
|
message_blocks.append(block)
|
||||||
break
|
break
|
||||||
@ -103,12 +116,12 @@ def create_and_format_message(
|
|||||||
message_blocks.append(block)
|
message_blocks.append(block)
|
||||||
current_length += block_length
|
current_length += block_length
|
||||||
|
|
||||||
# Сборка финального сообщения
|
|
||||||
content = ''.join(message_blocks).strip()
|
content = ''.join(message_blocks).strip()
|
||||||
full_message = header + content
|
full_message = header + content
|
||||||
|
|
||||||
# Финальная проверка длины
|
# Final validation and truncation if needed
|
||||||
|
full_message = validate_markdown(full_message)
|
||||||
if len(full_message) > MAX_LENGTH:
|
if len(full_message) > MAX_LENGTH:
|
||||||
full_message = full_message[:MAX_LENGTH - 17] + "\n```\n...\n```\n_... сообщение обрезано ..._"
|
full_message = full_message[:MAX_LENGTH - 30].rsplit('\n', 2)[0] + "\n```\n...\n```\n_... сообщение обрезано ..._"
|
||||||
|
|
||||||
return full_message
|
return full_message
|
Binary file not shown.
Loading…
x
Reference in New Issue
Block a user