diff --git a/gateway/app.py b/gateway/app.py index d6abe27..63c6b77 100644 --- a/gateway/app.py +++ b/gateway/app.py @@ -45,13 +45,30 @@ def _error_alert_bot_token() -> str: return os.getenv("ERROR_TELEGRAM_ALERT_BOT_TOKEN", "").strip() +def _normalize_telegram_chat_id(raw_id: str) -> str: + value = raw_id.strip() + if not value: + return value + + lowered = value.lower() + for prefix in ("https://t.me/", "http://t.me/", "t.me/"): + if lowered.startswith(prefix): + value = value[len(prefix):].strip() + break + + if not value.startswith(("@", "-100")) and not value.isdigit(): + value = f"@{value}" + return value + + def _error_alert_chat_ids() -> list[str]: raw = os.getenv("ERROR_TELEGRAM_ALERT_CHAT_IDS", "").strip() if not raw: raw = os.getenv("ERROR_TELEGRAM_ALERT_CHANNEL_ID", "").strip() if not raw: raw = os.getenv("ERROR_TELEGRAM_ALERT_CHANNEL", "").strip() - return [item.strip() for item in raw.split(",") if item.strip()] + normalized = [_normalize_telegram_chat_id(item) for item in raw.split(",")] + return [item for item in normalized if item] def _error_alert_min_status() -> int: diff --git a/tests/test_gateway_proxy.py b/tests/test_gateway_proxy.py index 74d9388..43e396b 100644 --- a/tests/test_gateway_proxy.py +++ b/tests/test_gateway_proxy.py @@ -307,3 +307,9 @@ def test_gateway_queues_error_alert_for_internal_exception(monkeypatch): assert alerts[0]['service'] == 'api-gateway' assert alerts[0]['status'] == 500 assert alerts[0]['path'] == '/proxy/sales/pipelines' + + +def test_gateway_normalizes_channel_alias_for_t_me(monkeypatch): + monkeypatch.setenv('ERROR_TELEGRAM_ALERT_CHANNEL_ID', 't.me/konturaitelecom') + + assert gateway_module._error_alert_chat_ids() == ['@konturaitelecom']