feat: add custom webhook headers config #50
This commit is contained in:
@@ -16,8 +16,18 @@ var Client = &http.Client{
|
|||||||
Timeout: 5 * time.Second,
|
Timeout: 5 * time.Second,
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cw *CustomWebhook) Request(url string, msg string) ([]byte, error) {
|
func (cw *CustomWebhook) Request(url string, msg string, headers map[string]string) ([]byte, error) {
|
||||||
resp, err := Client.Post(url, "application/json", bytes.NewBuffer([]byte(msg)))
|
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 {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,10 @@
|
|||||||
package channels
|
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 }
|
type CustomChannel struct{ *BaseChannel }
|
||||||
|
|
||||||
@@ -11,11 +15,19 @@ func NewCustomChannel() Channel {
|
|||||||
func (c *CustomChannel) Send(config ChannelConfig, msg *Message) (*Result, error) {
|
func (c *CustomChannel) Send(config ChannelConfig, msg *Message) (*Result, error) {
|
||||||
webhook := config.GetString("webhook")
|
webhook := config.GetString("webhook")
|
||||||
body := config.GetString("body")
|
body := config.GetString("body")
|
||||||
|
headersStr := config.GetString("headers")
|
||||||
|
|
||||||
if webhook == "" {
|
if webhook == "" {
|
||||||
return SendError("custom config missing: webhook is required"), nil
|
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)
|
_, formattedContent := c.FormatContent(msg)
|
||||||
cli := message.CustomWebhook{}
|
cli := message.CustomWebhook{}
|
||||||
|
|
||||||
@@ -27,7 +39,7 @@ func (c *CustomChannel) Send(config ChannelConfig, msg *Message) (*Result, error
|
|||||||
bodyStr = formattedContent
|
bodyStr = formattedContent
|
||||||
}
|
}
|
||||||
|
|
||||||
res, err := cli.Request(webhook, bodyStr)
|
res, err := cli.Request(webhook, bodyStr, headers)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return ErrorResult(string(res), err), nil
|
return ErrorResult(string(res), err), nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -76,6 +76,7 @@ const channelConfigFields: Record<string, { key: string; label: string; required
|
|||||||
],
|
],
|
||||||
Custom: [
|
Custom: [
|
||||||
{ key: 'webhook', label: 'Webhook URL', required: true, placeholder: 'https://...' },
|
{ key: 'webhook', label: 'Webhook URL', required: true, placeholder: 'https://...' },
|
||||||
|
{ key: 'headers', label: '请求头', required: false, placeholder: 'JSON格式,如 {"Authorization": "Bearer ..."}', type: 'textarea' },
|
||||||
{ key: 'body', label: '请求体模板', required: false, placeholder: '使用 TEXT 作为消息内容占位符', type: 'textarea' },
|
{ key: 'body', label: '请求体模板', required: false, placeholder: '使用 TEXT 作为消息内容占位符', type: 'textarea' },
|
||||||
],
|
],
|
||||||
Ntfy: [
|
Ntfy: [
|
||||||
|
|||||||
Reference in New Issue
Block a user