diff --git a/Dockerfile b/Dockerfile index cef071b..64b0f7d 100644 --- a/Dockerfile +++ b/Dockerfile @@ -9,8 +9,7 @@ RUN apt-get update && apt-get install -y --no-install-recommends \ WORKDIR /app -COPY main.py . -COPY requirements.txt . +COPY main.py requirements.txt ./ RUN pip install --no-cache-dir -r requirements.txt CMD ["python", "main.py"] diff --git a/main.py b/main.py index a9238f4..e43d997 100644 --- a/main.py +++ b/main.py @@ -1,4 +1,5 @@ import os +from datetime import datetime from dotenv import load_dotenv from aiogram import Bot, Dispatcher from aiogram.client.default import DefaultBotProperties @@ -14,6 +15,7 @@ TELEGRAM_TOKEN = os.getenv("TELEGRAM_TOKEN") TRILIUM_URL = os.getenv("TRILIUM_URL") TRILIUM_TOKEN = os.getenv("TRILIUM_TOKEN") INBOX_NOTE_ID = os.getenv("INBOX_NOTE_ID") +DAILY_NOTE_ID = os.getenv("DAILY_NOTE_ID") # ID папки для ежедневных заметок # создаем Trilium API клиент ea = ETAPI(server_url=TRILIUM_URL, token=TRILIUM_TOKEN) @@ -32,16 +34,114 @@ async def start(msg: Message): async def handler(msg: Message): text = msg.text.strip() + # Проверяем флаг /daily + is_daily = text.startswith("/daily") + + if is_daily: + # Удаляем флаг из текста + text = text[6:].strip() # удаляем "/daily" + # разделяем на заголовок и тело lines = text.split("\n", 1) title = lines[0][:100] # заголовок = первая строка content = lines[1] if len(lines) > 1 else "" # остальное — тело заметки - ea.create_note( - parentNoteId=INBOX_NOTE_ID, title=title, content=content, type="text" - ) + if is_daily: + # Работаем с ежедневной заметкой + today = datetime.now() + month_name = ( + today.strftime("%m - %B") + .replace("January", "Январь") + .replace("February", "Февраль") + .replace("March", "Март") + .replace("April", "Апрель") + .replace("May", "Май") + .replace("June", "Июнь") + .replace("July", "Июль") + .replace("August", "Август") + .replace("September", "Сентябрь") + .replace("October", "Октябрь") + .replace("November", "Ноябрь") + .replace("December", "Декабрь") + ) + day_name = ( + today.strftime("%d - %A") + .replace("Monday", "Понедельник") + .replace("Tuesday", "Вторник") + .replace("Wednesday", "Среда") + .replace("Thursday", "Четверг") + .replace("Friday", "Пятница") + .replace("Saturday", "Суббота") + .replace("Sunday", "Воскресенье") + ) + today_date = today.strftime("%Y-%m-%d") - await msg.answer("Заметка сохранена в Trilium.") + try: + # Получаем папку с ежедневными заметками + daily_root = ea.get_note(DAILY_NOTE_ID) + month_folder = None + + # Ищем папку месяца + if hasattr(daily_root, "children"): + for child in daily_root.children: + if month_name in child.title: + month_folder = child + break + + if not month_folder: + # Создаём папку месяца, если её нет + month_folder = ea.create_note( + parentNoteId=DAILY_NOTE_ID, title=month_name, type="text" + ) + + # Ищем папку дня внутри месячной папки + day_folder = None + if hasattr(month_folder, "children"): + for child in month_folder.children: + if day_name in child.title: + day_folder = child + break + + if not day_folder: + # Создаём папку дня, если её нет + day_folder = ea.create_note( + parentNoteId=month_folder.noteId, title=day_name, type="text" + ) + + # Ищем заметку с сегодняшней датой + existing_note = None + if hasattr(day_folder, "children"): + for child in day_folder.children: + if child.title == today_date: + existing_note = child + break + + if existing_note: + # Дописываем в существующую заметку + new_content = ( + existing_note.content + "\n\n---\n" + content + if existing_note.content + else content + ) + ea.update_note(noteId=existing_note.noteId, content=new_content) + await msg.answer(f"Добавлено в ежедневную заметку за {today_date}.") + else: + # Создаём новую заметку на сегодня + ea.create_note( + parentNoteId=day_folder.noteId, + title=today_date, + content=content, + type="text", + ) + await msg.answer(f"Создана ежедневная заметка за {today_date}.") + except Exception as e: + await msg.answer(f"Ошибка при работе с ежедневной заметкой: {str(e)}") + else: + # Обычное сохранение в Inbox + ea.create_note( + parentNoteId=INBOX_NOTE_ID, title=title, content=content, type="text" + ) + await msg.answer("Заметка сохранена в Trilium. Сто пудов!") async def main():