diff --git a/app/api/setting.py b/app/api/setting.py index e0014bf..440f78f 100644 --- a/app/api/setting.py +++ b/app/api/setting.py @@ -29,7 +29,7 @@ from fastapi import APIRouter, Body from app.core import Config from app.services import System, Notify from app.models.schema import * -import uuid +from app.models.config import Webhook as WebhookConfig router = APIRouter(prefix="/api/setting", tags=["全局设置"]) @@ -180,31 +180,19 @@ async def reorder_webhook(webhook: WebhookReorderIn = Body(...)) -> OutBase: @router.post( - "/webhook/test", - summary="测试自定义Webhook", - response_model=OutBase, - status_code=200, + "/webhook/test", summary="测试Webhook配置", response_model=OutBase, status_code=200 ) -async def test_webhook(webhook_data: dict = Body(...)) -> OutBase: +async def test_webhook(webhook: WebhookTestIn = Body(...)) -> OutBase: """测试自定义Webhook""" try: - webhook_config = { - "name": webhook_data.get("name", "测试Webhook"), - "url": webhook_data.get("url", ""), - "template": webhook_data.get("template", ""), - "enabled": True, - "headers": webhook_data.get("headers", {}), - "method": webhook_data.get("method", "POST"), - } - - await Notify.CustomWebhookPush( + webhook_config = WebhookConfig() + await webhook_config.load(webhook.data.model_dump()) + await Notify.WebhookPush( "AUTO-MAS Webhook测试", "这是一条测试消息,如果您收到此消息,说明Webhook配置正确!", webhook_config, ) - - return OutBase(message="Webhook测试成功") - except Exception as e: return OutBase(code=500, status="error", message=f"Webhook测试失败: {str(e)}") + return OutBase() diff --git a/app/models/schema.py b/app/models/schema.py index 0a04d5c..e28c213 100644 --- a/app/models/schema.py +++ b/app/models/schema.py @@ -86,10 +86,10 @@ class Webhook_Info(BaseModel): class Webhook_Data(BaseModel): - url: Optional[str] = Field(default=None, description="Webhook URL") - template: Optional[str] = Field(default=None, description="消息模板") - headers: Optional[Dict[str, str]] = Field(default=None, description="自定义请求头") - method: Optional[Literal["POST", "GET"]] = Field( + Url: Optional[str] = Field(default=None, description="Webhook URL") + Template: Optional[str] = Field(default=None, description="消息模板") + Headers: Optional[Dict[str, str]] = Field(default=None, description="自定义请求头") + Method: Optional[Literal["POST", "GET"]] = Field( default=None, description="请求方法" ) @@ -664,6 +664,10 @@ class WebhookReorderIn(WebhookInBase): indexList: List[str] = Field(..., description="Webhook ID列表, 按新顺序排列") +class WebhookTestIn(WebhookInBase): + data: Webhook = Field(..., description="Webhook配置数据") + + class PlanCreateIn(BaseModel): type: Literal["MaaPlan"]