Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ htmlcov/
.env
.env.*
.ccremote
.ccremote-attachments
17 changes: 1 addition & 16 deletions src/ccremote/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand All @@ -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)
18 changes: 8 additions & 10 deletions src/ccremote/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)

Expand Down Expand Up @@ -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:
Expand All @@ -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)
Expand All @@ -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):
Expand Down
Loading