Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for custom welcome messages #58

Merged
merged 2 commits into from
Oct 23, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
45 changes: 40 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,32 @@ 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 = []

welcome_messages.append(self.management_room_text.get("welcome"))
justinbot marked this conversation as resolved.
Show resolved Hide resolved
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([markdown.render(m) for m in welcome_messages])
justinbot marked this conversation as resolved.
Show resolved Hide resolved
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