Skip to content

Commit 05e21f8

Browse files
committed
feat: notify
1 parent fa4db41 commit 05e21f8

5 files changed

Lines changed: 98 additions & 46 deletions

File tree

config/config.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ var Config struct {
5353
QQBotCodingGroupID *int64 `env:"CODING_GROUP_ID"`
5454
QQBotUserID *int64 `env:"USER_ID"`
5555
QQBotUrl *string `env:"QQ_BOT_URL"`
56-
FeishuBotUrl *string `env:"FEISHU_BOT_URL"`
56+
FeishuAdminNotifierUrl *string `env:"FEISHU_ADMIN_NOTIFIER_URL"`
57+
FeishuDivisionNotifierUrl *string `env:"FEISHU_DIVISION_NOTIFIER_URL"`
5758
AdminOnlyTagIds []int `env:"ADMIN_ONLY_TAG_IDS"`
5859
AISummaryURL string `env:"AI_SUMMARY_URL" envDefault:"http://localhost:8080/internal"`
5960
SummaryFloorLimit int `env:"SUMMARY_FLOOR_LIMIT" envDefault:"15"`

models/floor.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -596,16 +596,16 @@ func (floor *Floor) SendSensitive(_ *gorm.DB) error {
596596
}
597597

598598
// construct message
599+
desc := fmt.Sprintf("有新的敏感内容需要审核,floorID: %d", floor.ID)
599600
message := Notification{
600601
Data: floor,
601602
Recipients: userIDs,
602-
Description: "Sensitive Review Required",
603+
Description: desc,
603604
Title: "您有待审核的内容",
604605
Type: MessageTypeSensitive,
605606
URL: fmt.Sprintf("/api/floors/%d", floor.ID),
606607
}
607-
608-
// send
609608
_, err := message.Send()
609+
utils.Notify(utils.NotificationTargetFeishuAdmin, desc)
610610
return err
611611
}

models/hole.go

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -565,31 +565,19 @@ func (hole *Hole) HoleHook() {
565565
}
566566

567567
if hole.DivisionID == 4 {
568-
go utils.NotifyQQ(&utils.BotMessage{
569-
MessageType: utils.MessageTypePrivate,
570-
UserID: config.Config.QQBotUserID,
571-
Message: notifyMessage,
572-
})
573-
go utils.NotifyFeishu(&utils.FeishuMessage{
574-
MsgType: "text",
575-
Content: notifyMessage,
576-
})
577-
}
578-
579-
tagToGroup := map[string]*int64{
580-
"@物理大神": config.Config.QQBotPhysicsGroupID,
581-
"@码上辅导": config.Config.QQBotCodingGroupID,
568+
utils.Notify(utils.NotificationTargetQQUser, notifyMessage)
569+
utils.Notify(utils.NotificationTargetFeishuDivision, notifyMessage)
582570
}
583571

584572
for _, tag := range hole.Tags {
585-
if tag != nil {
586-
if groupID, ok := tagToGroup[tag.Name]; ok {
587-
go utils.NotifyQQ(&utils.BotMessage{
588-
MessageType: utils.MessageTypeGroup,
589-
GroupID: groupID,
590-
Message: notifyMessage,
591-
})
592-
}
573+
if tag == nil {
574+
continue
575+
}
576+
switch tag.Name {
577+
case "@物理大神":
578+
utils.Notify(utils.NotificationTargetQQPhysicsGroup, notifyMessage)
579+
case "@码上辅导":
580+
utils.Notify(utils.NotificationTargetQQCodingGroup, notifyMessage)
593581
}
594582
}
595583
}

models/report.go

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"sync/atomic"
77
"time"
8+
"treehole_next/utils"
89

910
"github.com/rs/zerolog/log"
1011

@@ -152,21 +153,21 @@ func (report *Report) SendCreate(_ *gorm.DB) error {
152153
userIDs := []int{adminList.data[currentCounter-1]}
153154

154155
// construct message
156+
desc := fmt.Sprintf(
157+
"理由:%s,内容:%s",
158+
report.Reason,
159+
report.Floor.Content,
160+
)
155161
message := Notification{
156-
Data: report,
157-
Recipients: userIDs,
158-
Description: fmt.Sprintf(
159-
"理由:%s,内容:%s",
160-
report.Reason,
161-
report.Floor.Content,
162-
),
163-
Title: "您有举报需要处理",
164-
Type: MessageTypeReport,
165-
URL: fmt.Sprintf("/api/reports/%d", report.ID),
162+
Data: report,
163+
Recipients: userIDs,
164+
Description: desc,
165+
Title: "您有举报需要处理",
166+
Type: MessageTypeReport,
167+
URL: fmt.Sprintf("/api/reports/%d", report.ID),
166168
}
167-
168-
// send
169169
_, err := message.Send()
170+
utils.Notify(utils.NotificationTargetFeishuDivision, desc)
170171
return err
171172
}
172173

utils/bot.go

Lines changed: 70 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,37 +6,99 @@ import (
66
"fmt"
77
"io"
88
"net/http"
9+
"time"
910
"treehole_next/config"
1011
)
1112

13+
type NotificationTarget string
14+
15+
const (
16+
NotificationTargetQQUser NotificationTarget = "qq_user"
17+
NotificationTargetQQPhysicsGroup NotificationTarget = "qq_physics_group"
18+
NotificationTargetQQCodingGroup NotificationTarget = "qq_coding_group"
19+
NotificationTargetFeishuAdmin NotificationTarget = "feishu_admin"
20+
NotificationTargetFeishuDivision NotificationTarget = "feishu_division"
21+
)
22+
23+
type Notifier interface {
24+
Notify(target NotificationTarget, message string)
25+
}
26+
1227
type BotMessageType string
1328

1429
const (
1530
MessageTypeGroup BotMessageType = "group"
1631
MessageTypePrivate BotMessageType = "private"
1732
)
1833

19-
type BotMessage struct {
34+
type qqBotMessage struct {
2035
MessageType BotMessageType `json:"message_type"`
2136
GroupID *int64 `json:"group_id"`
2237
UserID *int64 `json:"user_id"`
2338
Message string `json:"message"`
2439
AutoEscape bool `json:"auto_escape default:false"`
2540
}
2641

27-
type FeishuMessage struct {
42+
type feishuMessage struct {
2843
MsgType string `json:"msg_type"`
2944
Content string `json:"message"`
3045
}
3146

32-
func NotifyFeishu(feishuMessage *FeishuMessage) {
47+
type botNotifier struct{}
48+
49+
var defaultNotifier Notifier = botNotifier{}
50+
var notificationHTTPClient = &http.Client{Timeout: 5 * time.Minute}
51+
52+
func Notify(target NotificationTarget, message string) {
53+
defaultNotifier.Notify(target, message)
54+
}
55+
56+
func (botNotifier) Notify(target NotificationTarget, message string) {
57+
if message == "" {
58+
return
59+
}
60+
61+
go func() {
62+
switch target {
63+
case NotificationTargetQQUser:
64+
notifyQQ(&qqBotMessage{
65+
MessageType: MessageTypePrivate,
66+
UserID: config.Config.QQBotUserID,
67+
Message: message,
68+
})
69+
case NotificationTargetQQPhysicsGroup:
70+
notifyQQ(&qqBotMessage{
71+
MessageType: MessageTypeGroup,
72+
GroupID: config.Config.QQBotPhysicsGroupID,
73+
Message: message,
74+
})
75+
case NotificationTargetQQCodingGroup:
76+
notifyQQ(&qqBotMessage{
77+
MessageType: MessageTypeGroup,
78+
GroupID: config.Config.QQBotCodingGroupID,
79+
Message: message,
80+
})
81+
case NotificationTargetFeishuAdmin:
82+
notifyFeishu(config.Config.FeishuAdminNotifierUrl, &feishuMessage{
83+
MsgType: "text",
84+
Content: message,
85+
})
86+
case NotificationTargetFeishuDivision:
87+
notifyFeishu(config.Config.FeishuDivisionNotifierUrl, &feishuMessage{
88+
MsgType: "text",
89+
Content: message,
90+
})
91+
}
92+
}()
93+
}
94+
95+
func notifyFeishu(url *string, feishuMessage *feishuMessage) {
3396
if feishuMessage == nil || feishuMessage.MsgType == "" {
3497
return
3598
}
36-
if config.Config.FeishuBotUrl == nil {
99+
if url == nil || *url == "" {
37100
return
38101
}
39-
url := *config.Config.FeishuBotUrl
40102

41103
jsonData, err := json.Marshal(feishuMessage)
42104
if err != nil {
@@ -46,7 +108,7 @@ func NotifyFeishu(feishuMessage *FeishuMessage) {
46108

47109
RequestLog(fmt.Sprintf("Request: %s", string(jsonData)), "NotifyFeishu", 0, false)
48110

49-
resp, err := http.Post(url, "application/json", bytes.NewBuffer(jsonData))
111+
resp, err := notificationHTTPClient.Post(*url, "application/json", bytes.NewBuffer(jsonData))
50112
if err != nil {
51113
RequestLog("Error creating request", "NotifyFeishu", 0, false)
52114
return
@@ -62,7 +124,7 @@ func NotifyFeishu(feishuMessage *FeishuMessage) {
62124
}
63125
}
64126

65-
func NotifyQQ(botMessage *BotMessage) {
127+
func notifyQQ(botMessage *qqBotMessage) {
66128
if botMessage == nil {
67129
return
68130
}
@@ -86,7 +148,7 @@ func NotifyQQ(botMessage *BotMessage) {
86148

87149
RequestLog(fmt.Sprintf("Request: %s", string(jsonData)), "NotifyQQ", 0, false)
88150

89-
resp, err := http.Post(url, "application/json", bytes.NewBuffer(jsonData))
151+
resp, err := notificationHTTPClient.Post(url, "application/json", bytes.NewBuffer(jsonData))
90152
if err != nil {
91153
RequestLog("Error creating request", "NotifyQQ", 0, false)
92154
return

0 commit comments

Comments
 (0)