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.database import TgMessage, TgChat, User
|
||||||
from src.core.settings.base import settings
|
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.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
|
from src.telethon_client import telethon_client
|
||||||
|
|
||||||
@ -24,8 +26,19 @@ async def notify_for_success(
|
|||||||
text=message
|
text=message
|
||||||
)
|
)
|
||||||
|
|
||||||
await telethon_client.forward_messages(
|
for message in messages:
|
||||||
entity=settings.NOTIFY.CHAT_ID,
|
try:
|
||||||
messages=[message.id for message in messages],
|
await telethon_client.forward_messages(
|
||||||
from_peer=source_chat,
|
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
|
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(
|
def create_and_format_message(
|
||||||
@ -44,21 +58,6 @@ def create_and_format_message(
|
|||||||
chat: TgChat,
|
chat: TgChat,
|
||||||
user_model: User | Channel,
|
user_model: User | Channel,
|
||||||
) -> str:
|
) -> 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
|
# 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}"
|
||||||
@ -115,4 +114,11 @@ def handle_chat_type(
|
|||||||
else:
|
else:
|
||||||
raise ValueError(f"Invalid chat type check it!{chat}")
|
raise ValueError(f"Invalid chat type check it!{chat}")
|
||||||
|
|
||||||
return chat_type, chat_username
|
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