MCPcopy Create free account
hub / github.com/aiprodcoder/MIXAPI / SendWebhookNotify

Function SendWebhookNotify

service/webhook.go:33–118  ·  view source on GitHub ↗

SendWebhookNotify 发送 webhook 通知

(webhookURL string, secret string, data dto.Notify)

Source from the content-addressed store, hash-verified

31
32// SendWebhookNotify 发送 webhook 通知
33func SendWebhookNotify(webhookURL string, secret string, data dto.Notify) error {
34 // 处理占位符
35 content := data.Content
36 for _, value := range data.Values {
37 content = fmt.Sprintf(content, value)
38 }
39
40 // 构建 webhook 负载
41 payload := WebhookPayload{
42 Type: data.Type,
43 Title: data.Title,
44 Content: content,
45 Values: data.Values,
46 Timestamp: time.Now().Unix(),
47 }
48
49 // 序列化负载
50 payloadBytes, err := json.Marshal(payload)
51 if err != nil {
52 return fmt.Errorf("failed to marshal webhook payload: %v", err)
53 }
54
55 // 创建 HTTP 请求
56 var req *http.Request
57 var resp *http.Response
58
59 if setting.EnableWorker() {
60 // 构建worker请求数据
61 workerReq := &WorkerRequest{
62 URL: webhookURL,
63 Key: setting.WorkerValidKey,
64 Method: http.MethodPost,
65 Headers: map[string]string{
66 "Content-Type": "application/json",
67 },
68 Body: payloadBytes,
69 }
70
71 // 如果有secret,添加签名到headers
72 if secret != "" {
73 signature := generateSignature(secret, payloadBytes)
74 workerReq.Headers["X-Webhook-Signature"] = signature
75 workerReq.Headers["Authorization"] = "Bearer " + secret
76 }
77
78 resp, err = DoWorkerRequest(workerReq)
79 if err != nil {
80 return fmt.Errorf("failed to send webhook request through worker: %v", err)
81 }
82 defer resp.Body.Close()
83
84 // 检查响应状态
85 if resp.StatusCode < 200 || resp.StatusCode >= 300 {
86 return fmt.Errorf("webhook request failed with status code: %d", resp.StatusCode)
87 }
88 } else {
89 req, err = http.NewRequest(http.MethodPost, webhookURL, bytes.NewBuffer(payloadBytes))
90 if err != nil {

Callers 1

NotifyUserFunction · 0.85

Calls 4

EnableWorkerFunction · 0.92
generateSignatureFunction · 0.85
DoWorkerRequestFunction · 0.85
GetHttpClientFunction · 0.85

Tested by

no test coverage detected