From 0d264e09a838de6af9182f2ca99f237b210a3979 Mon Sep 17 00:00:00 2001 From: Travis Ralston Date: Thu, 7 Jun 2018 13:29:48 -0600 Subject: [PATCH 1/2] Add configuration for basic message formats Fixes https://github.com/tulir/mautrix-telegram/issues/92 --- example-config.yaml | 9 +++++++++ mautrix_telegram/config.py | 3 +++ mautrix_telegram/portal.py | 25 ++++++++++++++++++++----- 3 files changed, 32 insertions(+), 5 deletions(-) diff --git a/example-config.yaml b/example-config.yaml index 94c2982f..cefbbb0f 100644 --- a/example-config.yaml +++ b/example-config.yaml @@ -115,6 +115,15 @@ bridge: # WARNING: Probably buggy, might get stuck in infinite loop. catch_up: false + # The formats to use when sending messages to Telegram via the relay bot. + # Available variables: + # $sender_display_name - The display name of the sender + # $message - The message text itself + message_formats: + m_text: + plain: "<$sender_display_name> $message" + html: "<$sender_display_name> $message" + filter: # Filter mode to use. Either "blacklist" or "whitelist". # If the mode is "blacklist", the listed chats will never be bridged. An empty blacklist disables the filter. diff --git a/mautrix_telegram/config.py b/mautrix_telegram/config.py index d4b391af..f599a7c8 100644 --- a/mautrix_telegram/config.py +++ b/mautrix_telegram/config.py @@ -181,6 +181,9 @@ class Config(DictWithRecursion): copy("bridge.native_stickers") copy("bridge.catch_up") + copy("bridge.message_formats.m_text.plain") + copy("bridge.message_formats.m_text.html") + copy("bridge.filter.mode") copy("bridge.filter.list") diff --git a/mautrix_telegram/portal.py b/mautrix_telegram/portal.py index af63e08e..3dcdec66 100644 --- a/mautrix_telegram/portal.py +++ b/mautrix_telegram/portal.py @@ -16,6 +16,8 @@ # along with this program. If not, see . from collections import deque from datetime import datetime +from string import Template +from html import escape as escape_html import asyncio import random import mimetypes @@ -656,16 +658,28 @@ class Portal: msgtype = message["msgtype"] if msgtype == "m.emote": if "formatted_body" in message: - message["formatted_body"] = f"* {sender.displayname} {message['formatted_body']}" - message["body"] = f"* {sender.displayname} {message['body']}" + tpl = config["bridge.message_formats.m_emote.html"] + tpl_args = dict(sender_display_name=sender.displayname, + message=message['formatted_body']) + message["formatted_body"] = Template(tpl).safe_substitute(tpl_args) + tpl = config["bridge.message_formats.m_emote.plain"] + tpl_args = dict(sender_display_name=sender.displayname, message=message['body']) + message["body"] = Template(tpl).safe_substitute(tpl_args) message["msgtype"] = "m.text" elif not sender.logged_in: html = message["formatted_body"] if "formatted_body" in message else None text = message["body"] if msgtype == "m.text": - if html: - html = f"<{sender.displayname}> {html}" - text = f"<{sender.displayname}> {text}" + # We use the plain text as HTML if available to ensure that we represent + # the event consistently. + if not html: + html = escape_html(text) + tpl = config["bridge.message_formats.m_text.html"] + tpl_args = dict(sender_display_name=sender.displayname,message=html) + html = Template(tpl).safe_substitute(tpl_args) + tpl = config["bridge.message_formats.m_text.plain"] + tpl_args = dict(sender_display_name=sender.displayname, message=text) + text = Template(tpl).safe_substitute(tpl_args) else: msgtype = msgtype[len("m."):] prefix = { @@ -681,6 +695,7 @@ class Portal: text = f"{sender.displayname} sent {prefix}{msgtype}{text}" if html: message["formatted_body"] = html + message["format"] = "org.matrix.custom.html" message["body"] = text async def _matrix_event_to_entities(self, client, event): From dad99823fc252ba37e05adcb9e4ce50115af8956 Mon Sep 17 00:00:00 2001 From: Travis Ralston Date: Thu, 7 Jun 2018 14:58:46 -0600 Subject: [PATCH 2/2] Add the m.emote message formats to the config --- example-config.yaml | 3 +++ mautrix_telegram/config.py | 2 ++ 2 files changed, 5 insertions(+) diff --git a/example-config.yaml b/example-config.yaml index cefbbb0f..7785d488 100644 --- a/example-config.yaml +++ b/example-config.yaml @@ -123,6 +123,9 @@ bridge: m_text: plain: "<$sender_display_name> $message" html: "<$sender_display_name> $message" + m_emote: + plain: "* $sender_display_name $message" + html: "* $sender_display_name $message" filter: # Filter mode to use. Either "blacklist" or "whitelist". diff --git a/mautrix_telegram/config.py b/mautrix_telegram/config.py index f599a7c8..2d9aaccc 100644 --- a/mautrix_telegram/config.py +++ b/mautrix_telegram/config.py @@ -183,6 +183,8 @@ class Config(DictWithRecursion): copy("bridge.message_formats.m_text.plain") copy("bridge.message_formats.m_text.html") + copy("bridge.message_formats.m_emote.plain") + copy("bridge.message_formats.m_emote.html") copy("bridge.filter.mode") copy("bridge.filter.list")