12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 |
- package util
- import (
- "log"
- "net/http"
- "strings"
- "sync"
- "xiaoniaokuaiyan.com/xiaoniao/config"
- )
- /**
- * @Author: qz
- * @Date: 2021/8/17 14:21
- * @Description: 钉钉机器人
- */
- const hesuanRobootUrl = "https://oapi.dingtalk.com/robot/send?access_token=7d85d053555f96d3ac465253858351fa0262eb0cbefef451dac98b909a418b4a"
- func SendOrderMsg(msg string) {
- //请求地址模板
- webHook := hesuanRobootUrl
- mobile := config.IniConf.Section("ding").Key("hs_mobile").Value()
- //content := `{"msgtype": "text",
- // "text": {"content": "HS ` + msg + `"},"at": {"atMobiles": ["13141018539"]}
- //}`
- content := `{"msgtype": "text",
- "text": {"content": "HS ` + msg + `"},"at": {"atMobiles": ` + mobile + `}
- }`
- //创建一个请求
- req, err := http.NewRequest("POST", webHook, strings.NewReader(content))
- if err != nil {
- // handle error
- }
- client := &http.Client{}
- //设置请求头
- req.Header.Set("Content-Type", "application/json; charset=utf-8")
- //发送请求
- resp, err := client.Do(req)
- //关闭请求
- defer resp.Body.Close()
- if err != nil {
- log.Println(err)
- }
- }
- type Ding interface {
- SendMsg(msg string)
- }
- type normalDing struct {
- RobootUrl string
- }
- var ins_d *normalDing
- var one_d sync.Once
- func NewNormalDing() *normalDing {
- one_d.Do(func() {
- ins_d = &normalDing{RobootUrl: ""}
- })
- return ins_d
- }
- func (n *normalDing) SendMsg(msg string) {
- webHook := n.RobootUrl
- content := `{"msgtype": "text",
- "text": {"content": "HS ` + msg + `"}
- }`
- //创建一个请求
- req, err := http.NewRequest("POST", webHook, strings.NewReader(content))
- if err != nil {
- // handle error
- }
- client := &http.Client{}
- //设置请求头
- req.Header.Set("Content-Type", "application/json; charset=utf-8")
- //发送请求
- resp, err := client.Do(req)
- //关闭请求
- defer resp.Body.Close()
- if err != nil {
- log.Println(err)
- }
- }
|