/
wecom.go
96 lines (82 loc) · 2.73 KB
/
wecom.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
package webhook
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strings"
"time"
)
type WeComWebhookResponse struct {
ErrorCode int `json:"errcode"`
ErrorMessage string `json:"errmsg"`
}
type WeComWebhookMarkdown struct {
Content string `json:"content"`
}
type WeComWebhook struct {
MessageType string `json:"msgtype"`
Markdown WeComWebhookMarkdown `json:"markdown"`
}
func init() {
register("bb.plugin.webhook.wecom", &WeComReceiver{})
}
type WeComReceiver struct {
}
func (receiver *WeComReceiver) post(context WebhookContext) error {
metaStrList := []string{}
for _, meta := range context.MetaList {
metaStrList = append(metaStrList, fmt.Sprintf("%s: <font color=\"comment\">%s</font>", meta.Name, meta.Value))
}
metaStrList = append(metaStrList, fmt.Sprintf("By: <font color=\"comment\">%s (%s)</font>", context.CreatorName, context.CreatorEmail))
metaStrList = append(metaStrList, fmt.Sprintf("At: <font color=\"comment\">%s</font>", time.Unix(context.CreatedTs, 0).Format(timeFormat)))
status := ""
if context.Level == WebhookSuccess {
status = "<font color=\"green\">Success</font> "
} else if context.Level == WebhookWarn {
status = "<font color=\"yellow\">Warn</font> "
} else if context.Level == WebhookError {
status = "<font color=\"red\">Error</font> "
}
content := fmt.Sprintf("# %s%s\n\n%s\n[View in Bytebase](%s)", status, context.Title, strings.Join(metaStrList, "\n"), context.Link)
if context.Description != "" {
content = fmt.Sprintf("# %s%s\n> %s\n\n%s\n[View in Bytebase](%s)", status, context.Title, context.Description, strings.Join(metaStrList, "\n"), context.Link)
}
post := WeComWebhook{
MessageType: "markdown",
Markdown: WeComWebhookMarkdown{
Content: content,
},
}
body, err := json.Marshal(post)
if err != nil {
return fmt.Errorf("failed to marshal webhook POST request: %v", context.URL)
}
req, err := http.NewRequest("POST",
context.URL, bytes.NewBuffer(body))
if err != nil {
return fmt.Errorf("failed to construct webhook POST request %v (%w)", context.URL, err)
}
req.Header.Set("Content-Type", "application/json")
client := &http.Client{
Timeout: timeout,
}
resp, err := client.Do(req)
if err != nil {
return fmt.Errorf("failed to POST webhook %v (%w)", context.URL, err)
}
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("failed to read POST webhook response %v (%w)", context.URL, err)
}
defer resp.Body.Close()
webhookResponse := &WeComWebhookResponse{}
if err := json.Unmarshal(b, webhookResponse); err != nil {
return fmt.Errorf("malformatted webhook response %v (%w)", context.URL, err)
}
if webhookResponse.ErrorCode != 0 {
return fmt.Errorf("%s", webhookResponse.ErrorMessage)
}
return nil
}