custom.go 1.1 KB

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