monitor.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. package monitor
  2. import (
  3. "context"
  4. "crypto/md5"
  5. "encoding/hex"
  6. "encoding/json"
  7. "net/http"
  8. "net/url"
  9. "strconv"
  10. "strings"
  11. "time"
  12. "go-common/library/log"
  13. )
  14. type wxParams struct {
  15. Username string `json:"username"`
  16. Content string `json:"content"`
  17. Token string `json:"token"`
  18. Timestamp int64 `json:"timestamp"`
  19. Sign string `json:"signature"`
  20. }
  21. type resp struct {
  22. Status int64 `json:"status"`
  23. Msg string `json:"msg"`
  24. }
  25. // Send send message to phone
  26. func (d *Dao) Send(c context.Context, users, msg, token, secret string) (err error) {
  27. params := url.Values{}
  28. params.Set("username", users)
  29. params.Set("content", msg)
  30. params.Set("token", token)
  31. params.Set("timestamp", strconv.FormatInt(time.Now().Unix(), 10))
  32. mh := md5.Sum([]byte(params.Encode() + secret))
  33. params.Set("signature", hex.EncodeToString(mh[:]))
  34. p := &wxParams{
  35. Username: params.Get("username"),
  36. Content: params.Get("content"),
  37. Token: params.Get("token"),
  38. Sign: params.Get("signature"),
  39. }
  40. p.Timestamp, _ = strconv.ParseInt(params.Get("timestamp"), 10, 64)
  41. bs, _ := json.Marshal(p)
  42. payload := strings.NewReader(string(bs))
  43. req, _ := http.NewRequest("POST", "http://bap.bilibili.co/api/v1/message/add", payload)
  44. req.Header.Add("content-type", "application/json; charset=utf-8")
  45. v := &resp{}
  46. if err = d.client.Do(context.TODO(), req, v); err != nil {
  47. log.Error("s.client.Do error(%v)", err)
  48. return
  49. }
  50. return
  51. }