Skip to content
This repository has been archived by the owner on Feb 28, 2023. It is now read-only.

Commit

Permalink
feat(notify): Support WebHook notify
Browse files Browse the repository at this point in the history
Signed-off-by: qwqcode <[email protected]>
  • Loading branch information
qwqcode committed May 23, 2022
1 parent 27b6fcd commit ed54720
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 6 deletions.
4 changes: 4 additions & 0 deletions artalk-go.example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 6 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"` // 嘈杂模式 (非回复管理员的评论也发送通知)
}

Expand Down Expand Up @@ -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"`
Expand Down
60 changes: 54 additions & 6 deletions model/notify_launcher/notify_launcher.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package notify_launcher

import (
"bytes"
"encoding/json"
"fmt"
"html"
"net/http"
Expand Down Expand Up @@ -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)
}()
}
}

// 飞书发送
Expand Down Expand Up @@ -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()
}

0 comments on commit ed54720

Please sign in to comment.