weixin.go 813 B

12345678910111213141516171819202122232425262728293031323334353637
  1. package util
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "jygit.jydev.jianyu360.cn/BaseService/ossService/config"
  6. "log"
  7. "net/http"
  8. )
  9. type WeixinMessage struct {
  10. MsgType string `json:"msgtype"`
  11. Text struct {
  12. Content string `json:"content"`
  13. } `json:"text"`
  14. }
  15. // SendWeixinNotification 发送企业微信机器人告警
  16. func SendWeixinNotification(message string) error {
  17. webhookURL := config.AppConfig.Weixin.WebhookURL
  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(webhookURL, "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. }