feat(bot): add chat proxy

This commit is contained in:
h
2026-01-29 16:15:52 +01:00
parent fc19240952
commit 0e14e945bd
5 changed files with 221 additions and 5 deletions

View File

@@ -0,0 +1,101 @@
from dataclasses import dataclass
from aiogram import Bot, Router, types
from aiogram.client.default import DefaultBotProperties
from aiogram.enums import ParseMode
from aiogram.filters import Command
router = Router()
@dataclass
class ProxyConfig:
bot_token: str
target_chat_id: int
proxy_bot: Bot
proxy_states: dict[int, ProxyConfig] = {}
def get_proxy_config(chat_id: int) -> ProxyConfig | None:
return proxy_states.get(chat_id)
async def cleanup_proxy(chat_id: int) -> None:
config = proxy_states.pop(chat_id, None)
if config:
await config.proxy_bot.session.close()
@router.message(Command("proxy"))
async def on_proxy(message: types.Message) -> None: # noqa: C901
if not message.from_user or not message.text:
return
chat_id = message.chat.id
args = message.text.split(maxsplit=2)
if len(args) == 1:
config = get_proxy_config(chat_id)
if config:
await message.answer(
f"Proxy active → chat {config.target_chat_id}\n\n"
"Use /proxy deactivate to stop."
)
else:
await message.answer(
"Usage:\n"
"/proxy BOT_TOKEN CHAT_ID - activate proxy\n"
"/proxy deactivate - stop proxy"
)
return
if args[1].lower() == "deactivate":
if chat_id in proxy_states:
await cleanup_proxy(chat_id)
await message.answer("✓ Proxy deactivated.")
else:
await message.answer("No active proxy.")
return
if len(args) < 3: # noqa: PLR2004
await message.answer("Usage: /proxy BOT_TOKEN CHAT_ID")
return
bot_token = args[1]
try:
target_chat_id = int(args[2])
except ValueError:
await message.answer("Invalid chat ID. Must be a number.")
return
if chat_id in proxy_states:
await cleanup_proxy(chat_id)
try:
proxy_bot = Bot(
token=bot_token, default=DefaultBotProperties(parse_mode=ParseMode.HTML)
)
bot_info = await proxy_bot.get_me()
try:
await proxy_bot.send_message(target_chat_id, "🔗 Proxy connected")
except Exception as e: # noqa: BLE001
await proxy_bot.session.close()
await message.answer(f"Cannot send to chat {target_chat_id}: {e}")
return
proxy_states[chat_id] = ProxyConfig(
bot_token=bot_token, target_chat_id=target_chat_id, proxy_bot=proxy_bot
)
await message.answer(
f"✓ Proxy activated via @{bot_info.username}\n"
f"Target: {target_chat_id}\n\n"
"All messages will be forwarded."
)
await message.delete()
except Exception as e: # noqa: BLE001
await message.answer(f"Invalid bot token: {e}")