intra_max_chatbot/bot/send_helper.py
2026-03-26 15:39:02 +03:00

94 lines
2.9 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""Обёртки над bot.send_message / bot.edit_message с автоматическим логированием."""
from database.queries.message_log import log_message
async def bot_send(
bot,
*,
chat_id: int,
user_id: int | None,
text: str,
attachments=None,
) -> str | None:
"""Отправить новое сообщение. Возвращает message_id (mid) или None."""
kw: dict = {"chat_id": chat_id, "text": text}
if attachments is not None:
kw["attachments"] = attachments
result = await bot.send_message(**kw)
mid: str | None = None
if result and result.message and result.message.body:
mid = result.message.body.mid
await log_message(
direction="out",
max_user_id=user_id,
max_chat_id=chat_id,
message_id=mid,
text=text,
)
return mid
async def bot_edit(
event,
*,
text: str,
attachments=None,
) -> bool:
"""
Редактировать сообщение, на которое нажата callback-кнопка.
Возвращает True при успехе. При ошибке (сообщение удалено, слишком старое)
возвращает False — вызывающий код должен откатиться на bot_send.
"""
if event.message is None or event.message.body is None:
return False
mid = event.message.body.mid
chat_id = event.message.recipient.chat_id
user_id: int | None = getattr(event.callback, "user", None)
if user_id is not None:
user_id = getattr(user_id, "user_id", None)
try:
await event.bot.edit_message(
message_id=mid,
text=text,
attachments=attachments if attachments is not None else [],
)
await log_message(
direction="out",
max_user_id=user_id,
max_chat_id=chat_id,
message_id=mid,
text=text,
)
return True
except Exception:
return False
async def bot_reply(
event,
*,
text: str,
attachments=None,
user_id: int | None = None,
chat_id: int | None = None,
) -> None:
"""
Для callback-событий: пробует отредактировать текущее сообщение.
При неудаче отправляет новое.
Использование в callback-обработчиках вместо bot_send — не создаёт спам.
"""
edited = await bot_edit(event, text=text, attachments=attachments)
if not edited:
_chat_id = chat_id
if _chat_id is None and event.message is not None:
_chat_id = event.message.recipient.chat_id
if _chat_id is not None:
await bot_send(
event.bot,
chat_id=_chat_id,
user_id=user_id,
text=text,
attachments=attachments,
)