feat: chat language display, disabled EntitySelect items, dev scripts

Chat language:
- Added language_code field to TelegramChat model + migration
- Saved from message.from.language_code on webhook/polling
- Displayed as badge on bot chat cards and target receiver items
- Resolved from DB in target API response (works for existing receivers)
- Shown in chat picker dropdown (desc includes language)

EntitySelect improvements:
- Tracker-target link selector shows all targets, already-linked ones
  appear disabled with "Already linked" hint
- Receiver chat picker shows already-added chats as disabled

Dev scripts:
- scripts/restart-backend.sh and restart-frontend.sh
- Updated .claude/docs/dev-servers.md to reference scripts
This commit is contained in:
2026-03-22 23:39:52 +03:00
parent e90c128dca
commit 82e400ddcd
16 changed files with 161 additions and 122 deletions
@@ -10,7 +10,7 @@ from fastapi import APIRouter, Depends, Header, HTTPException, Request
from sqlmodel import select
from sqlmodel.ext.asyncio.session import AsyncSession
from notify_bridge_core.notifications.telegram.media import TELEGRAM_API_BASE_URL
from notify_bridge_core.notifications.telegram.client import TelegramClient
from ..database.engine import get_session
from ..database.models import TelegramBot
@@ -68,50 +68,37 @@ async def telegram_webhook(
return {"ok": True, "skipped": "empty"}
# Auto-persist chat from incoming message
from_user = message.get("from", {})
msg_language = from_user.get("language_code", "")
try:
await save_chat_from_webhook(session, bot.id, chat_info)
await save_chat_from_webhook(session, bot.id, chat_info, language_code=msg_language)
await session.commit()
except Exception:
_LOGGER.warning("Failed to auto-save chat %s", chat_id, exc_info=True)
# Handle commands
if text.startswith("/"):
language_code = message.get("from", {}).get("language_code", "")
cmd_response = await handle_command(bot, chat_id, text, language_code=language_code)
message_id = message.get("message_id")
cmd_response = await handle_command(bot, chat_id, text, language_code=msg_language)
if cmd_response is not None:
if isinstance(cmd_response, list):
await send_media_group(bot.token, chat_id, cmd_response)
await send_media_group(bot.token, chat_id, cmd_response, reply_to_message_id=message_id)
else:
await send_reply(bot.token, chat_id, cmd_response)
await send_reply(bot.token, chat_id, cmd_response, reply_to_message_id=message_id)
return {"ok": True}
return {"ok": True, "skipped": "not_a_command"}
async def register_webhook(bot_token: str, webhook_url: str, secret: str | None = None) -> dict:
"""Register webhook URL with Telegram Bot API."""
"""Register webhook URL with Telegram Bot API via TelegramClient."""
async with aiohttp.ClientSession() as http:
url = f"{TELEGRAM_API_BASE_URL}{bot_token}/setWebhook"
payload: dict[str, Any] = {"url": webhook_url}
if secret:
payload["secret_token"] = secret
try:
async with http.post(url, json=payload) as resp:
result = await resp.json()
if result.get("ok"):
return {"success": True}
return {"success": False, "error": result.get("description")}
except aiohttp.ClientError as err:
return {"success": False, "error": str(err)}
client = TelegramClient(http, bot_token)
return await client.set_webhook(webhook_url, secret=secret)
async def unregister_webhook(bot_token: str) -> dict:
"""Remove webhook from Telegram Bot API."""
"""Remove webhook from Telegram Bot API via TelegramClient."""
async with aiohttp.ClientSession() as http:
url = f"{TELEGRAM_API_BASE_URL}{bot_token}/deleteWebhook"
try:
async with http.post(url) as resp:
result = await resp.json()
return {"success": result.get("ok", False)}
except aiohttp.ClientError as err:
return {"success": False, "error": str(err)}
client = TelegramClient(http, bot_token)
return await client.delete_webhook()