diff --git a/mautrix_telegram/config.py b/mautrix_telegram/config.py index 14148881..0d725557 100644 --- a/mautrix_telegram/config.py +++ b/mautrix_telegram/config.py @@ -217,6 +217,7 @@ def do_update(self, helper: ConfigUpdateHelper) -> None: del self["bridge.message_formats"] copy_dict("bridge.message_formats", override_existing_map=False) copy("bridge.emote_format") + copy("bridge.convert_commands") copy("bridge.relay_user_distinguishers") copy("bridge.state_event_formats.join") diff --git a/mautrix_telegram/example-config.yaml b/mautrix_telegram/example-config.yaml index b23083e4..3fbb5080 100644 --- a/mautrix_telegram/example-config.yaml +++ b/mautrix_telegram/example-config.yaml @@ -475,7 +475,8 @@ bridge: # $username - Telegram username (may not exist) # $mention - Telegram @username or displayname mention (depending on which exists) emote_format: "* $mention $formatted_body" - + # Replace ! with / to send telegram bot commands without interfering with matrix client commands + convert_commands: true # The formats to use when sending state events to Telegram via the relay bot. # # Variables from `message_formats` that have the `sender_` prefix are available without the prefix. diff --git a/mautrix_telegram/formatter/from_matrix/__init__.py b/mautrix_telegram/formatter/from_matrix/__init__.py index 87210f08..195b4062 100644 --- a/mautrix_telegram/formatter/from_matrix/__init__.py +++ b/mautrix_telegram/formatter/from_matrix/__init__.py @@ -54,10 +54,14 @@ async def matrix_reply_to_telegram( async def matrix_to_telegram( - client: TelegramClient, *, text: str | None = None, html: str | None = None + client: TelegramClient, + *, + text: str | None = None, + html: str | None = None, + convert_commands: bool = True, ) -> tuple[str, list[TypeMessageEntity]]: if html is not None: - return await _matrix_html_to_telegram(client, html) + return await _matrix_html_to_telegram(client, html, convert_commands) elif text is not None: return _matrix_text_to_telegram(text) else: @@ -65,10 +69,11 @@ async def matrix_to_telegram( async def _matrix_html_to_telegram( - client: TelegramClient, html: str + client: TelegramClient, html: str, convert_commands: bool ) -> tuple[str, list[TypeMessageEntity]]: try: - html = command_regex.sub(r"\1", html) + if convert_commands: + html = command_regex.sub(r"\1", html) html = html.replace("\t", " " * 4) html = not_command_regex.sub(r"\1", html) @@ -99,7 +104,8 @@ def _cut_long_message( def _matrix_text_to_telegram(text: str) -> tuple[str, list[TypeMessageEntity]]: - text = command_regex.sub(r"/\1", text) + if convert_commands: + text = command_regex.sub(r"/\1", text) text = text.replace("\t", " " * 4) text = not_command_regex.sub(r"\1", text) entities = [] diff --git a/mautrix_telegram/portal.py b/mautrix_telegram/portal.py index 6f0f51f6..a47f0a2a 100644 --- a/mautrix_telegram/portal.py +++ b/mautrix_telegram/portal.py @@ -1503,7 +1503,11 @@ async def _send_state_change_message( message = await self._get_state_change_message(event, user, **kwargs) if not message: return - message, entities = await formatter.matrix_to_telegram(self.bot.client, html=message) + message, entities = await formatter.matrix_to_telegram( + self.bot.client, + html=message, + convert_commands=self.config["bridge.convert_commands"], + ) response = await self.bot.client.send_message( self.peer, message, formatting_entities=entities ) @@ -1789,7 +1793,10 @@ async def _handle_matrix_text( reply_to: TelegramID | None, ) -> None: message, entities = await formatter.matrix_to_telegram( - client, text=content.body, html=content.formatted(Format.HTML) + client, + text=content.body, + html=content.formatted(Format.HTML), + convert_commands=self.config["bridge.convert_commands"], ) sender_id = sender.tgid if logged_in else self.bot.tgid async with self.send_lock(sender_id): @@ -1933,7 +1940,10 @@ async def _handle_matrix_file( capt, entities = ( await formatter.matrix_to_telegram( - client, text=caption.body, html=caption.formatted(Format.HTML) + client, + text=caption.body, + html=caption.formatted(Format.HTML), + convert_commands=self.config["bridge.convert_commands"], ) if caption else (None, None) @@ -2030,7 +2040,9 @@ async def _handle_matrix_location( caption = content["org.matrix.msc3488.location"]["description"] entities = [] except KeyError: - caption, entities = await formatter.matrix_to_telegram(client, text=content.body) + caption, entities = await formatter.matrix_to_telegram( + client, text=content.body, convert_commands=self.config["bridge.convert_commands"] + ) media = MessageMediaGeo(geo=GeoPoint(lat=lat, long=long, access_hash=0)) async with self.send_lock(sender_id):