mirror of
https://github.com/jxxghp/MoviePilot.git
synced 2026-07-12 16:02:35 +08:00
fix messages
This commit is contained in:
@@ -3,24 +3,50 @@ from typing import Optional, Union, List, Tuple, Any, Dict
|
||||
|
||||
from app.core.config import settings
|
||||
from app.core.context import Context, MediaInfo
|
||||
from app.helper.notification import NotificationHelper
|
||||
from app.log import logger
|
||||
from app.modules import _ModuleBase, checkMessage
|
||||
from app.modules import _ModuleBase
|
||||
from app.modules.wechat.WXBizMsgCrypt3 import WXBizMsgCrypt
|
||||
from app.modules.wechat.wechat import WeChat
|
||||
from app.schemas import MessageChannel, CommingMessage, Notification
|
||||
from app.schemas import MessageChannel, CommingMessage, Notification, NotificationConf
|
||||
from app.utils.dom import DomUtils
|
||||
|
||||
|
||||
class WechatModule(_ModuleBase):
|
||||
wechat: WeChat = None
|
||||
_channel = MessageChannel.Wechat
|
||||
_configs: Dict[str, NotificationConf] = {}
|
||||
_clients: Dict[str, WeChat] = {}
|
||||
|
||||
def init_module(self) -> None:
|
||||
self.wechat = WeChat()
|
||||
"""
|
||||
初始化模块
|
||||
"""
|
||||
clients = NotificationHelper().get_notifications()
|
||||
if not clients:
|
||||
return
|
||||
self._configs = {}
|
||||
self._clients = {}
|
||||
for client in clients:
|
||||
if client.type == "wechat" and client.enabled:
|
||||
self._configs[client.name] = client
|
||||
self._clients[client.name] = WeChat(**client.config)
|
||||
|
||||
@staticmethod
|
||||
def get_name() -> str:
|
||||
return "微信"
|
||||
|
||||
def get_client(self, name: str) -> Optional[WeChat]:
|
||||
"""
|
||||
获取Telegram客户端
|
||||
"""
|
||||
return self._clients.get(name)
|
||||
|
||||
def get_config(self, name: str) -> Optional[NotificationConf]:
|
||||
"""
|
||||
获取Telegram配置
|
||||
"""
|
||||
return self._configs.get(name)
|
||||
|
||||
def stop(self):
|
||||
pass
|
||||
|
||||
@@ -28,13 +54,14 @@ class WechatModule(_ModuleBase):
|
||||
"""
|
||||
测试模块连接性
|
||||
"""
|
||||
state = self.wechat.get_state()
|
||||
if state:
|
||||
return True, ""
|
||||
return False, "获取微信token失败"
|
||||
for name, client in self._clients.items():
|
||||
state = client.get_state()
|
||||
if not state:
|
||||
return False, f"企业微信 {name} 未就续"
|
||||
return True, ""
|
||||
|
||||
def init_setting(self) -> Tuple[str, Union[str, bool]]:
|
||||
return "MESSAGER", "wechat"
|
||||
pass
|
||||
|
||||
def message_parser(self, body: Any, form: Any,
|
||||
args: Any) -> Optional[CommingMessage]:
|
||||
@@ -49,6 +76,14 @@ class WechatModule(_ModuleBase):
|
||||
:return: 渠道、消息体
|
||||
"""
|
||||
try:
|
||||
# 消息来源
|
||||
source = args.get("source")
|
||||
if not source:
|
||||
return None
|
||||
# 获取客户端
|
||||
client = self.get_client(source)
|
||||
if not client:
|
||||
return None
|
||||
# URL参数
|
||||
sVerifyMsgSig = args.get("msg_signature")
|
||||
sVerifyTimeStamp = args.get("timestamp")
|
||||
@@ -113,7 +148,7 @@ class WechatModule(_ModuleBase):
|
||||
wechat_admins = settings.WECHAT_ADMINS.split(',')
|
||||
if wechat_admins and not any(
|
||||
user_id == admin_user for admin_user in wechat_admins):
|
||||
self.wechat.send_msg(title="用户无权限执行菜单命令", userid=user_id)
|
||||
client.send_msg(title="用户无权限执行菜单命令", userid=user_id)
|
||||
return None
|
||||
# 根据EventKey执行命令
|
||||
content = DomUtils.tag_value(root_node, "EventKey")
|
||||
@@ -127,49 +162,81 @@ class WechatModule(_ModuleBase):
|
||||
|
||||
if content:
|
||||
# 处理消息内容
|
||||
return CommingMessage(channel=MessageChannel.Wechat,
|
||||
return CommingMessage(channel=MessageChannel.Wechat, source=source,
|
||||
userid=user_id, username=user_id, text=content)
|
||||
except Exception as err:
|
||||
logger.error(f"微信消息处理发生错误:{str(err)}")
|
||||
return None
|
||||
|
||||
@checkMessage(MessageChannel.Wechat)
|
||||
def checkMessage(self, message: Notification, source: str) -> bool:
|
||||
"""
|
||||
检查消息渠道及消息类型,如不符合则不处理
|
||||
"""
|
||||
# 检查消息渠道
|
||||
if message.channel and message.channel != self._channel:
|
||||
return False
|
||||
# 检查消息来源
|
||||
if message.source and message.source != source:
|
||||
return False
|
||||
# 检查消息类型开关
|
||||
if message.mtype:
|
||||
conf = self.get_config(source)
|
||||
if conf:
|
||||
switchs = conf.switchs or []
|
||||
if message.mtype.value not in switchs:
|
||||
return False
|
||||
return True
|
||||
|
||||
def post_message(self, message: Notification) -> None:
|
||||
"""
|
||||
发送消息
|
||||
:param message: 消息内容
|
||||
:return: 成功或失败
|
||||
"""
|
||||
self.wechat.send_msg(title=message.title, text=message.text,
|
||||
image=message.image, userid=message.userid, link=message.link)
|
||||
for conf in self._configs.values():
|
||||
if not self.checkMessage(message, conf.name):
|
||||
continue
|
||||
client = self.get_client(conf.name)
|
||||
if client:
|
||||
client.send_msg(title=message.title, text=message.text,
|
||||
image=message.image, userid=message.userid, link=message.link)
|
||||
|
||||
@checkMessage(MessageChannel.Wechat)
|
||||
def post_medias_message(self, message: Notification, medias: List[MediaInfo]) -> Optional[bool]:
|
||||
def post_medias_message(self, message: Notification, medias: List[MediaInfo]) -> None:
|
||||
"""
|
||||
发送媒体信息选择列表
|
||||
:param message: 消息内容
|
||||
:param medias: 媒体列表
|
||||
:return: 成功或失败
|
||||
"""
|
||||
# 先发送标题
|
||||
self.wechat.send_msg(title=message.title, userid=message.userid, link=message.link)
|
||||
# 再发送内容
|
||||
return self.wechat.send_medias_msg(medias=medias, userid=message.userid)
|
||||
for conf in self._configs.values():
|
||||
if not self.checkMessage(message, conf.name):
|
||||
continue
|
||||
client = self.get_client(conf.name)
|
||||
if client:
|
||||
# 先发送标题
|
||||
client.send_msg(title=message.title, userid=message.userid, link=message.link)
|
||||
# 再发送内容
|
||||
client.send_medias_msg(medias=medias, userid=message.userid)
|
||||
|
||||
@checkMessage(MessageChannel.Wechat)
|
||||
def post_torrents_message(self, message: Notification, torrents: List[Context]) -> Optional[bool]:
|
||||
def post_torrents_message(self, message: Notification, torrents: List[Context]) -> None:
|
||||
"""
|
||||
发送种子信息选择列表
|
||||
:param message: 消息内容
|
||||
:param torrents: 种子列表
|
||||
:return: 成功或失败
|
||||
"""
|
||||
return self.wechat.send_torrents_msg(title=message.title, torrents=torrents,
|
||||
userid=message.userid, link=message.link)
|
||||
for conf in self._configs.values():
|
||||
if not self.checkMessage(message, conf.name):
|
||||
continue
|
||||
client = self.get_client(conf.name)
|
||||
if client:
|
||||
client.send_torrents_msg(title=message.title, torrents=torrents,
|
||||
userid=message.userid, link=message.link)
|
||||
|
||||
def register_commands(self, commands: Dict[str, dict]):
|
||||
"""
|
||||
注册命令,实现这个函数接收系统可用的命令菜单
|
||||
:param commands: 命令字典
|
||||
"""
|
||||
self.wechat.create_menus(commands)
|
||||
for client in self._clients.values():
|
||||
client.create_menus(commands)
|
||||
|
||||
Reference in New Issue
Block a user