Skip to content

Personal AI, On Personal Devices

Python

fix(channels): forward Telegram allowed_chat_ids to the channel adapter (#1127)

Taras Zubara8367b9b83f2
diff --git a/src/openjarvis/system/_channel_kwargs.py b/src/openjarvis/system/_channel_kwargs.py
index bcd92f24..b4a45d55 100644
--- a/src/openjarvis/system/_channel_kwargs.py
+++ b/src/openjarvis/system/_channel_kwargs.py
@@ -11,6 +11,10 @@ def _telegram(c: Any) -> Dict[str, Any]:
kw["bot_token"] = c.bot_token
if c.parse_mode:
kw["parse_mode"] = c.parse_mode
+ # Forward the configured DM allowlist; without this the
+ # allowed_chat_ids setting was silently ignored.
+ if getattr(c, "allowed_chat_ids", ""):
+ kw["allowed_chat_ids"] = c.allowed_chat_ids
return kw
diff --git a/tests/channels/test_channel_config.py b/tests/channels/test_channel_config.py
index 0e7ad81d..b03d8605 100644
--- a/tests/channels/test_channel_config.py
+++ b/tests/channels/test_channel_config.py
@@ -195,3 +195,24 @@ bot_token = "slack-token"
assert cfg.channel.slack.bot_token == "slack-token"
finally:
path.unlink()
+
+
+class TestBuildChannelKwargs:
+ """Config→kwargs mappers must forward every enforced field."""
+
+ def test_telegram_forwards_allowed_chat_ids(self):
+ from openjarvis.system._channel_kwargs import build_channel_kwargs
+
+ cfg = ChannelConfig()
+ cfg.telegram.bot_token = "tok"
+ cfg.telegram.allowed_chat_ids = "111, 222"
+ kwargs = build_channel_kwargs(cfg, "telegram")
+ assert kwargs.get("bot_token") == "tok"
+ assert kwargs.get("allowed_chat_ids") == "111, 222"
+
+ def test_telegram_omits_empty_allowed_chat_ids(self):
+ from openjarvis.system._channel_kwargs import build_channel_kwargs
+
+ cfg = ChannelConfig()
+ kwargs = build_channel_kwargs(cfg, "telegram")
+ assert "allowed_chat_ids" not in kwargs
diff --git a/tests/channels/test_telegram.py b/tests/channels/test_telegram.py
index 7c6cff2d..3db8376f 100644
--- a/tests/channels/test_telegram.py
+++ b/tests/channels/test_telegram.py
@@ -30,6 +30,70 @@ TestCommonChannel = make_common_channel_tests(
)
+ ("allowlist", "accepted"),
+ [("111, -222", ["111", "-222"]), ("", ["999", "111", "-222"])],
+)
+def test_configured_allowlist_gates_handlers_and_events(
+ tmp_path, monkeypatch, allowlist, accepted
+):
+ """Follow TOML -> builder -> adapter -> inbound dispatch without a bot API."""
+ from openjarvis.core.config import load_config
+ from openjarvis.system.builder import SystemBuilder
+
+ config_path = tmp_path / "config.toml"
+ config_path.write_text(
+ '[channel]\nenabled = true\ndefault_channel = "telegram"\n'
+ '[channel.telegram]\nbot_token = "test-token"\n'
+ f'allowed_chat_ids = "{allowlist}"\n'
+ )
+ cfg = load_config(config_path)
+ bus = EventBus(record_history=True)
+ channel = SystemBuilder(config=cfg)._resolve_channel(cfg, bus)
+ assert isinstance(channel, TelegramChannel)
+ received = []
+ channel.on_message(received.append)
+
+ handlers = []
+ app = SimpleNamespace(add_handler=handlers.append)
+ builder = MagicMock()
+ builder.token.return_value = builder
+ builder.build.return_value = app
+ telegram_ext = SimpleNamespace(
+ ApplicationBuilder=lambda: builder,
+ MessageHandler=lambda _filters, callback: SimpleNamespace(callback=callback),
+ filters=SimpleNamespace(TEXT=object()),
+ )
+ monkeypatch.setitem(sys.modules, "telegram.ext", telegram_ext)
+
+ async def deliver_updates(_app, _stop):
+ assert len(handlers) == 1
+ for chat_id in (999, 111, -222):
+ # An allowed sender ID must not authorize an unlisted chat.
+ update = SimpleNamespace(
+ message=SimpleNamespace(
+ from_user=SimpleNamespace(id=111),
+ text=f"chat-{chat_id}",
+ message_id=1,
+ chat=SimpleNamespace(id=chat_id),
+ )
+ )
+ await handlers[0].callback(update, None)
+
+ monkeypatch.setattr(channel, "_run_polling_lifecycle", deliver_updates)
+ channel._poll_loop()
+
+ assert [message.conversation_id for message in received] == accepted
+ inbound = [
+ event
+ for event in bus.history
+ if event.event_type == EventType.CHANNEL_MESSAGE_RECEIVED
+ ]
+ assert [event.data["content"] for event in inbound] == [
+ f"chat-{chat_id}" for chat_id in accepted
+ ]
+
+
class TestInit:
def test_defaults(self):
ch = TelegramChannel()