From baef30dda6deca3b42d79306b9b5980f0deb2f91 Mon Sep 17 00:00:00 2001 From: Alex Date: Sat, 21 Nov 2026 18:43:11 -0400 Subject: [PATCH] fix(wechat): fall back to a non-empty title for news messages WeCom news articles require a title; __send_image_message forwarded title=None, so image notifications without a title were rejected. Use the first non-empty text line, or 'MoviePilot' as last resort. --- app/modules/wechat/wechat.py | 3 ++ tests/test_wechat_notification_image.py | 37 +++++++++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/app/modules/wechat/wechat.py b/app/modules/wechat/wechat.py index 1f7c1d4e8..78897ab4b 100644 --- a/app/modules/wechat/wechat.py +++ b/app/modules/wechat/wechat.py @@ -265,6 +265,9 @@ class WeChat: """ if text: text = text.replace("\n\n", "\n") + if not title: + # 企业微信图文消息要求标题非空,缺省时取正文首行,仍为空则用应用名 + title = next((line.strip() for line in (text or "").splitlines() if line.strip()), "MoviePilot") if not userid: userid = "@all" req_json = { diff --git a/tests/test_wechat_notification_image.py b/tests/test_wechat_notification_image.py index 68e047653..acf948632 100644 --- a/tests/test_wechat_notification_image.py +++ b/tests/test_wechat_notification_image.py @@ -140,3 +140,40 @@ def test_wechat_news_payload_uses_signed_douban_proxy( image_url = captured["news"]["articles"][0]["picurl"] signed_source = parse_qs(urlparse(image_url).query)["url"][0] assert image_urls.verify_wechat_image_url(signed_source) == source_url + + +@pytest.mark.parametrize( + ("title", "text", "expected"), + [ + (None, "已添加订阅\n详情", "已添加订阅"), + ("", "\n第二行", "第二行"), + (None, None, "MoviePilot"), + ("奥德赛", "已添加订阅", "奥德赛"), + ], +) +def test_wechat_news_payload_title_fallback( + monkeypatch: pytest.MonkeyPatch, + title: Any, + text: Any, + expected: str, +) -> None: + """ + 图文消息没有标题时不应发送空标题,企业微信会拒收该消息。 + """ + monkeypatch.setattr(image_urls, "get_runtime_setting", _settings_with_domain) + captured: dict[str, Any] = {} + client = WeChat.__new__(WeChat) + client._appid = "1" + monkeypatch.setattr( + client, + "_WeChat__post_request", + lambda url, payload: captured.update(payload) or True, + ) + + assert client._WeChat__send_image_message( + title=title, + text=text, + image_url="https://image.tmdb.org/poster.jpg", + ) is True + + assert captured["news"]["articles"][0]["title"] == expected