From e7da316c90486ec674a3bfc70fd4d7b9026fbe57 Mon Sep 17 00:00:00 2001 From: engigu Date: Mon, 9 Mar 2026 14:03:04 +0800 Subject: [PATCH] feat: add custom webhook headers config #50 --- internal/sdk/message/custom.go | 14 ++++++++++++-- internal/sdk/messenger/channels/custom.go | 16 ++++++++++++++-- web/src/views/notify/Notify.vue | 1 + 3 files changed, 27 insertions(+), 4 deletions(-) diff --git a/internal/sdk/message/custom.go b/internal/sdk/message/custom.go index 19f9dc8..3269a6c 100644 --- a/internal/sdk/message/custom.go +++ b/internal/sdk/message/custom.go @@ -16,8 +16,18 @@ var Client = &http.Client{ Timeout: 5 * time.Second, } -func (cw *CustomWebhook) Request(url string, msg string) ([]byte, error) { - resp, err := Client.Post(url, "application/json", bytes.NewBuffer([]byte(msg))) +func (cw *CustomWebhook) Request(url string, msg string, headers map[string]string) ([]byte, error) { + req, err := http.NewRequest("POST", url, bytes.NewBuffer([]byte(msg))) + if err != nil { + return nil, err + } + + req.Header.Set("Content-Type", "application/json") + for k, v := range headers { + req.Header.Set(k, v) + } + + resp, err := Client.Do(req) if err != nil { return nil, err } diff --git a/internal/sdk/messenger/channels/custom.go b/internal/sdk/messenger/channels/custom.go index 89a5836..121fbff 100644 --- a/internal/sdk/messenger/channels/custom.go +++ b/internal/sdk/messenger/channels/custom.go @@ -1,6 +1,10 @@ package channels -import "github.com/engigu/baihu-panel/internal/sdk/message" +import ( + "encoding/json" + + "github.com/engigu/baihu-panel/internal/sdk/message" +) type CustomChannel struct{ *BaseChannel } @@ -11,11 +15,19 @@ func NewCustomChannel() Channel { func (c *CustomChannel) Send(config ChannelConfig, msg *Message) (*Result, error) { webhook := config.GetString("webhook") body := config.GetString("body") + headersStr := config.GetString("headers") if webhook == "" { return SendError("custom config missing: webhook is required"), nil } + var headers map[string]string + if headersStr != "" { + if err := json.Unmarshal([]byte(headersStr), &headers); err != nil { + return SendError("custom config error: headers must be a valid JSON object"), nil + } + } + _, formattedContent := c.FormatContent(msg) cli := message.CustomWebhook{} @@ -27,7 +39,7 @@ func (c *CustomChannel) Send(config ChannelConfig, msg *Message) (*Result, error bodyStr = formattedContent } - res, err := cli.Request(webhook, bodyStr) + res, err := cli.Request(webhook, bodyStr, headers) if err != nil { return ErrorResult(string(res), err), nil } diff --git a/web/src/views/notify/Notify.vue b/web/src/views/notify/Notify.vue index 2be6638..66224d5 100644 --- a/web/src/views/notify/Notify.vue +++ b/web/src/views/notify/Notify.vue @@ -76,6 +76,7 @@ const channelConfigFields: Record