diff --git a/backend/src/module/notification/notification.py b/backend/src/module/notification/notification.py index 909fc92ca..803fee925 100644 --- a/backend/src/module/notification/notification.py +++ b/backend/src/module/notification/notification.py @@ -9,6 +9,7 @@ ServerChanNotification, TelegramNotification, WecomNotification, + WecomRobotNotification, ) logger = logging.getLogger(__name__) @@ -23,6 +24,8 @@ def getClient(type: str): return BarkNotification elif type.lower() == "wecom": return WecomNotification + elif type.lower() == "wecom-robot": + return WecomRobotNotification else: return None @@ -43,8 +46,9 @@ def _get_poster(notify: Notification): def send_msg(self, notify: Notification) -> bool: self._get_poster(notify) try: - self.notifier.post_msg(notify) + resp = self.notifier.post_msg(notify) logger.debug(f"Send notification: {notify.official_title}") + return resp except Exception as e: logger.warning(f"Failed to send notification: {e}") return False diff --git a/backend/src/module/notification/plugin/__init__.py b/backend/src/module/notification/plugin/__init__.py index e9acda8f1..af63d22bf 100644 --- a/backend/src/module/notification/plugin/__init__.py +++ b/backend/src/module/notification/plugin/__init__.py @@ -2,3 +2,4 @@ from .server_chan import ServerChanNotification from .telegram import TelegramNotification from .wecom import WecomNotification +from .wecom_robot import WecomRobotNotification diff --git a/backend/src/module/notification/plugin/wecom_robot.py b/backend/src/module/notification/plugin/wecom_robot.py new file mode 100644 index 000000000..b071c56a2 --- /dev/null +++ b/backend/src/module/notification/plugin/wecom_robot.py @@ -0,0 +1,51 @@ +import logging + +import requests + +from module.models import Notification +from module.network import RequestContent + +logger = logging.getLogger(__name__) + + +class WecomRobotNotification(RequestContent): + """企业微信群机器人""" + + def __init__(self, token, chat_id, **kwargs): + super().__init__() + # token is wecom group robot webhook key + self.notification_url = ( + f"https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key={token}" + ) + + @staticmethod + def gen_message(notify: Notification) -> str: + text = f""" + 番剧名称:{notify.official_title}\n季度: 第{notify.season}季\n更新集数: 第{notify.episode}集 + """ + return text.strip() + + def post_msg(self, notify: Notification) -> bool: + title = "【番剧更新】" + msg = self.gen_message(notify) + picurl = notify.poster_path + if picurl.startswith("http"): + data = { + "msgtype": "news", + "news": { + "articles": [ + { + "title": title, + "description": msg, + "url": "https://mikanime.tv", + "picurl": picurl, + } + ] + }, + } + else: + msg = f"{title}\n{msg}" + data = {"msgtype": "text", "text": {"content": msg}} + resp = requests.post(url=self.notification_url, json=data, timeout=3) + logger.debug(f"Wecom-robot notification: {resp.status_code}") + return resp.status_code == 200 diff --git a/backend/src/test/test_notification.py b/backend/src/test/test_notification.py new file mode 100644 index 000000000..54f457338 --- /dev/null +++ b/backend/src/test/test_notification.py @@ -0,0 +1,26 @@ +from module.models import Notification +from module.notification import PostNotification + + +class TestPostNotification(PostNotification): + @staticmethod + def _get_poster(notify: Notification): + notify.poster_path = notify.poster_path + + +def test_notification(): + info1 = Notification( + official_title="番剧名", + season=1, + episode=1, + poster_path="https://mikanime.tv/images/Bangumi/202404/0fd46fc8.jpg", + ) + info2 = Notification( + official_title="番剧名", + season=1, + episode=1, + poster_path="posters/0fd46fc8.jpg", + ) + with TestPostNotification() as notifier: + assert notifier.send_msg(info1) + assert notifier.send_msg(info2) diff --git a/backend/src/test/test_rss_engine.py b/backend/src/test/test_rss_engine.py index cda69f6ed..315956c14 100644 --- a/backend/src/test/test_rss_engine.py +++ b/backend/src/test/test_rss_engine.py @@ -14,5 +14,7 @@ def test_rss_engine(): new_torrents = engine.pull_rss(result[1]) torrent = new_torrents[0] - assert torrent.name == "[Lilith-Raws] 无职转生,到了异世界就拿出真本事 / Mushoku Tensei - 11 [Baha][WEB-DL][1080p][AVC AAC][CHT][MP4]" - + assert ( + torrent.name + == "[Lilith-Raws] 无职转生,到了异世界就拿出真本事 / Mushoku Tensei - 11 [Baha][WEB-DL][1080p][AVC AAC][CHT][MP4]" + ) diff --git a/webui/src/components/setting/config-notification.vue b/webui/src/components/setting/config-notification.vue index c2bf5088a..607e6648f 100644 --- a/webui/src/components/setting/config-notification.vue +++ b/webui/src/components/setting/config-notification.vue @@ -11,6 +11,7 @@ const notificationType: NotificationType = [ 'server-chan', 'bark', 'wecom', + 'wecom-robot' ]; const items: SettingItem[] = [