diff --git a/server/plugin/notify/README.MD b/server/plugin/notify/README.MD index 174110a8d59182d1880a457196a4e41d51e2a070..b0b5cc19bd5a6d390b3ac02aec6a0a7031b77a05 100644 --- a/server/plugin/notify/README.MD +++ b/server/plugin/notify/README.MD @@ -2,15 +2,12 @@ 本插件用于向钉钉群推送消息 -### 使用步骤 - -#### 1. 使用说明 +### 1. 使用场景 - 当服务运行异常时,可以向钉钉推送异常信息,便于及时发现解决问题 - 推送一些关键业务的运行日志等 - -#### 2. 配置说明 +### 2. 配置说明 钉钉 token 等相关信息的获取,请参考 [钉钉官网](https://developers.dingtalk.com/document/robots/custom-robot-access?spm=ding_open_doc.document.0.0.7f8710afbfzduV#topic-2026027) @@ -23,6 +20,19 @@ var GlobalConfig_ = &config.DingDing{ Secret: "xxx", } ``` +### 3. 使用说明 + +在代码中调用 `SendTextMessage` 方法即可 +```go +func NotifyController(c *gin.Context) { + if err := service.ServiceGroupApp.SendTextMessage("test"); err != nil { + global.GVA_LOG.Error("发送失败!", zap.Any("err", err)) + response.FailWithMessage("发送失败", c) + } else { + response.OkWithData("发送成功", c) + } +} +``` ### 方法API diff --git a/server/plugin/notify/api/api.go b/server/plugin/notify/api/api.go index 3320575f3302aad320ba978293c3916fb4119b09..4874a814377aa8735d5b9dd7052e492de429c548 100644 --- a/server/plugin/notify/api/api.go +++ b/server/plugin/notify/api/api.go @@ -12,7 +12,7 @@ type Api struct { } func (s *Api) NotifyController(c *gin.Context) { - if err := service.ServiceGroupApp.Send(); err != nil { + if err := service.ServiceGroupApp.SendTextMessage("test"); err != nil { global.GVA_LOG.Error("发送失败!", zap.Any("err", err)) response.FailWithMessage("发送失败", c) } else { diff --git a/server/plugin/notify/service/notify.go b/server/plugin/notify/service/notify.go index ab992e96bd97c3ec876a54e45a7fda63a3c895c3..7011d1adecd811b012f29d270ce75622189bfd04 100644 --- a/server/plugin/notify/service/notify.go +++ b/server/plugin/notify/service/notify.go @@ -17,7 +17,12 @@ import ( type NotifyService struct { } -func SendTextMessage(content string) error { +//@author: [Espoir](https://github.com/nightsimon) +//@function: NotifyController +//@description: 钉钉通知测试 +//@return: err error + +func (e *NotifyService) SendTextMessage(content string) error { msg := map[string]interface{}{ "msgtype": "text", "text": map[string]string{ @@ -99,16 +104,3 @@ func sign(t int64, secret string) string { data := hmac256.Sum(nil) return base64.StdEncoding.EncodeToString(data) } - -//@author: [Espoir](https://github.com/nightsimon) -//@function: NotifyController -//@description: 钉钉通知测试 -//@return: err error - -func (e *NotifyService) Send() (err error) { - err = SendTextMessage("test") - if err != nil { - return err - } - return err -}