From ea6d7cdaa4b636d0f918219a8da446ef023eedb2 Mon Sep 17 00:00:00 2001 From: William Kray Date: Wed, 14 Aug 2024 15:30:49 -0700 Subject: [PATCH] include welcome module with multi-room message-mapping support --- base-config.yaml | 28 ++++++++++++++++++++++++++++ community/bot.py | 31 ++++++++++++++++++++++++++++--- maubot.yaml | 2 +- 3 files changed, 57 insertions(+), 4 deletions(-) diff --git a/base-config.yaml b/base-config.yaml index 1c60b92..d57d0e0 100644 --- a/base-config.yaml +++ b/base-config.yaml @@ -29,3 +29,31 @@ admins: invitees: - "@mybot:server.tld" - "@secondaryadmin:server.tld" + +# auto-greet users in rooms with these messages +# map greeting messages to a room +# you can use {user} to reference the joining user in this message using a +# matrix.to link (rendered as a "pill" in element clients) +# html formatting is supported +greetings: + generic: | + Welcome {user}! Please be sure to read the topic for helpful links and information. + Use Google for all other queries ;) + encrypted: | + welcome {user}, this is an encrypted room, so you may not be able to see messages previously sent here. don't be + alarmed. + +# which of the above greetings should be used in which rooms? use the exact name of each greeting +# you've assigned, e.g. 'generic' or 'encrypted' +greeting_rooms: + '!someroomid:server.tld': generic + '!someotherroom:server.tld': generic + '!myencryptedroomid:server.tld': encrypted + +# add a room ID here to send a message to when someone joins the above rooms +# (optional) +notification_room: + +# message to send to the notification room when someone joins one of the above rooms: +join_notification_message: | + User {user} has joined {room}. diff --git a/community/bot.py b/community/bot.py index 5dd04da..896710b 100644 --- a/community/bot.py +++ b/community/bot.py @@ -5,9 +5,10 @@ import json import time import re -from mautrix.client import Client, InternalEventType, MembershipEventDispatcher +from mautrix.client import Client, InternalEventType, MembershipEventDispatcher, SyncStream from mautrix.types import (Event, StateEvent, EventID, UserID, FileInfo, EventType, - MediaMessageEventContent, ReactionEvent, RedactionEvent) + MediaMessageEventContent, ReactionEvent, RedactionEvent, RoomID, + RoomAlias) from mautrix.errors import MNotFound from mautrix.util.config import BaseProxyConfig, ConfigUpdateHelper from maubot import Plugin, MessageEvent @@ -28,6 +29,10 @@ class Config(BaseProxyConfig): helper.copy("kick_threshold_days") helper.copy("encrypt") helper.copy("invitees") + helper.copy("notification_room") + helper.copy("join_notification_message") + helper.copy_dict("greeting_rooms") + helper.copy_dict("greetings") class CommunityBot(Plugin): @@ -102,9 +107,29 @@ class CommunityBot(Plugin): return report @event.on(InternalEventType.JOIN) - async def passive_sync(self, evt:StateEvent) -> None: + async def newjoin(self, evt:StateEvent) -> None: + # passive sync of tracking db if evt.room_id == self.config['parent_room']: await self.do_sync() + self.log.debug(self.config["greeting_rooms"]) + # greeting activities + room_id = str(evt.room_id) + if room_id in self.config["greeting_rooms"]: + greeting_map = self.config['greetings'] + greeting_name = self.config['greeting_rooms'][room_id] + if evt.source & SyncStream.STATE: + return + else: + nick = self.client.parse_user_id(evt.sender)[0] + pill = '{nick}'.format(mxid=evt.sender, nick=nick) + greeting = greeting_map[greeting_name].format(user=pill) + await self.client.send_notice(evt.room_id, html=greeting) + if self.config["notification_room"]: + roomnamestate = await self.client.get_state_event(evt.room_id, 'm.room.name') + roomname = roomnamestate['name'] + notification_message = self.config['join_notification_message'].format(user=evt.sender, + room=roomname) + await self.client.send_notice(self.config["notification_room"], html=notification_message) @event.on(EventType.ROOM_MESSAGE) async def update_message_timestamp(self, evt: MessageEvent) -> None: diff --git a/maubot.yaml b/maubot.yaml index 998124c..51dc571 100644 --- a/maubot.yaml +++ b/maubot.yaml @@ -1,6 +1,6 @@ maubot: 0.1.0 id: org.jobmachine.communitybot -version: 0.0.1 +version: 0.0.2 license: MIT modules: - community