diff --git a/artalk-go.example.yml b/artalk-go.example.yml index 536b19d..6ac0331 100644 --- a/artalk-go.example.yml +++ b/artalk-go.example.yml @@ -154,5 +154,9 @@ admin_notify: lark: enabled: false webhook_url: "" + # WebHook + webhook: + enabled: false + url: "" # 另支持 钉钉、Slack、LINE 等多种推送方式 # 详情参考:https://artalk.js.org/guide/backend/notify.html diff --git a/config/config.go b/config/config.go index 4e11161..1ddfc3f 100644 --- a/config/config.go +++ b/config/config.go @@ -227,6 +227,7 @@ type AdminNotifyConf struct { Bark NotifyBarkConf `mapstructure:"bark" json:"bark"` // bark Slack NotifySlackConf `mapstructure:"slack" json:"slack"` // slack LINE NotifyLINEConf `mapstructure:"line" json:"line"` // LINE + WebHook NotifyWebHookConf `mapstructure:"webhook" json:"webhook"` // WebHook NoiseMode bool `mapstructure:"noise_mode" json:"noise_mode"` // 嘈杂模式 (非回复管理员的评论也发送通知) } @@ -270,6 +271,11 @@ type NotifyLINEConf struct { Receivers []string `mapstructure:"receivers" json:"receivers"` } +type NotifyWebHookConf struct { + Enabled bool `mapstructure:"enabled" json:"enabled"` + URL string `mapstructure:"url" json:"url"` +} + // 使用转换 @link https://transform.tools/json-to-go type FrontendConf struct { Placeholder *string `mapstructure:"placeholder" json:"placeholder,omitempty"` diff --git a/model/notify_launcher/notify_launcher.go b/model/notify_launcher/notify_launcher.go index 53b183b..03742b0 100644 --- a/model/notify_launcher/notify_launcher.go +++ b/model/notify_launcher/notify_launcher.go @@ -1,6 +1,8 @@ package notify_launcher import ( + "bytes" + "encoding/json" "fmt" "html" "net/http" @@ -155,14 +157,25 @@ func AdminNotify(comment *model.Comment, pComment *model.Comment) { }() // 飞书 - go func() { - SendLark(subject, body) - }() + if config.Instance.AdminNotify.Lark.Enabled { + go func() { + SendLark(subject, body) + }() + } // Bark - go func() { - SendBark(subject, body) - }() + if config.Instance.AdminNotify.Bark.Enabled { + go func() { + SendBark(subject, body) + }() + } + + // WebHook + if config.Instance.AdminNotify.WebHook.Enabled { + go func() { + SendWebHook(subject, body, comment, pComment) + }() + } } // 飞书发送 @@ -205,3 +218,38 @@ func SendBark(title string, msg string) { defer result.Body.Close() } + +type NotifyWebHookReqBody struct { + NotifySubject string `json:"notify_subject"` + NotifyBody string `json:"notify_body"` + Comment interface{} `json:"comment"` + ParentComment interface{} `json:"parent_comment"` +} + +// WebHook 发送 +func SendWebHook(subject string, body string, comment *model.Comment, pComment *model.Comment) { + webhookConf := config.Instance.AdminNotify.WebHook + if !webhookConf.Enabled { + return + } + + reqData := NotifyWebHookReqBody{ + NotifySubject: subject, + NotifyBody: body, + Comment: comment.ToCooked(), + } + if !pComment.IsEmpty() { + reqData.ParentComment = pComment.ToCooked() + } else { + reqData.ParentComment = nil + } + + jsonByte, _ := json.Marshal(reqData) + result, err := http.Post(webhookConf.URL, "application/json", bytes.NewReader(jsonByte)) + if err != nil { + logrus.Error("[WebHook 推送]", " 消息发送失败:", err) + return + } + + defer result.Body.Close() +}