Добавлен Dockerfile и основной скрипт для интеграции с Trilium через Telegram бота
This commit is contained in:
53
main.py
Normal file
53
main.py
Normal file
@@ -0,0 +1,53 @@
|
||||
import os
|
||||
from dotenv import load_dotenv
|
||||
from aiogram import Bot, Dispatcher
|
||||
from aiogram.client.default import DefaultBotProperties
|
||||
from aiogram.enums import ParseMode
|
||||
from aiogram.filters import CommandStart
|
||||
from aiogram.types import Message
|
||||
from trilium_py.client import ETAPI
|
||||
|
||||
load_dotenv()
|
||||
|
||||
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")
|
||||
|
||||
# создаем Trilium API клиент
|
||||
ea = ETAPI(server_url=TRILIUM_URL, token=TRILIUM_TOKEN)
|
||||
|
||||
bot = Bot(token=TELEGRAM_TOKEN, default=DefaultBotProperties(parse_mode=ParseMode.HTML))
|
||||
|
||||
dp = Dispatcher()
|
||||
|
||||
|
||||
@dp.message(CommandStart())
|
||||
async def start(msg: Message):
|
||||
await msg.answer("Отправь мне текст — я создам заметку в Trilium.")
|
||||
|
||||
|
||||
@dp.message()
|
||||
async def handler(msg: Message):
|
||||
text = msg.text.strip()
|
||||
|
||||
# разделяем на заголовок и тело
|
||||
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"
|
||||
)
|
||||
|
||||
await msg.answer("Заметка сохранена в Trilium.")
|
||||
|
||||
|
||||
async def main():
|
||||
await dp.start_polling(bot)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
import asyncio
|
||||
|
||||
asyncio.run(main())
|
||||
Reference in New Issue
Block a user