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: Resolve markdown parsing issues across Telegram and Slack platforms

cyhhaoff25a19c12f4
diff --git a/core/controller.py b/core/controller.py
index 67d10363d..09e171994 100644
--- a/core/controller.py
+++ b/core/controller.py
@@ -143,12 +143,14 @@ How it works:
Use the buttons below to manage your Claude Code sessions, or simply type any message to add it to the processing queue."""
target_context = self._get_target_context(context)
- formatted_welcome = self.im_client.format_markdown(welcome_text)
+ # For Telegram, send with Markdown parse mode (not MarkdownV2)
+ # For Slack, use markdown
+ parse_mode = 'Markdown' if self.config.platform == "telegram" else 'markdown'
await self.im_client.send_message_with_buttons(
target_context,
- formatted_welcome,
+ welcome_text,
keyboard,
- parse_mode='markdown'
+ parse_mode=parse_mode
)
async def handle_user_message(self, context: MessageContext, message: str):
@@ -167,21 +169,21 @@ Use the buttons below to manage your Claude Code sessions, or simply type any me
# Send confirmation as reply in the thread
confirmation_text = f"📝 *Received*\n⏳ *Processing...*"
- formatted_confirmation = self.im_client.format_markdown(confirmation_text)
+ parse_mode = 'Markdown' if self.config.platform == "telegram" else 'markdown'
confirmation_msg = await self.im_client.send_message(
target_context,
- formatted_confirmation,
- parse_mode='markdown',
+ confirmation_text,
+ parse_mode=parse_mode,
reply_to=user_message_ts # Reply to the user's message
)
else:
# For non-Slack platforms, just send confirmation
confirmation_text = f"📝 *Received*\n⏳ *Processing...*"
- formatted_confirmation = self.im_client.format_markdown(confirmation_text)
+ parse_mode = 'Markdown' if self.config.platform == "telegram" else 'markdown'
confirmation_msg = await self.im_client.send_message(
target_context,
- formatted_confirmation,
- parse_mode='markdown'
+ confirmation_text,
+ parse_mode=parse_mode
)
# Store thread context with the message (use user's message timestamp for Slack)
@@ -245,11 +247,13 @@ Use the buttons below to manage your Claude Code sessions, or simply type any me
if thread_id:
target_context.thread_id = thread_id
- formatted_msg = self.im_client.format_markdown(claude_msg)
+ # Don't escape Claude messages - they already contain proper markdown
+ # Use appropriate parse_mode for each platform
+ parse_mode = 'markdown' if self.config.platform == "slack" else 'Markdown'
await self.im_client.send_message(
target_context,
- formatted_msg,
- parse_mode='markdown',
+ claude_msg,
+ parse_mode=parse_mode,
reply_to=thread_id if self.config.platform == "slack" else None
)
@@ -384,21 +388,29 @@ Use the buttons below to manage your Claude Code sessions, or simply type any me
current_cwd = custom_cwd if custom_cwd else self.config.claude.cwd
absolute_path = os.path.abspath(current_cwd)
- response_text = f"📁 Current Working Directory:\n{absolute_path}"
+ # Build response using formatter to avoid escaping issues
+ formatter = self.im_client.formatter
- # Check if directory exists
+ # Format path properly with code block
+ path_line = f"📁 Current Working Directory:\n{formatter.format_code_inline(absolute_path)}"
+
+ # Build status lines
+ status_lines = []
if os.path.exists(absolute_path):
- response_text += "\n✅ Directory exists"
+ status_lines.append("✅ Directory exists")
else:
- response_text += "\n⚠️ Directory does not exist"
+ status_lines.append("⚠️ Directory does not exist")
if custom_cwd:
- response_text += "\n💡 (User custom setting)"
+ status_lines.append("💡 \\(User custom setting\\)")
else:
- response_text += "\n💡 (Default from .env)"
+ status_lines.append("💡 \\(Default from \\.env\\)")
- formatted_text = self.im_client.format_markdown(response_text)
- await self.im_client.send_message(context, formatted_text, parse_mode='markdown')
+ # Combine all parts
+ response_text = path_line + "\n" + "\n".join(status_lines)
+
+ parse_mode = 'Markdown' if self.config.platform == "telegram" else 'markdown'
+ await self.im_client.send_message(context, response_text, parse_mode=parse_mode)
except Exception as e:
logger.error(f"Error getting cwd: {e}")
await self.im_client.send_message(context, f"Error getting working directory: {str(e)}")
@@ -427,10 +439,10 @@ Use the buttons below to manage your Claude Code sessions, or simply type any me
return
if not os.path.isdir(absolute_path):
- await self.im_client.send_message(
- context,
- f"❌ Path exists but is not a directory: {absolute_path}"
- )
+ formatter = self.im_client.formatter
+ error_text = f"❌ Path exists but is not a directory: {formatter.format_code_inline(absolute_path)}"
+ parse_mode = 'Markdown' if self.config.platform == "telegram" else 'markdown'
+ await self.im_client.send_message(context, error_text, parse_mode=parse_mode)
return
# Save to user settings
@@ -439,13 +451,14 @@ Use the buttons below to manage your Claude Code sessions, or simply type any me
logger.info(f"User {context.user_id} changed cwd to: {absolute_path}")
+ formatter = self.im_client.formatter
response_text = (
f"✅ Working directory changed to:\n"
- f"{absolute_path}\n\n"
+ f"{formatter.format_code_inline(absolute_path)}\n\n"
f"This setting has been saved for your user."
)
- formatted_text = self.im_client.format_markdown(response_text)
- await self.im_client.send_message(context, formatted_text, parse_mode='markdown')
+ parse_mode = 'Markdown' if self.config.platform == "telegram" else 'markdown'
+ await self.im_client.send_message(context, response_text, parse_mode=parse_mode)
except Exception as e:
logger.error(f"Error setting cwd: {e}")
@@ -535,10 +548,11 @@ Use the buttons below to manage your Claude Code sessions, or simply type any me
message_types = self.settings_manager.get_available_message_types()
display_names = self.settings_manager.get_message_type_display_names()
- # Create inline keyboard buttons
+ # Create inline keyboard buttons in 2x2 layout
buttons = []
+ row = []
- for msg_type in message_types:
+ for i, msg_type in enumerate(message_types):
is_hidden = msg_type in user_settings.hidden_message_types
checkbox = "☑️" if is_hidden else "⬜"
display_name = display_names.get(msg_type, msg_type)
@@ -546,20 +560,25 @@ Use the buttons below to manage your Claude Code sessions, or simply type any me
text=f"{checkbox} Hide {display_name}",
callback_data=f"toggle_msg_{msg_type}"
)
- buttons.append([button]) # One button per row for now
+ row.append(button)
+
+ # Create 2x2 layout
+ if len(row) == 2 or i == len(message_types) - 1:
+ buttons.append(row)
+ row = []
- # Add info button
+ # Add info button on its own row
buttons.append([InlineButton("ℹ️ About Message Types", callback_data="info_msg_types")])
keyboard = InlineKeyboard(buttons=buttons)
- # Send settings message
+ # Send settings message with escaped dash
target_context = self._get_target_context(context)
await self.im_client.send_message_with_buttons(
target_context,
- "⚙️ *Settings - Message Visibility*\n\nSelect which message types to hide from Claude output:",
+ "⚙️ *Settings \\- Message Visibility*\n\nSelect which message types to hide from Claude output:",
keyboard,
- parse_mode='markdown'
+ parse_mode='Markdown' if self.config.platform == "telegram" else 'markdown'
)
async def _handle_settings_slack(self, context: MessageContext):
@@ -595,7 +614,7 @@ Use the buttons below to manage your Claude Code sessions, or simply type any me
target_context,
"⚙️ *Personalization Settings*\n\nConfigure how Claude Code messages appear in your Slack workspace.",
keyboard,
- parse_mode='markdown'
+ parse_mode='Markdown' if self.config.platform == "telegram" else 'markdown'
)
async def handle_callback_query(self, context: MessageContext, callback_data: str):
@@ -689,8 +708,8 @@ Use the buttons below to manage your Claude Code sessions, or simply type any me
_Tip: All commands work in DMs, channels, and threads!_"""
- formatted_text = self.im_client.format_markdown(info_text)
- await self.im_client.send_message(context, formatted_text, parse_mode='markdown')
+ parse_mode = 'Markdown' if self.config.platform == "telegram" else 'markdown'
+ await self.im_client.send_message(context, info_text, parse_mode=parse_mode)
return
# Handle settings toggle buttons (existing functionality)
@@ -743,23 +762,30 @@ _Tip: All commands work in DMs, channels, and threads!_"""
await self.im_client.send_message(
context,
f"{display_name} messages are now {action}",
- parse_mode='markdown'
+ parse_mode='Markdown' if self.config.platform == "telegram" else 'markdown'
)
elif callback_data == "info_msg_types":
# Show info about message types
- info_text = (
- "📋 *Message Types Info:*\n\n"
- "• *System* - System initialization and status messages\n"
- "• *Response* - Tool execution responses and results\n"
- "• *Assistant* - Claude's messages and explanations\n"
- "• *Result* - Final execution results and summaries\n\n"
+ formatter = self.im_client.formatter
+
+ # Build info text using formatter to handle escaping properly
+ lines = [
+ f"📋 {formatter.format_bold('Message Types Info:')}",
+ "",
+ f"• {formatter.format_bold('System')} - System initialization and status messages",
+ f"• {formatter.format_bold('Response')} - Tool execution responses and results",
+ f"• {formatter.format_bold('Assistant')} - Claude's messages and explanations",
+ f"• {formatter.format_bold('Result')} - Final execution results and summaries",
+ "",
"Hidden messages won't be sent to your IM platform."
- )
+ ]
+
+ info_text = "\n".join(lines)
# Send as new message
- formatted_text = self.im_client.format_markdown(info_text)
- await self.im_client.send_message(context, formatted_text, parse_mode='markdown')
+ parse_mode = 'Markdown' if self.config.platform == "telegram" else 'markdown'
+ await self.im_client.send_message(context, info_text, parse_mode=parse_mode)
except Exception as e:
logger.error(f"Error handling callback query: {e}")
diff --git a/modules/im/base.py b/modules/im/base.py
index 02dab4c3f..3f2afda39 100644
--- a/modules/im/base.py
+++ b/modules/im/base.py
@@ -100,6 +100,8 @@ class BaseIMClient(ABC):
self.on_message_callback: Optional[Callable] = None
self.on_command_callbacks: Dict[str, Callable] = {}
self.on_callback_query_callback: Optional[Callable] = None
+ # Platform-specific formatter will be set by subclasses
+ self.formatter = None
@abstractmethod
async def send_message(self, context: MessageContext, text: str,
diff --git a/modules/im/slack.py b/modules/im/slack.py
index f29a8d94e..76f7fea57 100644
--- a/modules/im/slack.py
+++ b/modules/im/slack.py
@@ -9,6 +9,7 @@ from slack_sdk.errors import SlackApiError
from .base import BaseIMClient, MessageContext, InlineKeyboard, InlineButton
from config.settings import SlackConfig
+from .formatters import SlackFormatter
logger = logging.getLogger(__name__)
@@ -22,6 +23,9 @@ class SlackBot(BaseIMClient):
self.web_client = None
self.socket_client = None
+ # Initialize Slack formatter
+ self.formatter = SlackFormatter()
+
# Note: Thread handling now uses user's message timestamp directly
# Store callback handlers
diff --git a/modules/im/telegram.py b/modules/im/telegram.py
index d4b1daa74..01de50717 100644
--- a/modules/im/telegram.py
+++ b/modules/im/telegram.py
@@ -7,6 +7,7 @@ from telegram.helpers import escape_markdown
from telegram.error import TelegramError
from config.settings import TelegramConfig
from .base import BaseIMClient, MessageContext, InlineKeyboard, InlineButton
+from .formatters import TelegramFormatter
logger = logging.getLogger(__name__)
@@ -17,6 +18,9 @@ class TelegramBot(BaseIMClient):
super().__init__(config)
self.application = Application.builder().token(config.bot_token).build()
+ # Initialize Telegram formatter
+ self.formatter = TelegramFormatter()
+
# Store callback queries for answering
self._callback_queries: Dict[str, Any] = {}
@@ -258,7 +262,14 @@ class TelegramBot(BaseIMClient):
}
if parse_mode:
- kwargs['parse_mode'] = parse_mode if parse_mode != 'markdown' else 'MarkdownV2'
+ # Handle different parse modes:
+ # 'markdown' (from Slack) -> 'Markdown' (Telegram v1)
+ # 'Markdown' -> keep as is (Telegram v1)
+ # 'MarkdownV2' -> keep as is (Telegram v2)
+ if parse_mode == 'markdown':
+ kwargs['parse_mode'] = 'Markdown'
+ else:
+ kwargs['parse_mode'] = parse_mode
if reply_to or context.thread_id:
kwargs['reply_to_message_id'] = int(reply_to or context.thread_id)
@@ -296,7 +307,7 @@ class TelegramBot(BaseIMClient):
message = await bot.send_message(
chat_id=chat_id,
text=text,
- parse_mode=parse_mode if parse_mode != 'markdown' else 'MarkdownV2',
+ parse_mode='Markdown' if parse_mode == 'markdown' else parse_mode,
reply_markup=reply_markup
)
return str(message.message_id)
@@ -420,13 +431,12 @@ class TelegramBot(BaseIMClient):
raise
def format_markdown(self, text: str) -> str:
- """Format markdown text for Telegram
+ """Format markdown text for Telegram using TelegramFormatter
- Telegram already uses double asterisks for bold, so we don't need to change much
+ This properly escapes special characters for MarkdownV2
"""
- # Telegram uses standard markdown with double asterisks for bold
- # Just return the text as-is since it's already in the right format
- return text
+ # Use the formatter to properly escape text
+ return self.formatter.escape_special_chars(text)
def _is_authorized_chat(self, chat_id: int, chat_type: str) -> bool:
"""Check if a chat is authorized based on whitelist configuration"""