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 { msg := WeixinMessage{MsgType: "text"} msg.Text.Content = message data, err := json.Marshal(msg) if err != nil { return err } resp, err := http.Post(g.Config().MustGet(gctx.New(), "weixin.webhook_url").String(), "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 }