|
|
@@ -2,9 +2,12 @@ package service
|
|
|
|
|
|
import (
|
|
|
"fmt"
|
|
|
+ "net/http"
|
|
|
+ "net/url"
|
|
|
"one-api/common"
|
|
|
"one-api/dto"
|
|
|
"one-api/model"
|
|
|
+ "one-api/setting"
|
|
|
"strings"
|
|
|
)
|
|
|
|
|
|
@@ -51,6 +54,13 @@ func NotifyUser(userId int, userEmail string, userSetting dto.UserSetting, data
|
|
|
// 获取 webhook secret
|
|
|
webhookSecret := userSetting.WebhookSecret
|
|
|
return SendWebhookNotify(webhookURLStr, webhookSecret, data)
|
|
|
+ case dto.NotifyTypeBark:
|
|
|
+ barkURL := userSetting.BarkUrl
|
|
|
+ if barkURL == "" {
|
|
|
+ common.SysLog(fmt.Sprintf("user %d has no bark url, skip sending bark", userId))
|
|
|
+ return nil
|
|
|
+ }
|
|
|
+ return sendBarkNotify(barkURL, data)
|
|
|
}
|
|
|
return nil
|
|
|
}
|
|
|
@@ -64,3 +74,67 @@ func sendEmailNotify(userEmail string, data dto.Notify) error {
|
|
|
}
|
|
|
return common.SendEmail(data.Title, userEmail, content)
|
|
|
}
|
|
|
+
|
|
|
+func sendBarkNotify(barkURL string, data dto.Notify) error {
|
|
|
+ // 处理占位符
|
|
|
+ content := data.Content
|
|
|
+ for _, value := range data.Values {
|
|
|
+ content = strings.Replace(content, dto.ContentValueParam, fmt.Sprintf("%v", value), 1)
|
|
|
+ }
|
|
|
+
|
|
|
+ // 替换模板变量
|
|
|
+ finalURL := strings.ReplaceAll(barkURL, "{{title}}", url.QueryEscape(data.Title))
|
|
|
+ finalURL = strings.ReplaceAll(finalURL, "{{content}}", url.QueryEscape(content))
|
|
|
+
|
|
|
+ // 发送GET请求到Bark
|
|
|
+ var req *http.Request
|
|
|
+ var resp *http.Response
|
|
|
+ var err error
|
|
|
+
|
|
|
+ if setting.EnableWorker() {
|
|
|
+ // 使用worker发送请求
|
|
|
+ workerReq := &WorkerRequest{
|
|
|
+ URL: finalURL,
|
|
|
+ Key: setting.WorkerValidKey,
|
|
|
+ Method: http.MethodGet,
|
|
|
+ Headers: map[string]string{
|
|
|
+ "User-Agent": "OneAPI-Bark-Notify/1.0",
|
|
|
+ },
|
|
|
+ }
|
|
|
+
|
|
|
+ resp, err = DoWorkerRequest(workerReq)
|
|
|
+ if err != nil {
|
|
|
+ return fmt.Errorf("failed to send bark request through worker: %v", err)
|
|
|
+ }
|
|
|
+ defer resp.Body.Close()
|
|
|
+
|
|
|
+ // 检查响应状态
|
|
|
+ if resp.StatusCode < 200 || resp.StatusCode >= 300 {
|
|
|
+ return fmt.Errorf("bark request failed with status code: %d", resp.StatusCode)
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ // 直接发送请求
|
|
|
+ req, err = http.NewRequest(http.MethodGet, finalURL, nil)
|
|
|
+ if err != nil {
|
|
|
+ return fmt.Errorf("failed to create bark request: %v", err)
|
|
|
+ }
|
|
|
+
|
|
|
+ // 设置User-Agent
|
|
|
+ req.Header.Set("User-Agent", "OneAPI-Bark-Notify/1.0")
|
|
|
+
|
|
|
+ // 发送请求
|
|
|
+ client := GetHttpClient()
|
|
|
+ resp, err = client.Do(req)
|
|
|
+ if err != nil {
|
|
|
+ return fmt.Errorf("failed to send bark request: %v", err)
|
|
|
+ }
|
|
|
+ defer resp.Body.Close()
|
|
|
+
|
|
|
+ // 检查响应状态
|
|
|
+ if resp.StatusCode < 200 || resp.StatusCode >= 300 {
|
|
|
+ return fmt.Errorf("bark request failed with status code: %d", resp.StatusCode)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return nil
|
|
|
+}
|