ForcePilot/backend/package/yuxi/channel/extensions/workplace/mentions.py

33 lines
983 B
Python
Raw Normal View History

import re
import logging
logger = logging.getLogger(__name__)
MENTION_PATTERN = re.compile(r"@\[([^\]]+)\]\(mention:(\d+)\)")
class WorkplaceMentions:
@staticmethod
def extract_mentions(raw_message: dict) -> list[str]:
text = raw_message.get("message", {}).get("text", "")
if not text:
return []
matches = MENTION_PATTERN.findall(text)
mentioned_ids = [user_id for _, user_id in matches]
logger.debug("Extracted workplace mentions: %s", mentioned_ids)
return mentioned_ids
@staticmethod
def is_bot_mentioned(raw_message: dict, bot_page_id: str) -> bool:
text = raw_message.get("message", {}).get("text", "")
if not text:
return False
pattern = re.compile(rf"@\[([^\]]*)\]\(mention:({re.escape(bot_page_id)})\)")
return bool(pattern.search(text))
@staticmethod
def strip_mentions(text: str) -> str:
return MENTION_PATTERN.sub(r"@\1", text)