import logging from api_v1.utils.wecom_webhook import send_wecom_webhook_message from notifier.models import NotifierChannelEnum logger = logging.getLogger(__name__) class BaseNotifierBackend: channel = "" def notify(self, *, notifier, content: str, context: dict) -> dict: raise NotImplementedError class WeComWebhookNotifierBackend(BaseNotifierBackend): channel = NotifierChannelEnum.WECOM_WEBHOOK def notify(self, *, notifier, content: str, context: dict) -> dict: msgtype = str(notifier.get_config_value("msgtype", "markdown") or "markdown").strip().lower() timeout_seconds = float(notifier.get_config_value("timeout_seconds", 10.0) or 10.0) key = str(notifier.get_config_value("key", "") or "").strip() response = send_wecom_webhook_message( content=content, msgtype=msgtype, key=key or None, timeout_seconds=timeout_seconds, ) if not response.ok: raise RuntimeError(f"WeCom webhook 返回失败: errcode={response.errcode}, errmsg={response.errmsg}") result = { "channel": self.channel, "msgtype": msgtype, "errcode": response.errcode, "errmsg": response.errmsg, } logger.info("[notifier.backends] wecom webhook sent: notifier_id=%s result=%s", notifier.id, result) return result