feat(bot): add basic in-db message storage (no attachments yet)

This commit is contained in:
h
2025-08-25 00:49:06 +03:00
parent d21ff78727
commit 1d22cfd3db
6 changed files with 99 additions and 0 deletions

View File

@@ -9,6 +9,7 @@ client = AsyncMongoClient(env.db.connection_url)
async def init_db():
from .models import (
DynamicConfig,
MessageLog,
RespondSession,
ReviewSession,
)
@@ -19,5 +20,6 @@ async def init_db():
DynamicConfig,
RespondSession,
ReviewSession,
MessageLog,
],
)

View File

@@ -1,2 +1,3 @@
from .config import DynamicConfig
from .message_log import MessageLog
from .session import RespondSession, ReviewSession

View File

@@ -0,0 +1,44 @@
import datetime
from typing import Annotated, Optional
from aiogram.enums.message_origin_type import MessageOriginType
from beanie import Document, Indexed
from pydantic import BaseModel, Field
class MessageAttachment(BaseModel):
file_id: str
file_size: int
file_name: str
mime_type: str
class MessageLogBase(BaseModel):
chat_id: Annotated[int, Indexed()]
message_id: Annotated[int, Indexed()]
user_id: Annotated[int, Indexed()]
username: Optional[str] = None
first_name: str
last_name: Optional[str] = None
text: Optional[str]
timestamp: datetime.datetime
reply_to_message_id: Optional[int] = None
forward_from_type: Optional[MessageOriginType] = None
original_message_date: Optional[datetime.datetime] = None
original_message_author: Optional[str] = None
original_message_author_id: Optional[int] = None
attachment_file_ids: list[str] = Field(default_factory=list)
class MessageLog(MessageLogBase, Document):
class Settings:
name = "message_logs"
indexes = [[("chat_id", 1), ("timestamp", -1)]]
@classmethod
def get_n_messages(cls, chat_id: int, n: int):
return cls.find(cls.chat_id == chat_id).sort("timestamp", "-").limit(n)