From 6dfb690b182d665cf1e56a05ea51fbbe3e0bef19 Mon Sep 17 00:00:00 2001 From: nowanti Date: Fri, 18 Jul 2025 16:24:45 +0800 Subject: [PATCH] =?UTF-8?q?feat(=E9=80=9A=E7=9F=A5=E6=B8=A0=E9=81=93):=20?= =?UTF-8?q?=E7=BB=9F=E4=B8=80=E6=B6=88=E6=81=AF=E5=8F=91=E9=80=81=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3=E7=9A=84=E5=8F=82=E6=95=B0=E5=B9=B6=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E9=94=99=E8=AF=AF=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 为所有通知渠道统一send和send_async方法的参数顺序,增加title可选参数 在所有HTTP请求中添加响应状态检查以处理潜在错误 --- src/use_notify/channels/base.py | 4 +-- src/use_notify/channels/chanify.py | 6 ++-- src/use_notify/channels/console.py | 10 +++--- src/use_notify/channels/ding.py | 6 ++-- src/use_notify/channels/feishu.py | 6 ++-- src/use_notify/channels/pushdeer.py | 48 ++++++++++++++--------------- src/use_notify/channels/pushover.py | 6 ++-- src/use_notify/channels/wechat.py | 6 ++-- 8 files changed, 52 insertions(+), 40 deletions(-) diff --git a/src/use_notify/channels/base.py b/src/use_notify/channels/base.py index b30f4c8..d842475 100644 --- a/src/use_notify/channels/base.py +++ b/src/use_notify/channels/base.py @@ -8,9 +8,9 @@ def __init__(self, config: dict): self.config = AdDict(config) @abstractmethod - def send(self, message): + def send(self, content, title=None): raise NotImplementedError @abstractmethod - async def send_async(self, message): + async def send_async(self, content, title=None): raise NotImplementedError diff --git a/src/use_notify/channels/chanify.py b/src/use_notify/channels/chanify.py index ed9446c..9ef892c 100644 --- a/src/use_notify/channels/chanify.py +++ b/src/use_notify/channels/chanify.py @@ -32,11 +32,13 @@ def build_api_body(content, title=None): def send(self, content, title=None): api_body = self.build_api_body(content, title) with httpx.Client() as client: - client.post(self.api_url, data=api_body, headers=self.headers) + response = client.post(self.api_url, data=api_body, headers=self.headers) + response.raise_for_status() logger.debug("`chanify` send successfully") async def send_async(self, content, title=None): api_body = self.build_api_body(content, title) async with httpx.AsyncClient() as client: - await client.post(self.api_url, data=api_body, headers=self.headers) + response = await client.post(self.api_url, data=api_body, headers=self.headers) + response.raise_for_status() logger.debug("`chanify` send successfully") diff --git a/src/use_notify/channels/console.py b/src/use_notify/channels/console.py index 6f2b92b..b41b4e0 100644 --- a/src/use_notify/channels/console.py +++ b/src/use_notify/channels/console.py @@ -6,14 +6,16 @@ class Console(BaseChannel): def __init__(self, config=None): super().__init__(config or {}) - def send(self, title, content): + def send(self, content, title=None): """发送通知到控制台""" - print(f"\n📢 [默认实例通知] {title}") + title_display = title or "消息提醒" + print(f"\n📢 [默认实例通知] {title_display}") print(f"📝 {content}") print("-" * 50) - async def send_async(self, title, content): + async def send_async(self, content, title=None): """异步发送通知到控制台""" - print(f"\n📢 [默认实例异步通知] {title}") + title_display = title or "消息提醒" + print(f"\n📢 [默认实例异步通知] {title_display}") print(f"📝 {content}") print("-" * 50) diff --git a/src/use_notify/channels/ding.py b/src/use_notify/channels/ding.py index 9b1a078..0f16df1 100644 --- a/src/use_notify/channels/ding.py +++ b/src/use_notify/channels/ding.py @@ -39,11 +39,13 @@ def build_api_body(self, content, title=None): def send(self, content, title=None): api_body = self.build_api_body(content, title) with httpx.Client() as client: - client.post(self.api_url, json=api_body, headers=self.headers) + response = client.post(self.api_url, json=api_body, headers=self.headers) + response.raise_for_status() logger.debug("`钉钉` send successfully") async def send_async(self, content, title=None): api_body = self.build_api_body(content, title) async with httpx.AsyncClient() as client: - await client.post(self.api_url, json=api_body, headers=self.headers) + response = await client.post(self.api_url, json=api_body, headers=self.headers) + response.raise_for_status() logger.debug("`钉钉` send successfully") diff --git a/src/use_notify/channels/feishu.py b/src/use_notify/channels/feishu.py index f0a3c30..bffe1d4 100644 --- a/src/use_notify/channels/feishu.py +++ b/src/use_notify/channels/feishu.py @@ -50,11 +50,13 @@ def build_api_body(self, content, title=None): def send(self, content, title=None): api_body = self.build_api_body(content, title) with httpx.Client() as client: - client.post(self.api_url, json=api_body, headers=self.headers) + response = client.post(self.api_url, json=api_body, headers=self.headers) + response.raise_for_status() logger.debug("`飞书` send successfully") async def send_async(self, content, title=None): api_body = self.build_api_body(content, title) async with httpx.AsyncClient() as client: - await client.post(self.api_url, json=api_body, headers=self.headers) + response = await client.post(self.api_url, json=api_body, headers=self.headers) + response.raise_for_status() logger.debug("`飞书` send successfully") diff --git a/src/use_notify/channels/pushdeer.py b/src/use_notify/channels/pushdeer.py index f02ee1c..ae2d465 100644 --- a/src/use_notify/channels/pushdeer.py +++ b/src/use_notify/channels/pushdeer.py @@ -11,12 +11,12 @@ class PushDeer(BaseChannel): """pushdeer app 消息通知 - + 支持三种消息类型: - text: 纯文本消息 (默认) - image: 图片消息 - markdown: Markdown格式消息 - + 配置参数: - token: PushDeer的pushkey - base_url: 可选,自建PushDeer服务的URL,默认为"https://api2.pushdeer.com" @@ -29,42 +29,38 @@ def api_url(self): # 确保token存在 if not self.config.token: raise ValueError("PushDeer token (pushkey) is required") - + # Check if base_url exists in config, otherwise use default if self.config.base_url: base_url = self.config.base_url.rstrip("/") else: base_url = "https://api2.pushdeer.com" - + return f"{base_url}/message/push" - + def _prepare_params(self, content, title=None): """准备请求参数 - + Args: content: 消息内容 title: 消息标题 - + Returns: dict: 请求参数 """ # 默认标题 if not title: title = "消息提醒" - + # 确定消息类型 msg_type = getattr(self.config, "type", "markdown") if msg_type not in ["text", "markdown", "image"]: if msg_type: # 只有当type不为空且无效时才记录警告 logger.warning(f"Invalid message type: {msg_type}, fallback to text") msg_type = "markdown" - - params = { - "pushkey": self.config.token, - "text": title, - "type": msg_type - } - + + params = {"pushkey": self.config.token, "text": title, "type": msg_type} + # 根据消息类型处理内容 if msg_type == "text": # 对于text类型,content直接作为text参数 @@ -75,7 +71,7 @@ def _prepare_params(self, content, title=None): elif msg_type == "image": # 对于image类型,content应该是图片URL params["desp"] = content - + return params @property @@ -85,26 +81,30 @@ def headers(self): def send(self, content, title=None): """发送PushDeer消息 - + Args: content: 消息内容 title: 消息标题 """ params = self._prepare_params(content, title) - + with httpx.Client() as client: - client.get(self.api_url, params=params, headers=self.headers) - logger.debug(f"`pushdeer` send message successfully") + response = client.get(self.api_url, params=params, headers=self.headers) + response.raise_for_status() + logger.debug("`pushdeer` send message successfully") async def send_async(self, content, title=None): """异步发送PushDeer消息 - + Args: content: 消息内容 title: 消息标题 """ params = self._prepare_params(content, title) - + async with httpx.AsyncClient() as client: - await client.get(self.api_url, params=params, headers=self.headers) - logger.debug(f"`pushdeer` send message successfully") \ No newline at end of file + response = await client.get( + self.api_url, params=params, headers=self.headers + ) + response.raise_for_status() + logger.debug("`pushdeer` send message successfully") diff --git a/src/use_notify/channels/pushover.py b/src/use_notify/channels/pushover.py index 8910f5c..66afa94 100644 --- a/src/use_notify/channels/pushover.py +++ b/src/use_notify/channels/pushover.py @@ -30,11 +30,13 @@ def build_api_body(self, content, title=None): def send(self, content, title=None): api_body = self.build_api_body(content, title) with httpx.Client() as client: - client.post(self.api_url, data=api_body, headers=self.headers) + response = client.post(self.api_url, data=api_body, headers=self.headers) + response.raise_for_status() logger.debug("`pushover` send successfully") async def send_async(self, content, title=None): api_body = self.build_api_body(content, title) async with httpx.AsyncClient() as client: - await client.post(self.api_url, data=api_body, headers=self.headers) + response = await client.post(self.api_url, data=api_body, headers=self.headers) + response.raise_for_status() logger.debug("`pushover` send successfully") diff --git a/src/use_notify/channels/wechat.py b/src/use_notify/channels/wechat.py index e51f8c3..ebf0de6 100644 --- a/src/use_notify/channels/wechat.py +++ b/src/use_notify/channels/wechat.py @@ -35,11 +35,13 @@ def build_api_body(self, title, content): def send(self, content, title=None): api_body = self.build_api_body(title, content) with httpx.Client() as client: - client.post(self.api_url, json=api_body, headers=self.headers) + response = client.post(self.api_url, json=api_body, headers=self.headers) + response.raise_for_status() logger.debug("`WeChat` send successfully") async def send_async(self, content, title=None): api_body = self.build_api_body(title, content) async with httpx.AsyncClient() as client: - await client.post(self.api_url, json=api_body, headers=self.headers) + response = await client.post(self.api_url, json=api_body, headers=self.headers) + response.raise_for_status() logger.debug("`WeChat` send successfully")