@@ -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+
1227type BotMessageType string
1328
1429const (
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