1234567891011121314151617181920212223242526272829303132333435363738394041 |
- package util
- import (
- "bytes"
- "encoding/json"
- "github.com/gogf/gf/v2/frame/g"
- "github.com/gogf/gf/v2/os/gctx"
- "log"
- "net/http"
- )
- type WeixinMessage struct {
- MsgType string `json:"msgtype"`
- Text struct {
- Content string `json:"content"`
- } `json:"text"`
- }
- // SendWeixinNotification 发送企业微信机器人告警
- func SendWeixinNotification(message string) error {
- webhook_url := g.Config().MustGet(gctx.New(), "weixin.webhook_url").String()
- if webhook_url == "" {
- return nil
- }
- msg := WeixinMessage{MsgType: "text"}
- msg.Text.Content = message
- data, err := json.Marshal(msg)
- if err != nil {
- return err
- }
- resp, err := http.Post(webhook_url, "application/json", bytes.NewReader(data))
- if err != nil {
- log.Println("SendWeixinNotification error", err)
- return err
- }
- defer resp.Body.Close()
- if resp.StatusCode != http.StatusOK {
- return err
- }
- return nil
- }
|