Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

optionally disable converting "!"-prefixed commands to "/"-prefixed commands #962

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
22 changes: 15 additions & 7 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)
return _matrix_text_to_telegram(text, convert_commands)
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 @@ -98,8 +103,11 @@ def _cut_long_message(
return message, entities


def _matrix_text_to_telegram(text: str) -> tuple[str, list[TypeMessageEntity]]:
text = command_regex.sub(r"/\1", text)
def _matrix_text_to_telegram(
text: str, convert_commands: bool
) -> tuple[str, list[TypeMessageEntity]]:
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