dingding.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. package util
  2. import (
  3. "log"
  4. "net/http"
  5. "strings"
  6. "sync"
  7. "xiaoniaokuaiyan.com/xiaoniao/config"
  8. )
  9. /**
  10. * @Author: qz
  11. * @Date: 2021/8/17 14:21
  12. * @Description: 钉钉机器人
  13. */
  14. const hesuanRobootUrl = "https://oapi.dingtalk.com/robot/send?access_token=7d85d053555f96d3ac465253858351fa0262eb0cbefef451dac98b909a418b4a"
  15. func SendOrderMsg(msg string) {
  16. //请求地址模板
  17. webHook := hesuanRobootUrl
  18. mobile := config.IniConf.Section("ding").Key("hs_mobile").Value()
  19. //content := `{"msgtype": "text",
  20. // "text": {"content": "HS ` + msg + `"},"at": {"atMobiles": ["13141018539"]}
  21. //}`
  22. content := `{"msgtype": "text",
  23. "text": {"content": "HS ` + msg + `"},"at": {"atMobiles": ` + mobile + `}
  24. }`
  25. //创建一个请求
  26. req, err := http.NewRequest("POST", webHook, strings.NewReader(content))
  27. if err != nil {
  28. // handle error
  29. }
  30. client := &http.Client{}
  31. //设置请求头
  32. req.Header.Set("Content-Type", "application/json; charset=utf-8")
  33. //发送请求
  34. resp, err := client.Do(req)
  35. //关闭请求
  36. defer resp.Body.Close()
  37. if err != nil {
  38. log.Println(err)
  39. }
  40. }
  41. type Ding interface {
  42. SendMsg(msg string)
  43. }
  44. type normalDing struct {
  45. RobootUrl string
  46. }
  47. var ins_d *normalDing
  48. var one_d sync.Once
  49. func NewNormalDing() *normalDing {
  50. one_d.Do(func() {
  51. ins_d = &normalDing{RobootUrl: ""}
  52. })
  53. return ins_d
  54. }
  55. func (n *normalDing) SendMsg(msg string) {
  56. webHook := n.RobootUrl
  57. content := `{"msgtype": "text",
  58. "text": {"content": "HS ` + msg + `"}
  59. }`
  60. //创建一个请求
  61. req, err := http.NewRequest("POST", webHook, strings.NewReader(content))
  62. if err != nil {
  63. // handle error
  64. }
  65. client := &http.Client{}
  66. //设置请求头
  67. req.Header.Set("Content-Type", "application/json; charset=utf-8")
  68. //发送请求
  69. resp, err := client.Do(req)
  70. //关闭请求
  71. defer resp.Body.Close()
  72. if err != nil {
  73. log.Println(err)
  74. }
  75. }