-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.go
More file actions
39 lines (37 loc) · 1.02 KB
/
util.go
File metadata and controls
39 lines (37 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package dingding_go
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
)
func SendMessage(message interface{}, url string) error {
if message == nil {
return fmt.Errorf("消息为空")
}
body, err := json.Marshal(message)
if err != nil {
return fmt.Errorf("序列化消息失败 err:%s message:%+v", err, message)
}
res, err := http.Post(url, "application/json", bytes.NewBuffer(body))
if err != nil {
fmt.Println("钉钉消息发送失败 ", err)
return fmt.Errorf("钉钉消息发送失败 err:%s", err)
}
if res != nil && res.Body != nil {
defer res.Body.Close()
content, err := ioutil.ReadAll(res.Body)
if err != nil {
return fmt.Errorf("读取钉钉响应失败 err:%s", err)
}
if res.StatusCode == http.StatusFound {
return fmt.Errorf("机器人可能已被限流 请注意发送频率不要过高 会自行恢复")
}
if res.StatusCode != http.StatusOK {
return fmt.Errorf("钉钉消息发送失败 err:%s", content)
}
return nil
}
return fmt.Errorf("钉钉响应为空")
}