fix messages

This commit is contained in:
jxxghp
2024-07-02 13:50:41 +08:00
parent 9484093d22
commit 15a7297099
26 changed files with 555 additions and 233 deletions

View File

@@ -1,42 +1,91 @@
import json
import re
from typing import Optional, Union, List, Tuple, Any
from typing import Optional, Union, List, Tuple, Any, Dict
from app.core.context import MediaInfo, Context
from app.core.config import settings
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.slack.slack import Slack
from app.schemas import MessageChannel, CommingMessage, Notification
from app.schemas import MessageChannel, CommingMessage, Notification, NotificationConf
class SlackModule(_ModuleBase):
slack: Slack = None
_channel = MessageChannel.Telegram
_configs: Dict[str, NotificationConf] = {}
_clients: Dict[str, Slack] = {}
def init_module(self) -> None:
self.slack = Slack()
"""
初始化模块
"""
clients = NotificationHelper().get_notifications()
if not clients:
return
self._configs = {}
self._clients = {}
for client in clients:
if client.type == "telegram" and client.enabled:
self._configs[client.name] = client
self._clients[client.name] = Slack(**client.config, name=client.name)
@staticmethod
def get_name() -> str:
return "Slack"
def get_client(self, name: str) -> Optional[Slack]:
"""
获取Telegram客户端
"""
return self._clients.get(name)
def get_config(self, name: str) -> Optional[NotificationConf]:
"""
获取Telegram配置
"""
return self._configs.get(name)
def stop(self):
self.slack.stop()
"""
停止模块
"""
for client in self._clients.values():
client.stop()
def test(self) -> Tuple[bool, str]:
"""
测试模块连接性
"""
state = self.slack.get_state()
if state:
return True, ""
return False, "Slack未就续,请检查参数设置和网络连接"
for name, client in self._clients.items():
state = client.get_state()
if not state:
return False, f"Slack {name} 未就续"
return True, ""
def init_setting(self) -> Tuple[str, Union[str, bool]]:
return "MESSAGER", "slack"
pass
@staticmethod
def message_parser(body: Any, form: Any,
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 message_parser(self, body: Any, form: Any,
args: Any) -> Optional[CommingMessage]:
"""
解析消息内容,返回字典,注意以下约定值:
@@ -157,6 +206,14 @@ class SlackModule(_ModuleBase):
]
}
"""
# 来源
source = args.get("source")
if not source:
return None
# 获取客户端
client = self.get_client(source)
if not client:
return None
# 校验token
token = args.get("token")
if not token or token != settings.API_TOKEN:
@@ -189,38 +246,50 @@ class SlackModule(_ModuleBase):
username = msg_json.get("user_name")
else:
return None
logger.info(f"收到Slack消息userid={userid}, username={username}, text={text}")
return CommingMessage(channel=MessageChannel.Slack,
logger.info(f"收到来自 {source}Slack消息userid={userid}, username={username}, text={text}")
return CommingMessage(channel=MessageChannel.Slack, source=source,
userid=userid, username=username, text=text)
return None
@checkMessage(MessageChannel.Slack)
def post_message(self, message: Notification) -> None:
"""
发送消息
:param message: 消息
:return: 成功或失败
"""
self.slack.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.Slack)
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: 成功或失败
"""
return self.slack.send_meidas_msg(title=message.title, 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_meidas_msg(title=message.title, medias=medias, userid=message.userid)
@checkMessage(MessageChannel.Slack)
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.slack.send_torrents_msg(title=message.title, torrents=torrents,
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_torrents_msg(title=message.title, torrents=torrents,
userid=message.userid)

View File

@@ -41,6 +41,10 @@ class Slack:
self._client = slack_app.client
self._channel = channel
# 标记消息来源
if kwargs.get("name"):
self._ds_url = f"{self._ds_url}&source={kwargs.get('name')}"
# 注册消息响应
@slack_app.event("message")
def slack_message(message):