Skip to content

Commit

Permalink
optionally disable converting matrix-prefixed commands to telegram-pr…
Browse files Browse the repository at this point in the history
…efixed commands
  • Loading branch information
maltee1 committed May 10, 2024
1 parent 8bd5a4e commit 3d5ede5
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 10 deletions.
1 change: 1 addition & 0 deletions mautrix_telegram/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
3 changes: 2 additions & 1 deletion mautrix_telegram/example-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
16 changes: 11 additions & 5 deletions mautrix_telegram/formatter/from_matrix/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,21 +54,26 @@ 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:
raise ValueError("text or html must be provided to convert formatting")


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"<command>\1</command>", html)
if convert_commands:
html = command_regex.sub(r"<command>\1</command>", html)
html = html.replace("\t", " " * 4)
html = not_command_regex.sub(r"\1", html)

Expand Down Expand Up @@ -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 = []
Expand Down
20 changes: 16 additions & 4 deletions mautrix_telegram/portal.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit 3d5ede5

Please sign in to comment.