-
Notifications
You must be signed in to change notification settings - Fork 34
/
notify_message.go
167 lines (141 loc) · 3.36 KB
/
notify_message.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package lark
import (
"context"
"encoding/json"
"fmt"
"os"
"strings"
"time"
"github.com/mitchellh/mapstructure"
"github.com/pkg/errors"
"github.com/machinefi/w3bstream/pkg/depends/base/consts"
"github.com/machinefi/w3bstream/pkg/types"
)
type Content struct {
Post map[string]ContentBody `json:"post"`
}
type ContentBody struct {
Title string `json:"title"`
ContentElements [][]map[string]interface{} `json:"content"`
}
func TagElementToMap(e TagElement) (map[string]interface{}, error) {
ret := map[string]interface{}{"tag": e.Tag()}
err := mapstructure.Decode(e, &ret)
if err != nil {
return nil, err
}
return ret, nil
}
func TagElementsToMap(elem ...TagElement) ([][]map[string]interface{}, error) {
ms := make([][]map[string]interface{}, 0, len(elem))
for _, e := range elem {
m, err := TagElementToMap(e)
if err != nil {
return nil, err
}
ms = append(ms, []map[string]interface{}{m})
}
return ms, nil
}
type TagElement interface {
Tag() string
}
type ElementImg struct {
ImageKey string `mapstructure:"image_key"`
W int `mapstructure:"width"`
H int `mapstructure:"height"`
}
func (v *ElementImg) Tag() string { return "img" }
type ElementText struct {
Text string `mapstructure:"text"`
}
func (v *ElementText) Tag() string { return "text" }
type ElementHref struct {
Text string `mapstructure:"text"`
Ref string `mapstructure:"href"`
}
func (v *ElementHref) Tag() string { return "a" }
type ElementPin struct {
UserID string `mapstructure:"user_id"`
}
func (v *ElementPin) Tag() string { return "at" }
func Build(ctx context.Context, title, level, content string) ([]byte, error) {
notifier, _ := types.RobotNotifierConfigFromContext(ctx)
if notifier == nil {
return nil, nil
}
ts := time.Now().UTC().Unix()
sign := ""
if notifier.SignFn != nil {
signature, err := notifier.SignFn(ts)
if err != nil {
return nil, errors.Wrap(err, "signature")
}
sign = signature
}
ms, err := TagElementsToMap(
&ElementText{
Text: fmt.Sprintf("env: %s", notifier.Env),
},
&ElementText{
Text: fmt.Sprintf("project: %s", os.Getenv(consts.EnvProjectName)),
},
&ElementText{
Text: fmt.Sprintf("version: %s", os.Getenv(consts.EnvProjectVersion)),
},
&ElementText{
Text: content,
},
)
if err != nil {
return nil, errors.Wrap(err, "mapstructure")
}
msg := &struct {
Timestamp int64 `json:"timestamp,omitempty"`
Sign string `json:"sign,omitempty"`
MessageType string `json:"msg_type"`
Content Content `json:"content"`
}{
Timestamp: ts,
Sign: sign,
MessageType: "post",
Content: Content{
Post: map[string]ContentBody{
"en_ch": {
Title: fmt.Sprintf("%s [%s]", strings.ToUpper(level), title),
ContentElements: ms,
},
},
},
}
return json.Marshal(msg)
}
func ResponseHook(body []byte) error {
if len(body) == 0 {
return nil
}
v := &struct {
Code int `json:"code"`
Msg string `json:"msg"`
}{}
err := json.Unmarshal(body, v)
if err != nil {
return err
}
switch v.Code {
case 19021:
return ErrInvalidSignature
case 9499:
return ErrInvalidMessage
default:
if v.Code == 0 {
return nil
} else {
return errors.Errorf("code: %d, msg: %s", v.Code, v.Msg)
}
}
}
var (
ErrInvalidMessage = errors.New("invalid message")
ErrInvalidSignature = errors.New("invalid signature")
)