feat 多通知渠道支持

This commit is contained in:
jxxghp
2023-07-14 13:05:58 +08:00
parent c1e8b6d0ff
commit 6d2f4697b0
22 changed files with 359 additions and 255 deletions

View File

@ -1,6 +1,10 @@
from abc import abstractmethod, ABCMeta
from typing import Tuple, Union
from app.db.systemconfig_oper import SystemConfigOper
from app.schemas import Notification
from app.schemas.types import SystemConfigKey, MessageChannel
class _ModuleBase(metaclass=ABCMeta):
"""
@ -30,3 +34,32 @@ class _ModuleBase(metaclass=ABCMeta):
:return: None该方法可被多个模块同时处理
"""
pass
def checkMessage(channel_type: MessageChannel):
"""
检查消息渠道及消息类型,如不符合则不处理
"""
def decorator(func):
def wrapper(self, message: Notification, *args, **kwargs):
# 检查消息渠道
if message.channel and message.channel != channel_type:
return None
else:
# 检查消息类型开关
if message.mtype:
switchs = SystemConfigOper().get(SystemConfigKey.NotificationChannels)
for switch in switchs:
if switch.get("mtype") == message.mtype.value:
if channel_type == MessageChannel.Wechat and not switch.get("wechat"):
return None
if channel_type == MessageChannel.Telegram and not switch.get("telegram"):
return None
if channel_type == MessageChannel.Slack and not switch.get("slack"):
return None
return func(self, message, *args, **kwargs)
return wrapper
return decorator

View File

@ -5,9 +5,9 @@ from typing import Optional, Union, List, Tuple, Any
from app.core.context import MediaInfo, Context
from app.core.config import settings
from app.log import logger
from app.modules import _ModuleBase
from app.modules import _ModuleBase, checkMessage
from app.modules.slack.slack import Slack
from app.schemas import MessageChannel, CommingMessage
from app.schemas import MessageChannel, CommingMessage, Notification
class SlackModule(_ModuleBase):
@ -181,40 +181,33 @@ class SlackModule(_ModuleBase):
userid=userid, username=username, text=text)
return None
def post_message(self, title: str,
text: str = None, image: str = None,
userid: Union[str, int] = None) -> Optional[bool]:
@checkMessage(MessageChannel.Slack)
def post_message(self, message: Notification) -> Optional[bool]:
"""
发送消息
:param title: 标题
:param text: 内容
:param image: 图片
:param userid: 用户ID
:param message: 消息
:return: 成功或失败
"""
return self.slack.send_msg(title=title, text=text, image=image, userid=userid)
return self.slack.send_msg(title=message.title, text=message.text,
image=message.image, userid=message.userid)
def post_medias_message(self, title: str, items: List[MediaInfo],
userid: Union[str, int] = None) -> Optional[bool]:
@checkMessage(MessageChannel.Slack)
def post_medias_message(self, message: Notification, medias: List[MediaInfo]) -> Optional[bool]:
"""
发送媒体信息选择列表
:param title: 标题
:param items: 消息列表
:param userid: 用户ID
:param message: 消息体
:param medias: 媒体信息
:return: 成功或失败
"""
return self.slack.send_meidas_msg(title=title, medias=items, userid=userid)
return self.slack.send_meidas_msg(title=message.title, medias=medias, userid=message.userid)
def post_torrents_message(self, title: str, items: List[Context],
mediainfo: MediaInfo = None,
userid: Union[str, int] = None) -> Optional[bool]:
@checkMessage(MessageChannel.Slack)
def post_torrents_message(self, message: Notification, torrents: List[Context]) -> Optional[bool]:
"""
发送种子信息选择列表
:param title: 标题
:param items: 消息列表
:param mediainfo: 媒体信息
:param userid: 用户ID
:param message: 消息体
:param torrents: 种子信息
:return: 成功或失败
"""
return self.slack.send_torrents_msg(title=title, torrents=items,
userid=userid)
return self.slack.send_torrents_msg(title=message.title, torrents=torrents,
userid=message.userid)

View File

@ -4,9 +4,9 @@ from typing import Optional, Union, List, Tuple, Any
from app.core.context import MediaInfo, Context
from app.core.config import settings
from app.log import logger
from app.modules import _ModuleBase
from app.modules import _ModuleBase, checkMessage
from app.modules.telegram.telegram import Telegram
from app.schemas import MessageChannel, CommingMessage
from app.schemas import MessageChannel, CommingMessage, Notification
class TelegramModule(_ModuleBase):
@ -91,42 +91,36 @@ class TelegramModule(_ModuleBase):
userid=user_id, username=user_id, text=text)
return None
def post_message(self, title: str,
text: str = None, image: str = None, userid: Union[str, int] = None) -> Optional[bool]:
@checkMessage(MessageChannel.Telegram)
def post_message(self, message: Notification) -> Optional[bool]:
"""
发送消息
:param title: 标题
:param text: 内容
:param image: 图片
:param userid: 用户ID
:param message: 消息体
:return: 成功或失败
"""
return self.telegram.send_msg(title=title, text=text, image=image, userid=userid)
return self.telegram.send_msg(title=message.title, text=message.text,
image=message.image, userid=message.userid)
def post_medias_message(self, title: str, items: List[MediaInfo],
userid: Union[str, int] = None) -> Optional[bool]:
@checkMessage(MessageChannel.Telegram)
def post_medias_message(self, message: Notification, medias: List[MediaInfo]) -> Optional[bool]:
"""
发送媒体信息选择列表
:param title: 标题
:param items: 消息列表
:param userid: 用户ID
:param message: 消息体
:param medias: 媒体列表
:return: 成功或失败
"""
return self.telegram.send_meidas_msg(title=title, medias=items, userid=userid)
return self.telegram.send_meidas_msg(title=message.title, medias=medias,
userid=message.userid)
def post_torrents_message(self, title: str, items: List[Context],
mediainfo: MediaInfo = None,
userid: Union[str, int] = None) -> Optional[bool]:
@checkMessage(MessageChannel.Telegram)
def post_torrents_message(self, message: Notification, torrents: List[Context]) -> Optional[bool]:
"""
发送种子信息选择列表
:param title: 标题
:param items: 消息列表
:param mediainfo: 媒体信息
:param userid: 用户ID
:param message: 消息体
:param torrents: 种子列表
:return: 成功或失败
"""
return self.telegram.send_torrents_msg(title=title, torrents=items,
mediainfo=mediainfo, userid=userid)
return self.telegram.send_torrents_msg(title=message.title, torrents=torrents, userid=message.userid)
def register_commands(self, commands: dict):
"""

View File

@ -133,7 +133,6 @@ class Telegram(metaclass=Singleton):
return False
def send_torrents_msg(self, torrents: List[Context],
mediainfo: MediaInfo = None,
userid: str = "", title: str = "") -> Optional[bool]:
"""
发送列表消息
@ -141,8 +140,12 @@ class Telegram(metaclass=Singleton):
if not self._telegram_token or not self._telegram_chat_id:
return None
if not torrents:
return False
try:
index, caption = 1, "*%s*" % title
mediainfo = torrents[0].media_info
for context in torrents:
torrent = context.torrent_info
site_name = torrent.site_name

View File

@ -1,13 +1,13 @@
import xml.dom.minidom
from typing import Optional, Union, List, Tuple, Any
from app.core.context import MediaInfo, Context
from app.core.config import settings
from app.core.context import Context, MediaInfo
from app.log import logger
from app.modules import _ModuleBase
from app.modules import _ModuleBase, checkMessage
from app.modules.wechat.WXBizMsgCrypt3 import WXBizMsgCrypt
from app.modules.wechat.wechat import WeChat
from app.schemas import MessageChannel, CommingMessage
from app.schemas import MessageChannel, CommingMessage, Notification
from app.utils.dom import DomUtils
@ -114,41 +114,35 @@ class WechatModule(_ModuleBase):
logger.error(f"微信消息处理发生错误:{err}")
return None
def post_message(self, title: str,
text: str = None, image: str = None, userid: Union[str, int] = None) -> Optional[bool]:
@checkMessage(MessageChannel.Wechat)
def post_message(self, message: Notification) -> Optional[bool]:
"""
发送消息
:param title: 标题
:param text: 内容
:param image: 图片
:param userid: 用户ID
:param message: 消息内容
:return: 成功或失败
"""
return self.wechat.send_msg(title=title, text=text, image=image, userid=userid)
return self.wechat.send_msg(title=message.title, text=message.text,
image=message.image, userid=message.userid)
def post_medias_message(self, title: str, items: List[MediaInfo],
userid: Union[str, int] = None) -> Optional[bool]:
@checkMessage(MessageChannel.Wechat)
def post_medias_message(self, message: Notification, medias: List[MediaInfo]) -> Optional[bool]:
"""
发送媒体信息选择列表
:param title: 标题
:param items: 消息列表
:param userid: 用户ID
:param message: 消息内容
:param medias: 媒体列表
:return: 成功或失败
"""
# 先发送标题
self.wechat.send_msg(title=title)
self.wechat.send_msg(title=message.title)
# 再发送内容
return self.wechat.send_medias_msg(medias=items, userid=userid)
return self.wechat.send_medias_msg(medias=medias, userid=message.userid)
def post_torrents_message(self, title: str, items: List[Context],
mediainfo: MediaInfo,
userid: Union[str, int] = None) -> Optional[bool]:
@checkMessage(MessageChannel.Wechat)
def post_torrents_message(self, message: Notification, torrents: List[Context]) -> Optional[bool]:
"""
发送种子信息选择列表
:param title: 标题
:param items: 消息列表
:param mediainfo: 媒体信息
:param userid: 用户ID
:param message: 消息内容
:param torrents: 种子列表
:return: 成功或失败
"""
return self.wechat.send_torrents_msg(title=title, torrents=items, mediainfo=mediainfo, userid=userid)
return self.wechat.send_torrents_msg(title=message.title, torrents=torrents, userid=message.userid)

View File

@ -191,7 +191,7 @@ class WeChat(metaclass=Singleton):
}
return self.__post_request(message_url, req_json)
def send_torrents_msg(self, torrents: List[Context], mediainfo: MediaInfo,
def send_torrents_msg(self, torrents: List[Context],
userid: str = "", title: str = "") -> Optional[bool]:
"""
发送列表消息
@ -213,6 +213,7 @@ class WeChat(metaclass=Singleton):
for context in torrents:
torrent = context.torrent_info
meta = MetaInfo(title=torrent.title, subtitle=torrent.description)
mediainfo = context.media_info
torrent_title = f"{index}.【{torrent.site_name}" \
f"{meta.season_episode} " \
f"{meta.resource_term} " \