2023-09-07 16:05:48 +08:00

169 lines
5.5 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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 = 1
# 私有属性
_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',
'props': {
'cols': 12,
'md': 4
},
'content': [
{
'component': 'VSelect',
'props': {
'model': 'request_method',
'label': '请求方式',
'items': request_options
}
}
]
},
{
'component': 'VCol',
'props': {
'cols': 12,
'md': 8
},
'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._enabled or not self._webhook_url:
return
def __to_dict(_event):
result = {}
for key, value in _event.items():
if hasattr(value, 'to_dict'):
result[key] = value.to_dict()
else:
result[key] = str(value)
return result
event_info = {
"type": event.event_type,
"data": __to_dict(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