-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathnotification.go
More file actions
163 lines (135 loc) · 3.99 KB
/
notification.go
File metadata and controls
163 lines (135 loc) · 3.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package main
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"net/smtp"
"time"
)
type ChannelChange struct {
ChannelID int `json:"channel_id"`
ChannelName string `json:"channel_name"`
OldModels []string `json:"old_models"`
NewModels []string `json:"new_models"`
AddedModels []string `json:"added_models"`
RemovedModels []string `json:"removed_models"`
}
func sendNotification(change ChannelChange) error {
var e1, e2 error
if config.Notification.SMTP.Enabled {
if err := sendEmailNotification(change); err != nil {
e1 = fmt.Errorf("发送邮件通知失败: %v", err)
}
}
if config.Notification.Webhook.Enabled {
if err := sendWebhookNotification(change); err != nil {
e2 = fmt.Errorf("发送Webhook通知失败: %v", err)
}
}
if e1 != nil && e2 != nil {
return fmt.Errorf("%v\n%v", e1, e2)
}
if e1 != nil {
return e1
}
if e2 != nil {
return e2
}
return nil
}
func sendEmailNotification(change ChannelChange) error {
smtpConfig := config.Notification.SMTP
auth := smtp.PlainAuth("", smtpConfig.Username, smtpConfig.Password, smtpConfig.Host)
subject := "渠道模型变更通知"
body := fmt.Sprintf(`
渠道ID: %d
渠道名称: %s
新增模型: %v
移除模型: %v
最新可用模型: %v
`, change.ChannelID, change.ChannelName, change.AddedModels, change.RemovedModels, change.NewModels)
msg := fmt.Sprintf("From: %s\r\n"+
"To: %s\r\n"+
"Subject: %s\r\n"+
"\r\n"+
"%s\r\n", smtpConfig.From, smtpConfig.To, subject, body)
addr := fmt.Sprintf("%s:%d", smtpConfig.Host, smtpConfig.Port)
return smtp.SendMail(addr, auth, smtpConfig.From, []string{smtpConfig.To}, []byte(msg))
}
func sendWebhookNotification(change ChannelChange) error {
msg := fmt.Sprintf(`
渠道ID: %d
渠道名称: %s
新增模型: %v
移除模型: %v
最新可用模型: %v
`, change.ChannelID, change.ChannelName, change.AddedModels, change.RemovedModels, change.NewModels)
if config.Notification.Webhook.Type == "telegram" {
if err := sendTelegramNotification(msg); err != nil {
return fmt.Errorf("发送Telegram通知失败: %v", err)
}
}
return nil
}
func sendTelegramNotification(msg string) error {
telegramConfig := config.Notification.Webhook.Telegram
url := fmt.Sprintf("https://api.telegram.org/bot%s/sendMessage", config.Notification.Webhook.Secret)
data := map[string]string{
"chat_id": telegramConfig.ChatID,
"text": msg,
}
body, err := json.Marshal(data)
if err != nil {
return fmt.Errorf("序列化数据失败: %v", err)
}
// 重试逻辑
var lastErr error
for attempt := 1; attempt <= telegramConfig.Retry; attempt++ {
resp, err := http.Post(url, "application/json", bytes.NewReader(body))
if err != nil {
lastErr = fmt.Errorf("发送请求失败 (尝试 %d/%d): %v",
attempt, telegramConfig.Retry, err)
if attempt < telegramConfig.Retry {
time.Sleep(time.Second * 2) // 重试前等待2秒
continue
}
return lastErr
}
defer resp.Body.Close()
if resp.StatusCode == http.StatusOK {
return nil
}
lastErr = fmt.Errorf("请求失败 (尝试 %d/%d): %v",
attempt, telegramConfig.Retry, resp.Status)
if attempt < telegramConfig.Retry {
time.Sleep(time.Second * 2)
continue
}
}
return lastErr
}
func compareModels(old, new []string) ([]string, []string) {
oldMap := make(map[string]bool)
for _, model := range old {
oldMap[model] = true
}
newMap := make(map[string]bool)
for _, model := range new {
newMap[model] = true
}
var added, removed []string
// 查找新增的模型
for model := range newMap {
if !oldMap[model] {
added = append(added, model)
}
}
// 查找移除的模型
for model := range oldMap {
if !newMap[model] {
removed = append(removed, model)
}
}
return added, removed
}