ForcePilot/backend/package/yuxi/channel/extensions/tlon/hooks.py

40 lines
1.1 KiB
Python
Raw Normal View History

import re
import logging
from dataclasses import dataclass
logger = logging.getLogger(__name__)
@dataclass
class HookConfig:
trigger: str
pattern: str
action: str
action_params: dict | None = None
HOOKS: list[HookConfig] = []
async def check_and_execute_hooks(event: dict, client, account, ctx) -> bool:
content = event.get("content", "")
if not content:
return False
executed = False
for hook in HOOKS:
if not re.search(hook.pattern, content):
continue
try:
if hook.action == "send_group":
from yuxi.channel.extensions.tlon.send import send_group_message
params = dict(hook.action_params or {})
await send_group_message(client, **params)
elif hook.action == "react":
from yuxi.channel.extensions.tlon.send import add_reaction_group
params = dict(hook.action_params or {})
await add_reaction_group(client, **params)
executed = True
except Exception as e:
logger.warning("[tlon] Hook execution failed: %s", e)
return executed