import asyncio import logging from contextlib import suppress from aiogram import Bot, Dispatcher, F from aiogram.client.default import DefaultBotProperties from aiogram.client.session.aiohttp import AiohttpSession from aiogram.enums import ParseMode from aiogram.types import Message from config import load_config from modules.auth import AllowedUserFilter from modules.journal import Journal from modules.reminders import ReminderState, send_reminders async def run_bot() -> None: logging.basicConfig(level=logging.INFO) config = load_config() session = AiohttpSession(proxy=config.telegram_proxy) if config.telegram_proxy else None bot = Bot( token=config.bot_token, session=session, default=DefaultBotProperties(parse_mode=ParseMode.HTML), ) dispatcher = Dispatcher() journal = Journal( vault_path=config.vault_path, daily_notes_dir=config.daily_notes_dir, note_path_format=config.note_path_format, message_time_format=config.message_time_format, ) reminder_state = ReminderState() @dispatcher.message(AllowedUserFilter(config.allowed_user_ids), F.text) async def save_message(message: Message) -> None: await journal.append(message.text or "") reminder_state.mark_manual_entry(config.reminder_timezone) await message.answer("Записал") @dispatcher.message() async def ignore_message(message: Message) -> None: if message.from_user and message.from_user.id not in config.allowed_user_ids: logging.warning("Rejected message from user_id=%s", message.from_user.id) reminder_task = asyncio.create_task( send_reminders( bot=bot, user_ids=config.allowed_user_ids, min_seconds=config.reminder_min_seconds, max_seconds=config.reminder_max_seconds, start_hour=config.reminder_start_hour, end_hour=config.reminder_end_hour, timezone=config.reminder_timezone, state=reminder_state, ) ) try: await dispatcher.start_polling(bot) finally: reminder_task.cancel() with suppress(asyncio.CancelledError): await reminder_task def main() -> None: asyncio.run(run_bot()) if __name__ == "__main__": main()