fix: log MCP bot's own outbound messages to database

Bot can't see its own messages via Telegram polling. Now logs them
directly after sending so they appear in pull_updates.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Mikkel Georgsen 2026-03-30 11:55:32 +00:00
parent 2a88b528d4
commit 8ede43395e

View file

@ -196,21 +196,36 @@ class BridgeBot:
return None
async def send_to_group(self, text: str, attribution: str = "claude.ai"):
"""Send an attributed message to the group chat."""
"""Send an attributed message to the group chat and log it."""
formatted = f"*\\[{attribution}\\]* {self._escape_markdown(text)}"
result = None
try:
await self.bot.send_message(
result = await self.bot.send_message(
chat_id=self.group_chat_id,
text=formatted,
parse_mode="MarkdownV2",
)
except Exception:
# Fallback to plain text if markdown fails
plain = f"[{attribution}] {text}"
await self.bot.send_message(
result = await self.bot.send_message(
chat_id=self.group_chat_id,
text=plain,
)
finally:
# Log the sent message so it appears in pull_updates
if result:
from datetime import datetime, timezone
self.db.insert_message(
telegram_message_id=result.message_id,
chat_id=self.group_chat_id,
sender_type="mcp_bot",
sender_id=self._my_bot_id,
sender_name=f"[{attribution}]",
content=text,
reply_to_message_id=None,
has_attachment=False,
created_at=datetime.now(timezone.utc).isoformat(),
)
@staticmethod
def _escape_markdown(text: str) -> str: