From ee10527b6a5dcc685d9d3a51f8557fcc7858646d Mon Sep 17 00:00:00 2001 From: yubanmeiqin9048 <64676973+yubanmeiqin9048@users.noreply.github.com> Date: Sat, 5 Aug 2023 20:27:09 +0800 Subject: [PATCH] =?UTF-8?q?feat=20webhook=E6=8F=92=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/plugins/webhook/__init__.py | 151 ++++++++++++++++++++++++++++++++ 1 file changed, 151 insertions(+) create mode 100644 app/plugins/webhook/__init__.py diff --git a/app/plugins/webhook/__init__.py b/app/plugins/webhook/__init__.py new file mode 100644 index 00000000..e5ecdec3 --- /dev/null +++ b/app/plugins/webhook/__init__.py @@ -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