From 4d998e51cda2867919ead6863a417160db93f8f1 Mon Sep 17 00:00:00 2001 From: mic1on Date: Sat, 11 Jul 2026 21:09:22 +0800 Subject: [PATCH] fix: avoid None title in channel payloads --- src/use_notify/channels/chanify.py | 3 ++- src/use_notify/channels/wechat.py | 3 ++- tests/test_channels.py | 15 +++++++++++++++ 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/use_notify/channels/chanify.py b/src/use_notify/channels/chanify.py index 149490a..f9328e0 100644 --- a/src/use_notify/channels/chanify.py +++ b/src/use_notify/channels/chanify.py @@ -28,7 +28,8 @@ def headers(self): @staticmethod def build_api_body(content, title=None): - return {"text": f"{title}\n{content}"} + text = f"{title}\n{content}" if title else content + return {"text": text} def send(self, content, title=None): api_body = self.build_api_body(content, title) diff --git a/src/use_notify/channels/wechat.py b/src/use_notify/channels/wechat.py index 42a6386..4158632 100644 --- a/src/use_notify/channels/wechat.py +++ b/src/use_notify/channels/wechat.py @@ -21,7 +21,8 @@ def headers(self): return {"Content-Type": "application/json"} def build_api_body(self, title, content): - content = f"## {title}\n\n{content}" + if title: + content = f"## {title}\n\n{content}" api_body = {"markdown": {"content": content}, "msgtype": "markdown"} if self.config.mentioned_list: diff --git a/tests/test_channels.py b/tests/test_channels.py index f63ad2c..c89fade 100644 --- a/tests/test_channels.py +++ b/tests/test_channels.py @@ -110,6 +110,12 @@ def test_chanify_send_builds_expected_request(mock_client): response.raise_for_status.assert_called_once_with() +def test_chanify_omits_title_when_missing(): + channel = useNotifyChannel.Chanify({"token": "token"}) + + assert channel.build_api_body("hello") == {"text": "hello"} + + def test_ding_build_api_body_includes_mentions(): channel = useNotifyChannel.Ding( { @@ -240,6 +246,15 @@ def test_wechat_build_api_body_includes_mentions(): } +def test_wechat_omits_title_when_missing(): + channel = useNotifyChannel.WeChat({"token": "token"}) + + assert channel.build_api_body(None, "hello") == { + "markdown": {"content": "hello"}, + "msgtype": "markdown", + } + + @patch("httpx.Client") def test_wechat_send_rejects_business_error_response(mock_client): response = _mock_sync_http_response({"errcode": 40001, "errmsg": "invalid credential"})