add 消息推送插件 * 3

This commit is contained in:
jxxghp
2023-11-05 08:42:51 +08:00
parent f700f3a417
commit 4b609caf61
6 changed files with 646 additions and 0 deletions

BIN
icons/bark.webp Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.9 KiB

BIN
icons/pushdeer.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.6 KiB

240
plugins/barkmsg/__init__.py Normal file
View File

@@ -0,0 +1,240 @@
from typing import Any, List, Dict, Tuple
from urllib.parse import quote_plus
from app.core.event import eventmanager, Event
from app.log import logger
from app.plugins import _PluginBase
from app.schemas.types import EventType, NotificationType
from app.utils.http import RequestUtils
class BarkMsg(_PluginBase):
# 插件名称
plugin_name = "Bark消息通知"
# 插件描述
plugin_desc = "支持使用Bark发送消息通知。"
# 插件图标
plugin_icon = "bark.webp"
# 主题色
plugin_color = "#FF3B30"
# 插件版本
plugin_version = "1.0"
# 插件作者
plugin_author = "jxxghp"
# 作者主页
author_url = "https://github.com/jxxghp"
# 插件配置项ID前缀
plugin_config_prefix = "barkmsg_"
# 加载顺序
plugin_order = 27
# 可使用的用户级别
auth_level = 1
# 私有属性
_enabled = False
_server = None
_apikey = None
_params = None
_msgtypes = []
def init_plugin(self, config: dict = None):
if config:
self._enabled = config.get("enabled")
self._msgtypes = config.get("msgtypes") or []
self._server = config.get("server")
self._apikey = config.get("apikey")
self._params = config.get("params")
def get_state(self) -> bool:
return self._enabled and self._server and self._apikey
@staticmethod
def get_command() -> List[Dict[str, Any]]:
pass
def get_api(self) -> List[Dict[str, Any]]:
pass
def get_form(self) -> Tuple[List[dict], Dict[str, Any]]:
"""
拼装插件配置页面需要返回两块数据1、页面配置2、数据结构
"""
# 编历 NotificationType 枚举,生成消息类型选项
MsgTypeOptions = []
for item in NotificationType:
MsgTypeOptions.append({
"title": item.value,
"value": item.name
})
return [
{
'component': 'VForm',
'content': [
{
'component': 'VRow',
'content': [
{
'component': 'VCol',
'props': {
'cols': 12,
'md': 6
},
'content': [
{
'component': 'VSwitch',
'props': {
'model': 'enabled',
'label': '启用插件',
}
}
]
}
]
},
{
'component': 'VRow',
'content': [
{
'component': 'VCol',
'props': {
'cols': 12,
'md': 4
},
'content': [
{
'component': 'VTextField',
'props': {
'model': 'server',
'label': '服务器',
'placeholder': 'http://xxx',
}
}
]
},
{
'component': 'VCol',
'props': {
'cols': 12,
'md': 4
},
'content': [
{
'component': 'VTextField',
'props': {
'model': 'apikey',
'label': '密钥',
'placeholder': '',
}
}
]
},
{
'component': 'VCol',
'props': {
'cols': 12,
'md': 4
},
'content': [
{
'component': 'VTextField',
'props': {
'model': 'params',
'label': '附加参数',
'placeholder': '',
}
}
]
}
]
},
{
'component': 'VRow',
'content': [
{
'component': 'VCol',
'props': {
'cols': 12
},
'content': [
{
'component': 'VSelect',
'props': {
'multiple': True,
'chips': True,
'model': 'msgtypes',
'label': '消息类型',
'items': MsgTypeOptions
}
}
]
}
]
},
]
}
], {
"enabled": False,
'msgtypes': [],
'server': '',
'apikey': '',
'params': ''
}
def get_page(self) -> List[dict]:
pass
@eventmanager.register(EventType.NoticeMessage)
def send(self, event: Event):
"""
消息发送事件
"""
if not self.get_state():
return
if not event.event_data:
return
msg_body = event.event_data
# 类型
msg_type: NotificationType = msg_body.get("type")
# 标题
title = msg_body.get("title")
# 文本
text = msg_body.get("text")
if not title and not text:
logger.warn("标题和内容不能同时为空")
return
if (msg_type and self._msgtypes
and msg_type.name not in self._msgtypes):
logger.info(f"消息类型 {msg_type.value} 未开启消息发送")
return
try:
if not self._server or not self._apikey:
return False, "参数未配置"
sc_url = "%s/%s/%s/%s" % (self._server, self._apikey, quote_plus(title), quote_plus(text))
if self._params:
sc_url = "%s?%s" % (sc_url, self._params)
res = RequestUtils().post_res(sc_url)
if res and res.status_code == 200:
ret_json = res.json()
code = ret_json['code']
message = ret_json['message']
if code == 200:
logger.info("Bark消息发送成功")
else:
logger.warn(f"Bark消息发送失败{message}")
elif res is not None:
logger.warn(f"Bark消息发送失败错误码{res.status_code},错误原因:{res.reason}")
else:
logger.warn(f"Bark消息发送失败未获取到返回信息")
except Exception as msg_e:
logger.error(f"Bark消息发送失败{str(msg_e)}")
def stop_service(self):
"""
退出插件
"""
pass

195
plugins/iyuumsg/__init__.py Normal file
View File

@@ -0,0 +1,195 @@
from urllib.parse import urlencode
from app.plugins import _PluginBase
from app.core.event import eventmanager, Event
from app.schemas.types import EventType, NotificationType
from app.utils.http import RequestUtils
from typing import Any, List, Dict, Tuple
from app.log import logger
class IyuuMsg(_PluginBase):
# 插件名称
plugin_name = "IYUU消息通知"
# 插件描述
plugin_desc = "支持使用IYUU发送消息通知。"
# 插件图标
plugin_icon = "iyuu.png"
# 主题色
plugin_color = "#C9221B"
# 插件版本
plugin_version = "1.0"
# 插件作者
plugin_author = "jxxghp"
# 作者主页
author_url = "https://github.com/jxxghp"
# 插件配置项ID前缀
plugin_config_prefix = "iyuumsg_"
# 加载顺序
plugin_order = 25
# 可使用的用户级别
auth_level = 1
# 私有属性
_enabled = False
_token = None
_msgtypes = []
def init_plugin(self, config: dict = None):
if config:
self._enabled = config.get("enabled")
self._token = config.get("token")
self._msgtypes = config.get("msgtypes") or []
def get_state(self) -> bool:
return self._enabled and self._token
@staticmethod
def get_command() -> List[Dict[str, Any]]:
pass
def get_api(self) -> List[Dict[str, Any]]:
pass
def get_form(self) -> Tuple[List[dict], Dict[str, Any]]:
"""
拼装插件配置页面需要返回两块数据1、页面配置2、数据结构
"""
# 编历 NotificationType 枚举,生成消息类型选项
MsgTypeOptions = []
for item in NotificationType:
MsgTypeOptions.append({
"title": item.value,
"value": item.name
})
return [
{
'component': 'VForm',
'content': [
{
'component': 'VRow',
'content': [
{
'component': 'VCol',
'props': {
'cols': 12,
'md': 6
},
'content': [
{
'component': 'VSwitch',
'props': {
'model': 'enabled',
'label': '启用插件',
}
}
]
}
]
},
{
'component': 'VRow',
'content': [
{
'component': 'VCol',
'props': {
'cols': 12
},
'content': [
{
'component': 'VTextField',
'props': {
'model': 'token',
'label': 'IYUU令牌',
'placeholder': 'IYUUxxx',
}
}
]
}
]
},
{
'component': 'VRow',
'content': [
{
'component': 'VCol',
'props': {
'cols': 12
},
'content': [
{
'component': 'VSelect',
'props': {
'multiple': True,
'chips': True,
'model': 'msgtypes',
'label': '消息类型',
'items': MsgTypeOptions
}
}
]
}
]
},
]
}
], {
"enabled": False,
'token': '',
'msgtypes': []
}
def get_page(self) -> List[dict]:
pass
@eventmanager.register(EventType.NoticeMessage)
def send(self, event: Event):
"""
消息发送事件
"""
if not self.get_state():
return
if not event.event_data:
return
msg_body = event.event_data
# 类型
msg_type: NotificationType = msg_body.get("type")
# 标题
title = msg_body.get("title")
# 文本
text = msg_body.get("text")
if not title and not text:
logger.warn("标题和内容不能同时为空")
return
if (msg_type and self._msgtypes
and msg_type.name not in self._msgtypes):
logger.info(f"消息类型 {msg_type.value} 未开启消息发送")
return
try:
sc_url = "http://iyuu.cn/%s.send?%s" % (self._token, urlencode({"text": title, "desp": text}))
res = RequestUtils().get_res(sc_url)
if res and res.status_code == 200:
ret_json = res.json()
errno = ret_json.get('errcode')
error = ret_json.get('errmsg')
if errno == 0:
logger.info("IYUU消息发送成功")
else:
logger.warn(f"IYUU消息发送失败错误码{errno},错误原因:{error}")
elif res is not None:
logger.warn(f"IYUU消息发送失败错误码{res.status_code},错误原因:{res.reason}")
else:
logger.warn("IYUU消息发送失败未获取到返回信息")
except Exception as msg_e:
logger.error(f"IYUU消息发送失败{str(msg_e)}")
def stop_service(self):
"""
退出插件
"""
pass

View File

@@ -0,0 +1,210 @@
from typing import Any, List, Dict, Tuple
from pypushdeer import PushDeer
from app.core.event import eventmanager, Event
from app.log import logger
from app.plugins import _PluginBase
from app.schemas.types import EventType, NotificationType
class PushDeerMsg(_PluginBase):
# 插件名称
plugin_name = "PushDeer消息通知"
# 插件描述
plugin_desc = "支持使用PushDeer发送消息通知。"
# 插件图标
plugin_icon = "pushdeer.png"
# 主题色
plugin_color = "#FFFFFF"
# 插件版本
plugin_version = "1.0"
# 插件作者
plugin_author = "jxxghp"
# 作者主页
author_url = "https://github.com/jxxghp"
# 插件配置项ID前缀
plugin_config_prefix = "pushdeermsg_"
# 加载顺序
plugin_order = 26
# 可使用的用户级别
auth_level = 1
# 私有属性
_enabled = False
_server = None
_apikey = None
_msgtypes = []
def init_plugin(self, config: dict = None):
if config:
self._enabled = config.get("enabled")
self._msgtypes = config.get("msgtypes") or []
self._server = config.get("server")
self._apikey = config.get("apikey")
def get_state(self) -> bool:
return self._enabled and self._server and self._apikey
@staticmethod
def get_command() -> List[Dict[str, Any]]:
pass
def get_api(self) -> List[Dict[str, Any]]:
pass
def get_form(self) -> Tuple[List[dict], Dict[str, Any]]:
"""
拼装插件配置页面需要返回两块数据1、页面配置2、数据结构
"""
# 编历 NotificationType 枚举,生成消息类型选项
MsgTypeOptions = []
for item in NotificationType:
MsgTypeOptions.append({
"title": item.value,
"value": item.name
})
return [
{
'component': 'VForm',
'content': [
{
'component': 'VRow',
'content': [
{
'component': 'VCol',
'props': {
'cols': 12,
'md': 6
},
'content': [
{
'component': 'VSwitch',
'props': {
'model': 'enabled',
'label': '启用插件',
}
}
]
}
]
},
{
'component': 'VRow',
'content': [
{
'component': 'VCol',
'props': {
'cols': 12,
'md': 6
},
'content': [
{
'component': 'VTextField',
'props': {
'model': 'server',
'label': '服务器',
'placeholder': 'http://xxx',
}
}
]
},
{
'component': 'VCol',
'props': {
'cols': 12,
'md': 6
},
'content': [
{
'component': 'VTextField',
'props': {
'model': 'apikey',
'label': '密钥',
'placeholder': 'PDUUxxx',
}
}
]
}
]
},
{
'component': 'VRow',
'content': [
{
'component': 'VCol',
'props': {
'cols': 12
},
'content': [
{
'component': 'VSelect',
'props': {
'multiple': True,
'chips': True,
'model': 'msgtypes',
'label': '消息类型',
'items': MsgTypeOptions
}
}
]
}
]
},
]
}
], {
"enabled": False,
'msgtypes': [],
'server': '',
'apikey': ''
}
def get_page(self) -> List[dict]:
pass
@eventmanager.register(EventType.NoticeMessage)
def send(self, event: Event):
"""
消息发送事件
"""
if not self.get_state():
return
if not event.event_data:
return
msg_body = event.event_data
# 类型
msg_type: NotificationType = msg_body.get("type")
# 标题
title = msg_body.get("title")
# 文本
text = msg_body.get("text")
if not title and not text:
logger.warn("标题和内容不能同时为空")
return
if (msg_type and self._msgtypes
and msg_type.name not in self._msgtypes):
logger.info(f"消息类型 {msg_type.value} 未开启消息发送")
return
try:
if not self._server or not self._apikey:
return False, "参数未配置"
pushdeer = PushDeer(server=self._server, pushkey=self._apikey)
res = pushdeer.send_markdown(title, desp=text)
if res:
logger.info(f"PushDeer消息发送成功")
else:
logger.warn(f"PushDeer消息发送失败")
except Exception as msg_e:
logger.error(f"PushDeer消息发送失败错误信息{str(msg_e)}")
def stop_service(self):
"""
退出插件
"""
pass

View File

@@ -0,0 +1 @@
pypushdeer~=0.0.3