Skip to content

Commit

Permalink
Merge pull request #58 from justinbot/justinbot/management-room-text
Browse files Browse the repository at this point in the history
Add support for custom welcome messages
  • Loading branch information
tulir authored Oct 23, 2021
2 parents 689e5f5 + 402c037 commit 99e7a17
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 5 deletions.
6 changes: 6 additions & 0 deletions mautrix/bridge/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,12 @@ def do_update(self, helper: ConfigUpdateHelper) -> None:

copy("appservice.ephemeral_events")

copy("bridge.management_room_text.welcome")
copy("bridge.management_room_text.welcome_connected")
copy("bridge.management_room_text.welcome_unconnected")
copy("bridge.management_room_text.additional_help")
copy("bridge.management_room_multiple_messages")

copy("manhole.enabled")
copy("manhole.path")
copy("manhole.whitelist")
Expand Down
44 changes: 39 additions & 5 deletions mautrix/bridge/matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
MediaRepoConfig)
from mautrix.errors import IntentError, MatrixError, MForbidden, DecryptionError, SessionNotFound
from mautrix.appservice import AppService
from mautrix.util import markdown
from mautrix.util.logging import TraceLogger
from mautrix.util.opt_prometheus import Histogram

Expand Down Expand Up @@ -81,6 +82,19 @@ def __init__(self, command_processor: Optional[CommandProcessor] = None,
db_url=self._get_db_url(bridge.config),
key_sharing_config=self.config["bridge.encryption.key_sharing"])

self.management_room_text = self.config.get(
"bridge.management_room_text",
{
"welcome": "Hello, I'm a bridge bot.",
"welcome_connected": "Use `help` for help.",
"welcome_unconnected": "Use `help` for help on how to log in.",
},
)
self.management_room_multiple_messages = self.config.get(
"bridge.management_room_multiple_messages",
False,
)

@staticmethod
def _get_db_url(config: 'BaseBridgeConfig') -> str:
db_url = config["bridge.encryption.database"]
Expand Down Expand Up @@ -244,11 +258,31 @@ async def accept_bot_invite(self, room_id: RoomID, inviter: 'BaseUser') -> None:
await self.send_welcome_message(room_id, inviter)

async def send_welcome_message(self, room_id: RoomID, inviter: 'BaseUser') -> None:
await self.az.intent.send_notice(
room_id=room_id,
text=f"Hello, I'm a bridge bot. Use `{self.commands.command_prefix} help` for help.",
html=f"Hello, I'm a bridge bot. Use <code>{self.commands.command_prefix} help</code> "
f"for help.")
has_two_members, bridge_bot_in_room = await self._is_direct_chat(room_id)
is_management = has_two_members and bridge_bot_in_room

welcome_messages = [self.management_room_text.get("welcome")]

if is_management:
if await inviter.is_logged_in():
welcome_messages.append(self.management_room_text.get("welcome_connected"))
else:
welcome_messages.append(self.management_room_text.get("welcome_unconnected"))

additional_help = self.management_room_text.get("additional_help")
if additional_help:
welcome_messages.append(additional_help)
else:
cmd_prefix = self.commands.command_prefix
welcome_messages.append(f"Use `{cmd_prefix} help` for help.")

if self.management_room_multiple_messages:
for m in welcome_messages:
await self.az.intent.send_notice(room_id, text=m, html=markdown.render(m))
else:
combined = "\n".join(welcome_messages)
combined_html = "".join(map(markdown.render, welcome_messages))
await self.az.intent.send_notice(room_id, text=combined, html=combined_html)

async def int_handle_invite(self, room_id: RoomID, user_id: UserID, invited_by: UserID,
event_id: EventID) -> None:
Expand Down

0 comments on commit 99e7a17

Please sign in to comment.