Merge pull request #33 from yubanmeiqin9048/main

This commit is contained in:
jxxghp 2023-08-05 22:28:59 +08:00 committed by GitHub
commit 983105e83f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -0,0 +1,151 @@
from app.plugins import _PluginBase
from app.core.event import eventmanager
from app.schemas.types import EventType
from app.utils.http import RequestUtils
from typing import Any, List, Dict, Tuple
from app.log import logger
class WebHook(_PluginBase):
# 插件名称
plugin_name = "Webhook"
# 插件描述
plugin_desc = "事件发生时向第三方地址发送请求。"
# 插件图标
plugin_icon = "webhook.png"
# 主题色
plugin_color = "#C73A63"
# 插件版本
plugin_version = "1.0"
# 插件作者
plugin_author = "jxxghp"
# 作者主页
author_url = "https://github.com/jxxghp"
# 插件配置项ID前缀
plugin_config_prefix = "webhook_"
# 加载顺序
plugin_order = 14
# 可使用的用户级别
auth_level = 2
# 私有属性
_webhook_url = None
_method = None
_enabled = False
def init_plugin(self, config: dict = None):
if config:
self._enabled = config.get("enabled")
self._webhook_url = config.get("webhook_url")
self._method = config.get('request_method')
def get_state(self) -> bool:
return self._enabled
@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数据结构
"""
request_options = ["POST","GET"]
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',
'content': [
{
'component': 'VSelect',
'props': {
'model': 'request_method',
'label': '请求方式',
'items': request_options
}
}
]
},
{
'component': 'VCol',
'props': {
'cols': 12
},
'content': [
{
'component': 'VTextField',
'props': {
'model': 'webhook_url',
'label': 'webhook地址'
}
}
]
}
]
},
]
}
], {
"enabled": False,
"request_method": "POST",
"webhook_url": ""
}
def get_page(self) -> List[dict]:
pass
@eventmanager.register(EventType)
def send(self, event):
"""
向第三方Webhook发送请求
"""
if not self._webhook_url:
return
event_info = {
"type": event.event_type,
"data": str(event.event_data)
}
if self._method == 'POST':
ret = RequestUtils(content_type="application/json").post_res(self._webhook_url, json=event_info)
else:
ret = RequestUtils().get_res(self._webhook_url, params=event_info)
if ret:
logger.info("发送成功:%s" % self._webhook_url)
elif ret is not None:
logger.error(f"发送失败,状态码:{ret.status_code},返回信息:{ret.text} {ret.reason}")
else:
logger.error("发送失败,未获取到返回信息")
def stop_service(self):
"""
退出插件
"""
pass