-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdingtalk.go
executable file
·125 lines (104 loc) · 2.96 KB
/
dingtalk.go
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"regexp"
)
// 获取一言
func getHitokoto(url string) map[string]interface{} {
response, error := http.Get(url)
if error != nil {
panic(error)
}
defer response.Body.Close()
return json2Map(response.Body)
}
// 从命令行获取参数
func getArgument(name string, defaultValue string) string {
args := os.Args[1:]
length := len(args)
for index, value := range args {
if value == name {
if index+1 <= length {
return args[index+1]
}
}
}
return defaultValue
}
// 获取参数并设置默认值为空字符串
func getArgDefaultEmpty(name string) string {
return getArgument(name, "")
}
// 将json数据从字节数组转换成map
func json2Map(jsonString io.Reader) map[string]interface{} {
body, err := ioutil.ReadAll(jsonString)
if err != nil {
panic(err)
}
// 解析 json
result := make(map[string]interface{})
err = json.Unmarshal(body, &result)
if err != nil {
panic(err)
}
return result
}
// 发送json请求,向钉钉推数据
// 已知 url 和 json 都是合法的,忽略其它异常
func postWithJson(robotUrl string, data []byte) map[string]interface{} {
request, _ := http.NewRequest("POST", robotUrl, bytes.NewBuffer(data))
request.Header.Set("Content-Type", "application/json")
client := &http.Client{}
response, err := client.Do(request)
if err != nil {
panic(err)
}
defer response.Body.Close()
return json2Map(response.Body)
}
// 构造推送向钉钉的数据
// 数据结构是清晰的,数据变量也是已知的,所以忽略错误
func makeData(source string, creator string, sentence string) []byte {
jsonData, _ := json.Marshal(map[string]interface{}{
"msgtype": "markdown",
"markdown": map[string]string{
"title": "一言",
"text": "**" + sentence + "** \n > " + creator + "「" + source + "」",
},
})
return jsonData
}
func main() {
// 构造正则,验证输入参数是否是合法的 url
// 正则固定,忽略异常
regs, _ := regexp.Compile("^https?://")
apiUrl := getArgDefaultEmpty("--api-url")
if !regs.MatchString(apiUrl) {
fmt.Println("--api-url is required")
os.Exit(127)
}
robotUrl := getArgDefaultEmpty("--robot-url")
if !regs.MatchString(robotUrl) {
fmt.Println("--robot-url must be url")
os.Exit(125)
}
// 获取数据和推送到钉钉
hitokoto := getHitokoto(apiUrl)
response := postWithJson(robotUrl, makeData(
hitokoto["from"].(string),
hitokoto["creator"].(string),
hitokoto["hitokoto"].(string),
))
errCode := int(response["errcode"].(float64))
if errCode != 0 {
fmt.Println(response["errmsg"])
os.Exit(errCode)
}
fmt.Println("success")
}