DispelEvilBot/common/repositories/admin_messages_repo.py

53 lines
1.4 KiB
Python

"""Журнал ручных отправок сообщений из админки."""
from __future__ import annotations
from typing import Any
from psycopg.types.json import Jsonb
from common.db.pool import get_pool
async def log_message(
*,
editor_id: int | None,
chat_id: int,
text: str,
buttons: list[tuple[str, str]],
has_image: bool,
success: bool,
error: str | None = None,
) -> None:
async with get_pool().connection() as conn:
await conn.execute(
"""
INSERT INTO bot.admin_messages (editor_id, chat_id, text, buttons, has_image, success, error)
VALUES (%s, %s, %s, %s, %s, %s, %s)
""",
(
editor_id,
chat_id,
text,
Jsonb([{"text": button_text, "url": url} for button_text, url in buttons]),
has_image,
success,
error,
),
)
async def recent(*, limit: int = 10) -> list[dict[str, Any]]:
async with get_pool().connection() as conn:
cur = await conn.execute(
"""
SELECT id, editor_id, chat_id, left(text, 120) AS preview, buttons,
has_image, success, error, sent_at
FROM bot.admin_messages
ORDER BY sent_at DESC
LIMIT %s
""",
(limit,),
)
return await cur.fetchall()