diff --git a/example-config.yaml b/example-config.yaml index c97bd70c..5c312147 100644 --- a/example-config.yaml +++ b/example-config.yaml @@ -292,6 +292,13 @@ bridge: # When private_chat_invite is empty, this message is sent to users /starting the # relaybot. Telegram's "markdown" is supported. message: This is a Matrix bridge relaybot and does not support direct chats + # List of users to invite to all group chat portals created by the bridge. + group_chat_invite: [] + # Whether or not the relaybot should not bridge events in unbridged group chats. + # If false, portals will be created when the relaybot receives messages, just like normal + # users. This behavior is usually not desirable, as it interferes with manually bridging + # the chat to another room. + ignore_unbridged_group_chat: true # Whether or not to allow creating portals from Telegram. authless_portals: true # Whether or not to allow Telegram group admins to use the bot commands. diff --git a/mautrix_telegram/abstract_user.py b/mautrix_telegram/abstract_user.py index 72fb69d1..0ad0dafd 100644 --- a/mautrix_telegram/abstract_user.py +++ b/mautrix_telegram/abstract_user.py @@ -351,6 +351,8 @@ class AbstractUser(ABC): Optional[po.Portal]]: if isinstance(update, UpdateShortChatMessage): portal = po.Portal.get_by_tgid(TelegramID(update.chat_id)) + if not portal: + self.log.warning(f"Received message in chat with unknown type {update.chat_id}") sender = pu.Puppet.get(TelegramID(update.from_id)) elif isinstance(update, UpdateShortMessage): portal = po.Portal.get_by_tgid(TelegramID(update.user_id), self.tgid, "user") @@ -404,7 +406,9 @@ class AbstractUser(ABC): async def update_message(self, original_update: UpdateMessage) -> None: update, sender, portal = self.get_message_details(original_update) - if portal and not portal.allow_bridging: + if not portal: + return + elif portal and not portal.allow_bridging: self.log.debug(f"Ignoring message in portal {portal.tgid_log} (bridging disallowed)") return @@ -413,10 +417,9 @@ class AbstractUser(ABC): if not config["bridge.relaybot.private_chat.invite"]: self.log.debug(f"Ignoring private message to bot from {sender.id}") return - elif not portal or not portal.mxid: - tgid_log = portal.tgid_log if portal else original_update.chat_id - self.log.debug( - f"Ignoring message received by bot in unbridged chat {tgid_log}") + elif not portal.mxid and config["bridge.relaybot.ignore_unbridged_group_chat"]: + self.log.debug("Ignoring message received by bot" + f" in unbridged chat {portal.tgid_log}") return if self.ignore_incoming_bot_events and self.relaybot and sender.id == self.relaybot.tgid: diff --git a/mautrix_telegram/config.py b/mautrix_telegram/config.py index f13cb442..37c7be4a 100644 --- a/mautrix_telegram/config.py +++ b/mautrix_telegram/config.py @@ -154,6 +154,8 @@ class Config(BaseBridgeConfig): copy("bridge.relaybot.private_chat.invite") copy("bridge.relaybot.private_chat.state_changes") copy("bridge.relaybot.private_chat.message") + copy("bridge.relaybot.group_chat_invite") + copy("bridge.relaybot.ignore_unbridged_group_chat") copy("bridge.relaybot.authless_portals") copy("bridge.relaybot.whitelist_group_admins") copy("bridge.relaybot.whitelist") diff --git a/mautrix_telegram/portal/metadata.py b/mautrix_telegram/portal/metadata.py index 03efa681..31f2049d 100644 --- a/mautrix_telegram/portal/metadata.py +++ b/mautrix_telegram/portal/metadata.py @@ -293,11 +293,15 @@ class PortalMetadata(BasePortal, ABC): users = participants = None if not direct: users, participants = await self._get_users(user, entity) + extra_invites = config["bridge.relaybot.group_chat_invite"] + invites += extra_invites + for invite in extra_invites: + power_levels.users.setdefault(invite, 100) self._participants_to_power_levels(participants, power_levels) elif self.bot and self.tg_receiver == self.bot.tgid: invites = config["bridge.relaybot.private_chat.invite"] for invite in invites: - power_levels.users[invite] = 100 + power_levels.users.setdefault(invite, 100) self.title = puppet.displayname initial_state = [{ "type": EventType.ROOM_POWER_LEVELS.serialize(),