Skip to content

Your lightweight open-source personal agent.

Merge main into pr-436

Re-bin83433198ca6e
diff --cc nanobot/channels/telegram.py
index 99c64d40d,6c57288d4..81cf0ca9f
--- a/nanobot/channels/telegram.py
+++ b/nanobot/channels/telegram.py
@@@ -309,12 -512,8 +512,11 @@@ class TelegramChannel(BaseChannel)
if media_file and self._app:
try:
file = await self._app.bot.get_file(media_file.file_id)
- ext = self._get_extension(media_type, getattr(media_file, 'mime_type', None))
-
+ ext = self._get_extension(
- media_type,
++ media_type,
+ getattr(media_file, 'mime_type', None),
- getattr(media_file, 'file_name', None)
++ getattr(media_file, 'file_name', None),
+ )
-
# Save to workspace/media/
from pathlib import Path
media_dir = Path.home() / ".nanobot" / "media"
@@@ -388,14 -619,14 +622,19 @@@
except asyncio.CancelledError:
pass
except Exception as e:
- logger.debug(f"Typing indicator stopped for {chat_id}: {e}")
-
- def _get_extension(self, media_type: str, mime_type: str | None, filename: str | None = None) -> str:
- """
- Get file extension based on media type.
- If mime_type is unknown, try to get extension from filename.
- """
- # 1. Try known mime types
+ logger.debug("Typing indicator stopped for {}: {}", chat_id, e)
+
+ async def _on_error(self, update: object, context: ContextTypes.DEFAULT_TYPE) -> None:
+ """Log polling / handler errors instead of silently swallowing them."""
+ logger.error("Telegram error: {}", context.error)
+
- def _get_extension(self, media_type: str, mime_type: str | None) -> str:
- """Get file extension based on media type."""
++ def _get_extension(
++ self,
++ media_type: str,
++ mime_type: str | None,
++ filename: str | None = None,
++ ) -> str:
++ """Get file extension based on media type or original filename."""
if mime_type:
ext_map = {
"image/jpeg": ".jpg", "image/png": ".png", "image/gif": ".gif",
@@@ -403,15 -634,6 +642,14 @@@
}
if mime_type in ext_map:
return ext_map[mime_type]
-
- # 2. Try simple type mapping
- type_map = {"image": ".jpg", "voice": ".ogg", "audio": ".mp3"}
- if media_type in type_map:
- return type_map[media_type]
-
- # 3. Fallback: try to get extension from filename
+
+ type_map = {"image": ".jpg", "voice": ".ogg", "audio": ".mp3", "file": ""}
- return type_map.get(media_type, "")
++ if ext := type_map.get(media_type, ""):
++ return ext
++
+ if filename:
+ from pathlib import Path
- return Path(filename).suffix
-
++
++ return "".join(Path(filename).suffixes)
++
+ return ""
diff --cc tests/test_telegram_channel.py
index 000000000,610054978..acd2a9687
mode 000000,100644..100644
--- a/tests/test_telegram_channel.py
+++ b/tests/test_telegram_channel.py
@@@ -1,0 -1,162 +1,169 @@@
+ from types import SimpleNamespace
+
+ import pytest
+
+ from nanobot.bus.events import OutboundMessage
+ from nanobot.bus.queue import MessageBus
+ from nanobot.channels.telegram import TelegramChannel
+ from nanobot.config.schema import TelegramConfig
+
+
+ class _FakeHTTPXRequest:
+ instances: list["_FakeHTTPXRequest"] = []
+
+ def __init__(self, **kwargs) -> None:
+ self.kwargs = kwargs
+ self.__class__.instances.append(self)
+
+
+ class _FakeUpdater:
+ def __init__(self, on_start_polling) -> None:
+ self._on_start_polling = on_start_polling
+
+ async def start_polling(self, **kwargs) -> None:
+ self._on_start_polling()
+
+
+ class _FakeBot:
+ def __init__(self) -> None:
+ self.sent_messages: list[dict] = []
+
+ async def get_me(self):
+ return SimpleNamespace(username="nanobot_test")
+
+ async def set_my_commands(self, commands) -> None:
+ self.commands = commands
+
+ async def send_message(self, **kwargs) -> None:
+ self.sent_messages.append(kwargs)
+
+
+ class _FakeApp:
+ def __init__(self, on_start_polling) -> None:
+ self.bot = _FakeBot()
+ self.updater = _FakeUpdater(on_start_polling)
+ self.handlers = []
+ self.error_handlers = []
+
+ def add_error_handler(self, handler) -> None:
+ self.error_handlers.append(handler)
+
+ def add_handler(self, handler) -> None:
+ self.handlers.append(handler)
+
+ async def initialize(self) -> None:
+ pass
+
+ async def start(self) -> None:
+ pass
+
+
+ class _FakeBuilder:
+ def __init__(self, app: _FakeApp) -> None:
+ self.app = app
+ self.token_value = None
+ self.request_value = None
+ self.get_updates_request_value = None
+
+ def token(self, token: str):
+ self.token_value = token
+ return self
+
+ def request(self, request):
+ self.request_value = request
+ return self
+
+ def get_updates_request(self, request):
+ self.get_updates_request_value = request
+ return self
+
+ def proxy(self, _proxy):
+ raise AssertionError("builder.proxy should not be called when request is set")
+
+ def get_updates_proxy(self, _proxy):
+ raise AssertionError("builder.get_updates_proxy should not be called when request is set")
+
+ def build(self):
+ return self.app
+
+
+ @pytest.mark.asyncio
+ async def test_start_uses_request_proxy_without_builder_proxy(monkeypatch) -> None:
+ config = TelegramConfig(
+ enabled=True,
+ token="123:abc",
+ allow_from=["*"],
+ proxy="http://127.0.0.1:7890",
+ )
+ bus = MessageBus()
+ channel = TelegramChannel(config, bus)
+ app = _FakeApp(lambda: setattr(channel, "_running", False))
+ builder = _FakeBuilder(app)
+
+ monkeypatch.setattr("nanobot.channels.telegram.HTTPXRequest", _FakeHTTPXRequest)
+ monkeypatch.setattr(
+ "nanobot.channels.telegram.Application",
+ SimpleNamespace(builder=lambda: builder),
+ )
+
+ await channel.start()
+
+ assert len(_FakeHTTPXRequest.instances) == 1
+ assert _FakeHTTPXRequest.instances[0].kwargs["proxy"] == config.proxy
+ assert builder.request_value is _FakeHTTPXRequest.instances[0]
+ assert builder.get_updates_request_value is _FakeHTTPXRequest.instances[0]
+
+
+ def test_derive_topic_session_key_uses_thread_id() -> None:
+ message = SimpleNamespace(
+ chat=SimpleNamespace(type="supergroup"),
+ chat_id=-100123,
+ message_thread_id=42,
+ )
+
+ assert TelegramChannel._derive_topic_session_key(message) == "telegram:-100123:topic:42"
+
+
++def test_get_extension_falls_back_to_original_filename() -> None:
++ channel = TelegramChannel(TelegramConfig(), MessageBus())
++
++ assert channel._get_extension("file", None, "report.pdf") == ".pdf"
++ assert channel._get_extension("file", None, "archive.tar.gz") == ".tar.gz"
++
++
+ @pytest.mark.asyncio
+ async def test_send_progress_keeps_message_in_topic() -> None:
+ config = TelegramConfig(enabled=True, token="123:abc", allow_from=["*"])
+ channel = TelegramChannel(config, MessageBus())
+ channel._app = _FakeApp(lambda: None)
+
+ await channel.send(
+ OutboundMessage(
+ channel="telegram",
+ chat_id="123",
+ content="hello",
+ metadata={"_progress": True, "message_thread_id": 42},
+ )
+ )
+
+ assert channel._app.bot.sent_messages[0]["message_thread_id"] == 42
+
+
+ @pytest.mark.asyncio
+ async def test_send_reply_infers_topic_from_message_id_cache() -> None:
+ config = TelegramConfig(enabled=True, token="123:abc", allow_from=["*"], reply_to_message=True)
+ channel = TelegramChannel(config, MessageBus())
+ channel._app = _FakeApp(lambda: None)
+ channel._message_threads[("123", 10)] = 42
+
+ await channel.send(
+ OutboundMessage(
+ channel="telegram",
+ chat_id="123",
+ content="hello",
+ metadata={"message_id": 10},
+ )
+ )
+
+ assert channel._app.bot.sent_messages[0]["message_thread_id"] == 42
+ assert channel._app.bot.sent_messages[0]["reply_parameters"].message_id == 10