From 4673546b42ca1b6ffcb554c5a2146fbb1b3e03e1 Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Sat, 17 Feb 2018 19:16:21 +0200 Subject: [PATCH] Add option to bridge notices and command to get relaybot info --- example-config.yaml | 2 ++ mautrix_telegram/commands/auth.py | 15 +++++++++++++++ mautrix_telegram/commands/handler.py | 4 +++- mautrix_telegram/commands/meta.py | 1 + mautrix_telegram/portal.py | 4 +++- 5 files changed, 24 insertions(+), 2 deletions(-) diff --git a/example-config.yaml b/example-config.yaml index 73d20d34..221ff539 100644 --- a/example-config.yaml +++ b/example-config.yaml @@ -67,6 +67,8 @@ bridge: # Show message editing as a reply to the original message. # If this is false, message edits are not shown at all, as Matrix does not support editing yet. edits_as_replies: False + # Whether or not Matrix bot messages (type m.notice) should be bridged. + bridge_notices: False # The prefix for commands. Only required in non-management rooms. command_prefix: "!tg" diff --git a/mautrix_telegram/commands/auth.py b/mautrix_telegram/commands/auth.py index e96d47b0..de38fdbf 100644 --- a/mautrix_telegram/commands/auth.py +++ b/mautrix_telegram/commands/auth.py @@ -32,6 +32,21 @@ async def ping(evt): return await evt.reply("You're not logged in.") +@command_handler() +async def ping_bot(evt): + if not evt.tgbot: + return await evt.reply("Telegram message relay bot not configured.") + bot_info = await evt.tgbot.client.get_me() + localpart = evt.config.get("bridge.username_template", "telegram_{userid}").format( + userid=bot_info.id) + hs = evt.config["homeserver"]["domain"] + mxid = f"@{localpart}:{hs}" + displayname = bot_info.first_name + return await evt.reply(f"Telegram message relay bot is active: " + + f"[{displayname}](https://matrix.to/#/{mxid}) (ID {bot_info.id})\n\n" + + f"To use the bot, simply invite it to a portal room.") + + @command_handler(needs_auth=False, management_only=True) def register(evt): return evt.reply("Not yet implemented.") diff --git a/mautrix_telegram/commands/handler.py b/mautrix_telegram/commands/handler.py index e7cf956f..2ed4679d 100644 --- a/mautrix_telegram/commands/handler.py +++ b/mautrix_telegram/commands/handler.py @@ -45,6 +45,8 @@ class CommandEvent: self.az = handler.az self.log = handler.log self.loop = handler.loop + self.tgbot = handler.tgbot + self.config = handler.config self.command_prefix = handler.command_prefix self.room_id = room self.sender = sender @@ -87,7 +89,7 @@ class CommandHandler: log = logging.getLogger("mau.commands") def __init__(self, context): - self.az, self.db, self.config, self.loop, _ = context + self.az, self.db, self.config, self.loop, self.tgbot = context self.command_prefix = self.config["bridge.command_prefix"] # region Utility functions for handling commands diff --git a/mautrix_telegram/commands/meta.py b/mautrix_telegram/commands/meta.py index 6ffff94f..0338b402 100644 --- a/mautrix_telegram/commands/meta.py +++ b/mautrix_telegram/commands/meta.py @@ -65,6 +65,7 @@ def help(evt): `channel` (defaults to `group`). #### Portal management +**ping-bot** - Get info of the message relay Telegram bot. **upgrade** - Upgrade a normal Telegram group to a supergroup. **invite-link** - Get a Telegram invite link to the current chat. **delete-portal** - Forget the current portal room. Only works for group chats; to delete diff --git a/mautrix_telegram/portal.py b/mautrix_telegram/portal.py index 421f7400..dee0974e 100644 --- a/mautrix_telegram/portal.py +++ b/mautrix_telegram/portal.py @@ -45,6 +45,7 @@ class Portal: db = None az = None bot = None + bridge_notices = False by_mxid = {} by_tgid = {} @@ -490,7 +491,7 @@ class Portal: f"<{sender.displayname}> {message['formatted_body']}" message["body"] = f"<{sender.displayname}> {message['body']}" - if type == "m.text": + if type == "m.text" or (self.bridge_notices and type == "m.notice"): if "format" in message and message["format"] == "org.matrix.custom.html": message, entities = formatter.matrix_to_telegram(message["formatted_body"]) response = await client.send_message(self.peer, message, entities=entities, @@ -1092,3 +1093,4 @@ class Portal: def init(context): global config Portal.az, Portal.db, config, _, Portal.bot = context + Portal.bridge_notices = config["bridge.bridge_notices"]