add fix for sender success
This commit is contained in:
parent
65d527b9d2
commit
696cce8bdf
@ -1,7 +1,9 @@
|
||||
from telethon.errors.rpcerrorlist import MessageIdInvalidError
|
||||
|
||||
from src.core.database import TgMessage, TgChat, User
|
||||
from src.core.settings.base import settings
|
||||
from src.core.tg_service.notify_sender import send_to_tg_from_bot
|
||||
from src.core.tg_service.utils import create_and_format_message
|
||||
from src.core.tg_service.utils import create_and_format_message, create_text_for_error_message_success
|
||||
|
||||
from src.telethon_client import telethon_client
|
||||
|
||||
@ -24,8 +26,19 @@ async def notify_for_success(
|
||||
text=message
|
||||
)
|
||||
|
||||
await telethon_client.forward_messages(
|
||||
entity=settings.NOTIFY.CHAT_ID,
|
||||
messages=[message.id for message in messages],
|
||||
from_peer=source_chat,
|
||||
)
|
||||
for message in messages:
|
||||
try:
|
||||
await telethon_client.forward_messages(
|
||||
entity=settings.NOTIFY.CHAT_ID,
|
||||
messages=message.id,
|
||||
from_peer=source_chat,
|
||||
)
|
||||
except MessageIdInvalidError:
|
||||
await send_to_tg_from_bot(
|
||||
text=create_text_for_error_message_success(
|
||||
"🔒 Удаленное сообщение\n\n"
|
||||
"⚠️ Сообщение удалено или недоступно\n\n"
|
||||
f"📝 Текст:`{message.text}`"
|
||||
f"👤 ID_USER:`{message.user_id}`"
|
||||
)
|
||||
)
|
||||
|
@ -37,6 +37,20 @@ def check_message_condition(
|
||||
|
||||
return True
|
||||
|
||||
def escape_markdown_v2(text: str) -> str:
|
||||
if not text:
|
||||
return ""
|
||||
escape_chars = '_*[]()~`>#+-=|{}.!'
|
||||
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
|
||||
|
||||
|
||||
def create_and_format_message(
|
||||
@ -44,21 +58,6 @@ def create_and_format_message(
|
||||
chat: TgChat,
|
||||
user_model: User | Channel,
|
||||
) -> str:
|
||||
def escape_markdown_v2(text: str) -> str:
|
||||
if not text:
|
||||
return ""
|
||||
escape_chars = '_*[]()~`>#+-=|{}.!'
|
||||
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}"
|
||||
user_link = f"[{username}](tg://user?id={user_model.id})" if user_model.username else f"ID: {user_model.id}"
|
||||
@ -116,3 +115,10 @@ def handle_chat_type(
|
||||
raise ValueError(f"Invalid chat type check it!{chat}")
|
||||
|
||||
return chat_type, chat_username
|
||||
|
||||
|
||||
def create_text_for_error_message_success(
|
||||
text: str
|
||||
) -> str:
|
||||
text = escape_markdown_v2(text)
|
||||
return validate_markdown(text)
|
||||
|
Loading…
x
Reference in New Issue
Block a user