Skip to content

armgit / avibe

Public Imported from GitHub
0

Make your computer a home for your AI partner. Avibe is the local-first Agent OS: Vibey, its built-in AI partner, works out of the box, and Claude Code, Codex & OpenCode run unchanged beside it — reach them from your browser, phone, or any chat app.

fix(telegram): preserve reply target on rich markdown fallback

cyh87e876867ca1
diff --git a/modules/im/telegram.py b/modules/im/telegram.py
index 46ba20f91..e19540e43 100644
--- a/modules/im/telegram.py
+++ b/modules/im/telegram.py
@@ -879,6 +879,25 @@ class TelegramBot(BaseIMClient):
description = str(err).lower()
return "method not found" in description
+ async def _send_message_with_buttons_payload(
+ self,
+ context: MessageContext,
+ text: str,
+ keyboard: InlineKeyboard,
+ *,
+ parse_mode: Optional[str] = None,
+ reply_to: Optional[str] = None,
+ ) -> str:
+ payload = self._build_payload(
+ context,
+ self.format_markdown(text),
+ keyboard=keyboard,
+ reply_to=reply_to,
+ parse_mode=parse_mode,
+ )
+ result = await telegram_api.call_api(self.config.bot_token, "sendMessage", payload, proxy_url=self._proxy_url)
+ return str(result["result"]["message_id"])
+
async def send_markdown_message(
self,
context: MessageContext,
@@ -894,7 +913,13 @@ class TelegramBot(BaseIMClient):
"""
if not self._should_send_rich_markdown(text):
if keyboard is not None:
- return await self.send_message_with_buttons(context, text, keyboard, parse_mode="markdown")
+ return await self._send_message_with_buttons_payload(
+ context,
+ text,
+ keyboard,
+ parse_mode="markdown",
+ reply_to=reply_to,
+ )
return await self.send_message(context, text, parse_mode="markdown", reply_to=reply_to)
payload = self._build_rich_message_payload(context, text, keyboard=keyboard, reply_to=reply_to)
@@ -910,7 +935,13 @@ class TelegramBot(BaseIMClient):
self._rich_markdown_supported = False
logger.warning("Telegram sendRichMessage failed; falling back to sendMessage", exc_info=True)
if keyboard is not None:
- return await self.send_message_with_buttons(context, text, keyboard, parse_mode="markdown")
+ return await self._send_message_with_buttons_payload(
+ context,
+ text,
+ keyboard,
+ parse_mode="markdown",
+ reply_to=reply_to,
+ )
return await self.send_message(context, text, parse_mode="markdown", reply_to=reply_to)
self._rich_markdown_supported = True
return str(result["result"]["message_id"])
diff --git a/tests/test_telegram_bot.py b/tests/test_telegram_bot.py
index 39a6a656a..c36f3c6a2 100644
--- a/tests/test_telegram_bot.py
+++ b/tests/test_telegram_bot.py
@@ -640,6 +640,27 @@ def test_send_markdown_message_uses_rich_message_for_structured_markdown() -> No
}
+def test_send_markdown_message_preserves_reply_to_on_plain_button_path() -> None:
+ bot = TelegramBot(TelegramConfig(bot_token="123456:test-token"))
+ context = MessageContext(user_id="42", channel_id="-100123", platform="telegram")
+ keyboard = InlineKeyboard(buttons=[[InlineButton(text="Inspect", callback_data="quick_reply:Inspect")]])
+
+ with patch(
+ "modules.im.telegram.telegram_api.call_api",
+ new=AsyncMock(return_value={"result": {"message_id": 88}}),
+ ) as call_mock:
+ result = asyncio.run(bot.send_markdown_message(context, "Hello **world**", keyboard=keyboard, reply_to="77"))
+
+ assert result == "88"
+ assert call_mock.await_args.args[1] == "sendMessage"
+ payload = call_mock.await_args.args[2]
+ assert payload["reply_parameters"] == {"message_id": 77}
+ assert payload["reply_markup"] == {
+ "inline_keyboard": [[{"text": "Inspect", "callback_data": "quick_reply:Inspect"}]]
+ }
+ assert payload["parse_mode"] == "HTML"
+
+
def test_send_markdown_message_falls_back_when_rich_message_is_rejected() -> None:
bot = TelegramBot(TelegramConfig(bot_token="123456:test-token"))
context = MessageContext(user_id="42", channel_id="-100123", platform="telegram")
@@ -663,6 +684,32 @@ def test_send_markdown_message_falls_back_when_rich_message_is_rejected() -> Non
assert fallback_payload["text"] == "# Summary\n\n- shipped"
+def test_send_markdown_message_preserves_reply_to_on_rejected_rich_button_fallback() -> None:
+ bot = TelegramBot(TelegramConfig(bot_token="123456:test-token"))
+ context = MessageContext(user_id="42", channel_id="-100123", platform="telegram")
+ keyboard = InlineKeyboard(buttons=[[InlineButton(text="Inspect", callback_data="quick_reply:Inspect")]])
+ markdown = "# Summary\n\n- shipped"
+
+ with patch(
+ "modules.im.telegram.telegram_api.call_api",
+ new=AsyncMock(
+ side_effect=[
+ RuntimeError("Bad Request: rich message is invalid"),
+ {"result": {"message_id": 89}},
+ ]
+ ),
+ ) as call_mock:
+ result = asyncio.run(bot.send_markdown_message(context, markdown, keyboard=keyboard, reply_to="77"))
+
+ assert result == "89"
+ assert [call.args[1] for call in call_mock.await_args_list] == ["sendRichMessage", "sendMessage"]
+ fallback_payload = call_mock.await_args_list[1].args[2]
+ assert fallback_payload["reply_parameters"] == {"message_id": 77}
+ assert fallback_payload["reply_markup"] == {
+ "inline_keyboard": [[{"text": "Inspect", "callback_data": "quick_reply:Inspect"}]]
+ }
+
+
def test_send_markdown_message_disables_rich_path_after_method_missing() -> None:
bot = TelegramBot(TelegramConfig(bot_token="123456:test-token"))
context = MessageContext(user_id="42", channel_id="-100123", platform="telegram")