fix(telegram): decode JSON-encoded allowlist strings before comma-split

This commit is contained in:
Konstantin Khlopkov
2026-09-12 21:59:32 +00:00
committed by Teknium
parent a15b8982d4
commit 5cc9b31b7a
2 changed files with 102 additions and 0 deletions
+19
View File
@@ -33,6 +33,23 @@ def _redact_telegram_error_text(error: object) -> str:
return "<telegram error redacted>"
def _decode_json_list_literal(raw):
"""Decode a JSON-encoded allowlist written by ``hermes config set``.
String-typed defaults keep list literals verbatim on write (``allowed_chats`` is
declared as ``""``), so the config can hold ``'["-100","-200"]'`` as a string.
Malformed JSON passes through unchanged and keeps the legacy comma-split path.
"""
if isinstance(raw, str) and raw.lstrip()[:1] == "[":
try:
loaded = json.loads(raw)
except ValueError:
return raw
if isinstance(loaded, list):
return loaded
return raw
def _consume_abandoned_task(task: asyncio.Task) -> None:
"""Observe a detached task's terminal exception to avoid noisy loop logs."""
try:
@@ -5058,6 +5075,7 @@ class TelegramAdapter(BasePlatformAdapter):
raw = self.config.extra.get(key)
if raw is None:
raw = _scoped_gate_env(env_name)
raw = _decode_json_list_literal(raw)
if isinstance(raw, list):
return {str(part).strip() for part in raw if str(part).strip()}
return {part.strip() for part in str(raw).split(",") if part.strip()}
@@ -5131,6 +5149,7 @@ class TelegramAdapter(BasePlatformAdapter):
raw = self.config.extra.get("ignored_threads")
if raw is None:
raw = _scoped_gate_env("TELEGRAM_IGNORED_THREADS")
raw = _decode_json_list_literal(raw)
ignored: set[int] = set()
for value in (raw if isinstance(raw, list) else str(raw).split(",")):
text = str(value).strip()