custom.go 1.0 KB

1234567891011121314151617181920212223242526272829303132333435
  1. package channel
  2. import (
  3. "bytes"
  4. "errors"
  5. "message-pusher/common"
  6. "message-pusher/model"
  7. "net/http"
  8. "strings"
  9. )
  10. func SendCustomMessage(message *model.Message, user *model.User, channel_ *model.Channel) error {
  11. url := channel_.URL
  12. if strings.HasPrefix(url, "http:") {
  13. return errors.New("自定义通道必须使用 HTTPS 协议")
  14. }
  15. if strings.HasPrefix(url, common.ServerAddress) {
  16. return errors.New("自定义通道不能使用本服务地址")
  17. }
  18. template := channel_.Other
  19. template = strings.Replace(template, "$url", message.URL, -1)
  20. template = strings.Replace(template, "$to", message.To, -1)
  21. template = strings.Replace(template, "$title", message.Title, -1)
  22. template = strings.Replace(template, "$description", message.Description, -1)
  23. template = strings.Replace(template, "$content", message.Content, -1)
  24. reqBody := []byte(template)
  25. resp, err := http.Post(url, "application/json", bytes.NewReader(reqBody))
  26. if err != nil {
  27. return err
  28. }
  29. if resp.StatusCode != 200 {
  30. return errors.New(resp.Status)
  31. }
  32. return nil
  33. }