diff --git a/.gitignore b/.gitignore index bfa8e6a..12755b7 100644 --- a/.gitignore +++ b/.gitignore @@ -18,3 +18,4 @@ htmlcov/ .env .env.* .ccremote +.ccremote-attachments diff --git a/src/ccremote/bot.py b/src/ccremote/bot.py index 707bb4b..8149435 100644 --- a/src/ccremote/bot.py +++ b/src/ccremote/bot.py @@ -7,6 +7,7 @@ from aiogram import Bot, Dispatcher, F from aiogram.enums import ParseMode +from aiogram.types import BotCommand, BotCommandScopeChat from ccremote.markdown import md_to_tg_html @@ -54,8 +55,6 @@ async def register_commands(bot: Bot, chat_id: int, commands: list[tuple[str, st Fetches current commands first and only calls set_my_commands if they differ. """ - from aiogram.types import BotCommand, BotCommandScopeChat - if not commands: await unregister_commands(bot, chat_id) return @@ -82,22 +81,8 @@ async def register_commands(bot: Bot, chat_id: int, commands: list[tuple[str, st async def unregister_commands(bot: Bot, chat_id: int) -> None: """Remove all bot commands for this chat.""" - from aiogram.types import BotCommandScopeChat - try: await bot.delete_my_commands(scope=BotCommandScopeChat(chat_id=chat_id)) logger.info("Unregistered commands for chat %s", chat_id) except Exception: logger.exception("Failed to unregister commands for chat %s", chat_id) - - -async def notify_user(bot: Bot, chat_id: int, text: str) -> None: - """Send a notification DM.""" - try: - await bot.send_message( - chat_id=chat_id, - text=md_to_tg_html(text), - parse_mode=ParseMode.HTML, - ) - except Exception: - logger.debug("Could not notify user %s", chat_id) diff --git a/src/ccremote/cli.py b/src/ccremote/cli.py index 9862909..652445d 100644 --- a/src/ccremote/cli.py +++ b/src/ccremote/cli.py @@ -7,9 +7,15 @@ import contextlib import logging import sys +import uuid from pathlib import Path +from aiogram import Bot + +from ccremote.bot import create_dispatcher, send_message from ccremote.config import SETUP_INSTRUCTIONS, ConfigError, config_file_exists, load_config +from ccremote.models import Session +from ccremote.relay import setup_relay_handlers logger = logging.getLogger(__name__) @@ -42,14 +48,6 @@ def main() -> None: ) async def _run() -> None: - import uuid - - from aiogram import Bot - - from ccremote.bot import create_dispatcher, notify_user - from ccremote.models import Session - from ccremote.relay import setup_relay_handlers - dp = create_dispatcher(config) async with Bot(token=config.bot_token) as bot: @@ -62,7 +60,7 @@ async def _run() -> None: ) msg = f"🟢 **ccremote active**\n`{cwd}`" - await notify_user(bot, config.allowed_user, msg) + await send_message(bot, config.allowed_user, msg) logger.info("ccremote active in %s — send messages to @%s", cwd, me.username) setup_relay_handlers(dp, bot, session, config) @@ -73,7 +71,7 @@ async def _run() -> None: logger.info("Stopping ccremote...") session.terminate() with contextlib.suppress(Exception): - await notify_user(bot, config.allowed_user, "🔴 **ccremote stopped**") + await send_message(bot, config.allowed_user, "🔴 **ccremote stopped**") logger.info("Stopped.") with contextlib.suppress(KeyboardInterrupt):