import random, re import config from gacha_response import gacha_response from fediverse_factory import get_fediverse_service from fediverse_types import FediverseNotification, NotificationType, Visibility def parse_notification(notification: FediverseNotification, fediverse_service=None): '''Parses any notifications received by the bot and sends any commands to gacha_response()''' if fediverse_service is None: fediverse_service = get_fediverse_service() # We get the type of notification to filter the ones that we actually want # to parse if notification.type not in (NotificationType.MENTION, NotificationType.REPLY): return # Ignore anything that isn't a mention # Return early if no post attached if not notification.post: return # We want the visibility to be related to the type that was received (so if # people don't want to dump a bunch of notes on home they don't have to) if notification.post.visibility != Visibility.SPECIFIED: visibility = Visibility.HOME else: visibility = Visibility.SPECIFIED # Get the full Activitypub ID of the user full_user = notification.user.full_handle note_text = notification.post.text note_id = notification.post.id note = note_text.strip().lower() if note_text else "" # Check for both short and fully-qualified name mentions username_variants = [ config.USER, f'@{config.USER.split("@")[1]}' ] # Make sure the notification text explicitly mentions the bot if not any(variant in note for variant in username_variants): return # Find command and arguments after the mention # Removes all mentions (regex = mentions that start with @ and may contain @domain) cleaned_text = re.sub(r"@\w+(?:@\S+)?", "", note).strip() parts = cleaned_text.split() command = parts[0].lower() if parts else None arguments = parts[1:] if len(parts) > 1 else [] # TODO: move response generation to a different function response = gacha_response(command.lower(), full_user, arguments, notification.post) if not response: return if isinstance(response, str): fediverse_service.create_post( text=response, reply_to_id=note_id, visibility=visibility ) else: fediverse_service.create_post( text=response[0], reply_to_id=note_id, visibility=visibility, file_ids=response[1] #visible_user_ids=[] #todo: write actual visible users ids so pleromers can use the bot privately )