From 6216af500f427eda36e3b1f757dac2743476b5ab Mon Sep 17 00:00:00 2001 From: kercylan98 Date: Thu, 27 Apr 2023 18:19:12 +0800 Subject: [PATCH] =?UTF-8?q?=E9=A3=9E=E4=B9=A6=E9=80=9A=E7=9F=A5=E6=8E=A8?= =?UTF-8?q?=E9=80=81=E5=9F=BA=E6=9C=AC=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- go.mod | 1 + go.sum | 5 +++++ notify/notifies/feishu.go | 43 +++++++++++++++++++++++++++++++++++++++ notify/notify.go | 2 +- notify/readers/feishu.go | 40 ++++++++++++++++++++++++++++++++++++ notify/sender.go | 2 +- 6 files changed, 91 insertions(+), 2 deletions(-) create mode 100644 notify/notifies/feishu.go create mode 100644 notify/readers/feishu.go diff --git a/go.mod b/go.mod index 980476d..1b7aba9 100644 --- a/go.mod +++ b/go.mod @@ -22,6 +22,7 @@ require ( github.com/go-playground/locales v0.14.1 // indirect github.com/go-playground/universal-translator v0.18.1 // indirect github.com/go-playground/validator/v10 v10.11.2 // indirect + github.com/go-resty/resty/v2 v2.7.0 // indirect github.com/goccy/go-json v0.10.0 // indirect github.com/golang/protobuf v1.5.2 // indirect github.com/gopherjs/gopherjs v1.17.2 // indirect diff --git a/go.sum b/go.sum index b746eb9..a9afdc8 100644 --- a/go.sum +++ b/go.sum @@ -31,6 +31,8 @@ github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJn github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY= github.com/go-playground/validator/v10 v10.11.2 h1:q3SHpufmypg+erIExEKUmsgmhDTyhcJ38oeKGACXohU= github.com/go-playground/validator/v10 v10.11.2/go.mod h1:NieE624vt4SCTJtD87arVLvdmjPAeV8BQlHtMnw9D7s= +github.com/go-resty/resty/v2 v2.7.0 h1:me+K9p3uhSmXtrBZ4k9jcEAfJmuC8IivWHwaLZwPrFY= +github.com/go-resty/resty/v2 v2.7.0/go.mod h1:9PWDzw47qPphMRFfhsyk0NnSgvluHcljSMVIq3w7q0I= github.com/goccy/go-json v0.10.0 h1:mXKd9Qw4NuzShiRlOXKews24ufknHO7gx30lsDyokKA= github.com/goccy/go-json v0.10.0/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I= github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q= @@ -191,6 +193,7 @@ golang.org/x/net v0.0.0-20200625001655-4c5254603344/go.mod h1:/O7V0waA8r7cgGh81R golang.org/x/net v0.0.0-20200707034311-ab3426394381/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA= golang.org/x/net v0.0.0-20201010224723-4f7140c49acb/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM= +golang.org/x/net v0.0.0-20211029224645-99673261e6eb/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= golang.org/x/net v0.8.0 h1:Zrh2ngAOFYneWTAIAPethzeaQLuHwhuBkuV6ZiRnUaQ= golang.org/x/net v0.8.0/go.mod h1:QVkue5JL9kW//ek3r6jTKnTFis1tRmNAW2P1shuFdJc= golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= @@ -208,6 +211,7 @@ golang.org/x/sys v0.0.0-20200808120158-1030fc2bf1d9/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20211204120058-94396e421777/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220704084225-05e143d24a9e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= @@ -217,6 +221,7 @@ golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.8.0 h1:57P1ETyNKtuIjB4SRd15iJxuhj8Gc416Y78H3qgMh68= golang.org/x/text v0.8.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= diff --git a/notify/notifies/feishu.go b/notify/notifies/feishu.go new file mode 100644 index 0000000..8cafa25 --- /dev/null +++ b/notify/notifies/feishu.go @@ -0,0 +1,43 @@ +package notifies + +import "encoding/json" + +const ( + FeiShuMsgTypeText = "text" // 文本 + FeiShuMsgTypeRichText = "post" // 富文本 + FeiShuMsgTypeImage = "image" // 图片 + FeiShuMsgTypeInteractive = "interactive" // 消息卡片 + FeiShuMsgTypeShareChat = "share_chat" // 分享群名片 + FeiShuMsgTypeShareUser = "share_user" // 分享个人名片 + FeiShuMsgTypeAudio = "audio" // 音频 + FeiShuMsgTypeMedia = "media" // 视频 + FeiShuMsgTypeFile = "file" // 文件 + FeiShuMsgTypeSticker = "sticker" // 表情包 + +) + +func NewFeiShu(msgType, receiveId, content string) *FeiShu { + return &FeiShu{ + ReceiveId: receiveId, + Content: content, + MsgType: msgType, + } +} + +type FeiShu struct { + ReceiveId string `json:"receive_id"` + Content string `json:"content"` + MsgType string `json:"msg_type"` +} + +func (slf *FeiShu) GetTitle() string { + return "" +} + +func (slf *FeiShu) GetContent() (string, error) { + data, err := json.Marshal(slf) + if err != nil { + return "", err + } + return string(data), nil +} diff --git a/notify/notify.go b/notify/notify.go index 6f450e0..e28afa1 100644 --- a/notify/notify.go +++ b/notify/notify.go @@ -5,5 +5,5 @@ type Notify interface { // GetTitle 获取通知标题 GetTitle() string // GetContent 获取通知内容 - GetContent() string + GetContent() (string, error) } diff --git a/notify/readers/feishu.go b/notify/readers/feishu.go new file mode 100644 index 0000000..53e11fe --- /dev/null +++ b/notify/readers/feishu.go @@ -0,0 +1,40 @@ +package readers + +import ( + "fmt" + "github.com/go-resty/resty/v2" + "minotaur/notify" + "net/http" +) + +func NewFeiShu(webhook, receiveId string) *FeiShu { + return &FeiShu{ + client: resty.New(), + webhook: webhook, + receiveId: receiveId, + } +} + +type FeiShu struct { + client *resty.Client + webhook string + receiveId string +} + +func (slf *FeiShu) Push(notify notify.Notify) error { + content, err := notify.GetContent() + if err != nil { + return err + } + resp, err := slf.client.R(). + SetHeader("Content-Type", "application/json"). + SetBody(content). + Post(slf.webhook) + if err != nil { + return err + } + if resp.StatusCode() != http.StatusOK { + return fmt.Errorf("FeiShu notify reader push failed, err: %s", resp.String()) + } + return err +} diff --git a/notify/sender.go b/notify/sender.go index ce7ad59..f78a678 100644 --- a/notify/sender.go +++ b/notify/sender.go @@ -1,5 +1,5 @@ package notify type Sender interface { - Push(notify Notify) + Push(notify Notify) error }