Skip to content

Open-source AI teammates in Slack, Teams, Discord, Telegram and Mattermost. Self-host it, with any agent harness and any model.

fix(telegram): configure explicit network timeouts on application builder

mrtuxcoder935b46d8ecfe
diff --git a/core/switch_core/bridges/collaboration/telegram/adapter.py b/core/switch_core/bridges/collaboration/telegram/adapter.py
index e097024d7..dc6c50da6 100644
--- a/core/switch_core/bridges/collaboration/telegram/adapter.py
+++ b/core/switch_core/bridges/collaboration/telegram/adapter.py
@@ -712,7 +712,15 @@ class TelegramAdapter(PlatformAdapter):
return
assert self._config.bot_token is not None # guaranteed by the validator
- app = ApplicationBuilder().token(self._config.bot_token).build()
+ app = (
+ ApplicationBuilder()
+ .token(self._config.bot_token)
+ .connect_timeout(30.0)
+ .read_timeout(30.0)
+ .get_updates_connect_timeout(30.0)
+ .get_updates_read_timeout(30.0)
+ .build()
+ )
app.add_handler(TypeHandler(Update, self._make_on_update()))
self._app = app
self._bot = app.bot
diff --git a/core/tests/switch_core/bridges/collaboration/test_telegram_adapter.py b/core/tests/switch_core/bridges/collaboration/test_telegram_adapter.py
index 743d8b06e..e4baeb95a 100644
--- a/core/tests/switch_core/bridges/collaboration/test_telegram_adapter.py
+++ b/core/tests/switch_core/bridges/collaboration/test_telegram_adapter.py
@@ -1545,10 +1545,30 @@ class _FakeApplication:
class _FakeBuilder:
def __init__(self, app: _FakeApplication) -> None:
self._app = app
+ self.connect_timeout_val: float | None = None
+ self.read_timeout_val: float | None = None
+ self.get_updates_connect_timeout_val: float | None = None
+ self.get_updates_read_timeout_val: float | None = None
def token(self, _token: str) -> _FakeBuilder:
return self
+ def connect_timeout(self, timeout: float) -> _FakeBuilder:
+ self.connect_timeout_val = timeout
+ return self
+
+ def read_timeout(self, timeout: float) -> _FakeBuilder:
+ self.read_timeout_val = timeout
+ return self
+
+ def get_updates_connect_timeout(self, timeout: float) -> _FakeBuilder:
+ self.get_updates_connect_timeout_val = timeout
+ return self
+
+ def get_updates_read_timeout(self, timeout: float) -> _FakeBuilder:
+ self.get_updates_read_timeout_val = timeout
+ return self
+
def build(self) -> _FakeApplication:
return self._app
@@ -1577,6 +1597,22 @@ def test_starting_begins_polling_and_learns_the_bot_id() -> None:
]
+def test_starting_configures_network_timeouts_on_application_builder() -> None:
+ adapter = TelegramAdapter(
+ config=TelegramConnectionConfig(bot_token="token", bot_username=BOT_USERNAME)
+ )
+ app = _FakeApplication()
+ builder = _FakeBuilder(app)
+
+ with patch.object(adapter_module, "ApplicationBuilder", lambda: builder):
+ _run(adapter.start(_noop, _noop, _noop, _noop, _noop))
+
+ assert builder.connect_timeout_val == 30.0
+ assert builder.read_timeout_val == 30.0
+ assert builder.get_updates_connect_timeout_val == 30.0
+ assert builder.get_updates_read_timeout_val == 30.0
+
+
def test_a_quiet_copy_calls_the_bot_api_without_polling() -> None:
"""Another switch-core process holds the bridge and polls Telegram for it:
Telegram hands each update to one poller, so this copy must not take a