weixin.go 822 B

12345678910111213141516171819202122232425262728293031323334353637
  1. package util
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "github.com/gogf/gf/v2/frame/g"
  6. "github.com/gogf/gf/v2/os/gctx"
  7. "log"
  8. "net/http"
  9. )
  10. type WeixinMessage struct {
  11. MsgType string `json:"msgtype"`
  12. Text struct {
  13. Content string `json:"content"`
  14. } `json:"text"`
  15. }
  16. // SendWeixinNotification 发送企业微信机器人告警
  17. func SendWeixinNotification(message string) error {
  18. msg := WeixinMessage{MsgType: "text"}
  19. msg.Text.Content = message
  20. data, err := json.Marshal(msg)
  21. if err != nil {
  22. return err
  23. }
  24. resp, err := http.Post(g.Config().MustGet(gctx.New(), "weixin.webhook_url").String(), "application/json", bytes.NewReader(data))
  25. if err != nil {
  26. log.Println("SendWeixinNotification error", err)
  27. return err
  28. }
  29. defer resp.Body.Close()
  30. if resp.StatusCode != http.StatusOK {
  31. return err
  32. }
  33. return nil
  34. }