-
Notifications
You must be signed in to change notification settings - Fork 63
/
ding.go
76 lines (65 loc) · 1.59 KB
/
ding.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
package notice
import (
"encoding/json"
"fmt"
"io"
"net/http"
"strings"
"github.com/li4n0/revsuit/internal/record"
"github.com/pkg/errors"
)
var _ Bot = (*DingTalk)(nil)
type DingTalk struct {
URL string
}
type dingAt struct {
AtMobiles []string `json:"atMobiles"`
IsAtAll bool `json:"isAtAll"`
}
type dingMarkdown struct {
Title string `json:"title"`
Text string `json:"text"`
}
type dingPayload struct {
MsgType string `json:"msgtype"`
Markdown dingMarkdown `json:"markdown"`
At []dingAt `json:"at"`
}
func (d *DingTalk) name() string {
return "DingTalk"
}
func (d *DingTalk) buildPayload(r record.Record) string {
payload := dingPayload{
MsgType: "markdown",
Markdown: dingMarkdown{
Title: "New Connection",
Text: "**<font color='#e96900' face='Fira Code' size='3'>New Connection</font>**\n" +
formatRecordField(r, "> **<font color='#e96900' face='Fira Code'>%s: </font>**<font color='#e96900' face='Fira Code'>%v</font>\n"),
},
At: []dingAt{
{
IsAtAll: true,
},
},
}
p, err := json.Marshal(&payload)
if err != nil {
return ""
}
return string(p)
}
func (d *DingTalk) notice(r record.Record) error {
resp, err := http.Post(d.URL, "application/json", strings.NewReader(d.buildPayload(r)))
if err != nil {
return errors.Wrap(err, "HTTP request")
}
defer resp.Body.Close()
if resp.StatusCode/100 != 2 {
data, err := io.ReadAll(resp.Body)
if err != nil {
return errors.Wrap(err, "read HTTP response body")
}
return fmt.Errorf("non-success response status code %d with body: %s", resp.StatusCode, data)
}
return nil
}