diff --git a/example-config.yaml b/example-config.yaml index 438c4bef..f0863de6 100644 --- a/example-config.yaml +++ b/example-config.yaml @@ -180,6 +180,7 @@ bridge: # The formats to use when sending messages to Telegram via the relay bot. + # Text msgtypes (m.text, m.notice and m.emote) support HTML, media msgtypes don't. # # Telegram doesn't have built-in emotes, so the m.emote format is also used for non-relaybot users. # @@ -187,15 +188,17 @@ bridge: # $sender_displayname - The display name of the sender (e.g. Example User) # $sender_username - The username (Matrix ID localpart) of the sender (e.g. exampleuser) # $sender_mxid - The Matrix ID of the sender (e.g. @exampleuser:example.com) - # $message - The message content as HTML + # $body - The plaintext body (file name for media msgtypes) + # $formatted_body - The message content as HTML (for text msgtypes) message_formats: - m.text: "$sender_displayname: $message" - m.emote: "* $sender_displayname $message" - m.file: "$sender_displayname sent a file: $message" - m.image: "$sender_displayname sent an image: $message" - m.audio: "$sender_displayname sent an audio file: $message" - m.video: "$sender_displayname sent a video: $message" - m.location: "$sender_displayname sent a location: $message" + m.text: "$sender_displayname: $formatted_body" + m.notice: "$sender_displayname: $formatted_body" + m.emote: "* $sender_displayname $formatted_body" + m.file: "$sender_displayname sent a file: $body" + m.image: "$sender_displayname sent an image: $body" + m.audio: "$sender_displayname sent an audio file: $body" + m.video: "$sender_displayname sent a video: $body" + m.location: "$sender_displayname sent a location: $body" # The formats to use when sending state events to Telegram via the relay bot. # diff --git a/mautrix_telegram/portal/matrix.py b/mautrix_telegram/portal/matrix.py index cd575d68..136ed844 100644 --- a/mautrix_telegram/portal/matrix.py +++ b/mautrix_telegram/portal/matrix.py @@ -167,7 +167,7 @@ class PortalMatrix(BasePortal, MautrixBasePortal, ABC): async def _apply_msg_format(self, sender: 'u.User', content: MessageEventContent ) -> None: - if not content.formatted_body or content.format != Format.HTML: + if isinstance(content, TextMessageEventContent) and content.format != Format.HTML: content.format = Format.HTML content.formatted_body = escape_html(content.body).replace("\n", "
") @@ -177,8 +177,14 @@ class PortalMatrix(BasePortal, MautrixBasePortal, ABC): tpl_args = dict(sender_mxid=sender.mxid, sender_username=sender.mxid_localpart, sender_displayname=escape_html(displayname), - message=content.formatted_body) - content.formatted_body = Template(tpl).safe_substitute(tpl_args) + body=content.body) + if isinstance(content, TextMessageEventContent): + tpl_args["formatted_body"] = content.formatted_body + tpl_args["message"] = content.formatted_body + content.formatted_body = Template(tpl).safe_substitute(tpl_args) + else: + tpl_args["message"] = content.body + content.body = Template(tpl).safe_substitute(tpl_args) async def _pre_process_matrix_message(self, sender: 'u.User', use_relaybot: bool, content: MessageEventContent) -> None: