From 545e1fff0b7f36049637b32072269360c6755bcc Mon Sep 17 00:00:00 2001 From: Ljxtt <1058648620@qq.com> Date: Tue, 23 Jul 2024 22:27:16 +0800 Subject: [PATCH] feat: Add ntfy notification method --- .../src/module/notification/notification.py | 3 ++ .../module/notification/plugin/__init__.py | 1 + .../src/module/notification/plugin/ntfy.py | 31 +++++++++++++++++++ .../setting/config-notification.vue | 1 + webui/types/config.ts | 2 +- 5 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 backend/src/module/notification/plugin/ntfy.py diff --git a/backend/src/module/notification/notification.py b/backend/src/module/notification/notification.py index 909fc92ca..eeb94d24a 100644 --- a/backend/src/module/notification/notification.py +++ b/backend/src/module/notification/notification.py @@ -9,6 +9,7 @@ ServerChanNotification, TelegramNotification, WecomNotification, + NtfyNotification, ) logger = logging.getLogger(__name__) @@ -23,6 +24,8 @@ def getClient(type: str): return BarkNotification elif type.lower() == "wecom": return WecomNotification + elif type.lower() == 'ntfy': + return NtfyNotification else: return None diff --git a/backend/src/module/notification/plugin/__init__.py b/backend/src/module/notification/plugin/__init__.py index e9acda8f1..12020c199 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 .ntfy import NtfyNotification diff --git a/backend/src/module/notification/plugin/ntfy.py b/backend/src/module/notification/plugin/ntfy.py new file mode 100644 index 000000000..20be0e790 --- /dev/null +++ b/backend/src/module/notification/plugin/ntfy.py @@ -0,0 +1,31 @@ +import logging + +from module.models import Notification +from module.network import RequestContent + +logger = logging.getLogger(__name__) + + +class NtfyNotification(RequestContent): + def __init__(self, token, chat_id, **kwargs): + super().__init__() + + self.notification_url = f"{chat_id}" + self.token = token + + @staticmethod + def gen_message(notify: Notification) -> str: + text = f""" + 番剧名称:{notify.official_title}\n季度: 第{notify.season}季\n更新集数: 第{notify.episode}集\n + """ + return text.strip() + + def post_msg(self, notify: Notification) -> bool: + text = self.gen_message(notify) + self.header["Authorization"] = "Bearer " + self.token + self.header["Title"] = notify.official_title.encode('utf-8') + # self.header["Icon"] = notify.poster_path + data = text.encode('utf-8') + resp = self.post_data(self.notification_url, data) + logger.debug(f"ServerChan notification: {resp.status_code}") + return resp.status_code == 200 \ No newline at end of file diff --git a/webui/src/components/setting/config-notification.vue b/webui/src/components/setting/config-notification.vue index 506d40480..74a9230b3 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', + 'ntfy', ]; const items: SettingItem[] = [ diff --git a/webui/types/config.ts b/webui/types/config.ts index a53b1189a..b3ecf0bba 100644 --- a/webui/types/config.ts +++ b/webui/types/config.ts @@ -43,7 +43,7 @@ export interface Config { }; notification: { enable: boolean; - type: 'telegram' | 'server-chan' | 'bark' | 'wecom'; + type: 'telegram' | 'server-chan' | 'bark' | 'wecom' | 'ntfy'; token: string; chat_id: string; };