/
webhook_send.go
238 lines (211 loc) · 5.75 KB
/
webhook_send.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
package wecom
import (
"context"
"encoding/json"
"io/fs"
"net/http"
"time"
"github.com/pkg/errors"
"github.com/wenerme/go-req"
)
type WebhookSendRequest struct {
Key string
Content MessageContent
Context context.Context
Request req.Request
}
type WebhookSendResponse struct{}
// WebhookSend request webhook to send robot messages
func WebhookSend(r *WebhookSendRequest) (err error) {
b := &SendPayload{}
err = b.SetContent(r.Content)
if err != nil {
return err
}
er := GenericResponse{}
err = req.Request{
BaseURL: DefaultAPI,
URL: "/cgi-bin/webhook/send",
Method: http.MethodPost,
Query: map[string]string{
"key": r.Key,
},
Context: r.Context,
Body: b,
Options: []interface{}{req.JSONEncode, req.JSONDecode},
}.With(r.Request).Fetch(&er)
if err == nil {
err = er.AsError()
}
return
}
type MessageContent interface {
MessageType() string
}
const (
MessageTypeText = "text"
MessageTypeMarkdown = "markdown"
MessageTypeImage = "image"
MessageTypeNews = "news"
MessageTypeFile = "file"
MessageTypeTemplateCard = "template_card"
MentionAll = "@all"
)
type SendImageContent struct {
Base64 string `json:"base64,omitempty"`
MD5 string `json:"md5,omitempty"`
}
func (SendImageContent) MessageType() string {
return MessageTypeImage
}
type SendTextContent struct {
Content string `json:"content,omitempty"` // <= 2048byte
MentionedList []string `json:"mentioned_list,omitempty"` // userid or MentionAll
MentionedMobileList []string `json:"mentioned_mobile_list,omitempty"` // mobile or MentionAll
}
func (SendTextContent) MessageType() string {
return MessageTypeText
}
type SendNewsContent struct {
Articles []SendNewsArticle `json:"articles,omitempty"`
}
type SendNewsArticle struct {
Title string `json:"title,omitempty"`
Description string `json:"description,omitempty"`
URL string `json:"url,omitempty"`
PictureURL string `json:"picurl,omitempty"`
}
func (SendNewsContent) MessageType() string {
return MessageTypeNews
}
type SendMarkdownContent struct {
Content string `json:"content,omitempty"`
}
func (SendMarkdownContent) MessageType() string {
return MessageTypeMarkdown
}
type SendFileContent struct {
MediaID string `json:"media_id,omitempty"`
}
func (SendFileContent) MessageType() string {
return MessageTypeFile
}
type SendTemplateCardContent struct {
CardType string `json:"card_type"`
Source struct {
IconURL string `json:"icon_url"`
Description string `json:"desc"`
} `json:"source"`
MainTitle struct {
Title string `json:"title"`
Description string `json:"desc"`
} `json:"main_title"`
EmphasisContent struct {
Title string `json:"title"`
Description string `json:"desc"`
} `json:"emphasis_content"`
SubTitleText string `json:"sub_title_text"`
HorizontalContentList []struct {
KeyName string `json:"keyname"`
Value string `json:"value"`
Type int `json:"type,omitempty"`
URL string `json:"url,omitempty"`
MediaID string `json:"media_id,omitempty"`
} `json:"horizontal_content_list"`
JumpList []struct {
Type int `json:"type"`
URL string `json:"url,omitempty"`
Title string `json:"title"`
AppID string `json:"appid,omitempty"`
PagePath string `json:"pagepath,omitempty"`
} `json:"jump_list"`
CardAction struct {
Type int `json:"type"`
URL string `json:"url"`
AppID string `json:"appid"`
PagePath string `json:"pagepath"`
} `json:"card_action"`
}
func (SendTemplateCardContent) MessageType() string {
return MessageTypeTemplateCard
}
type SendPayload struct {
MessageType string `json:"msgtype"`
Text *SendTextContent `json:"text,omitempty"`
Markdown *SendMarkdownContent `json:"markdown,omitempty"`
Image *SendImageContent `json:"image,omitempty"`
News *SendNewsContent `json:"news,omitempty"`
File *SendFileContent `json:"file,omitempty"`
TemplateCard *SendTemplateCardContent `json:"template_card,omitempty"`
}
func (p *SendPayload) SetContent(c MessageContent) (err error) {
if c == nil {
err = errors.Errorf("nil message content")
return
}
p.MessageType = c.MessageType()
switch v := c.(type) {
case SendTextContent:
p.Text = &v
case *SendTextContent:
p.Text = v
case SendMarkdownContent:
p.Markdown = &v
case *SendMarkdownContent:
p.Markdown = v
case SendImageContent:
p.Image = &v
case *SendImageContent:
p.Image = v
case SendNewsContent:
p.News = &v
case *SendNewsContent:
p.News = v
case SendFileContent:
p.File = &v
case *SendFileContent:
p.File = v
case SendTemplateCardContent:
p.TemplateCard = &v
case *SendTemplateCardContent:
p.TemplateCard = v
default:
err = errors.Errorf("unsupported message content %T", v)
}
return
}
type WebhookUploadMediaResponse struct {
Type string `json:"type,omitempty"` // file
MediaID string `json:"media_id,omitempty"`
CreatedAt json.Number `json:"created_at,omitempty"`
}
func (v WebhookUploadMediaResponse) CreatedAtTime() time.Time {
i, _ := v.CreatedAt.Int64()
return time.Unix(i, 0)
}
type WebhookUploadMediaRequest struct {
Key string
File fs.File
Context context.Context
Request req.Request
}
// WebhookUploadMedia upload media to get MediaID
func WebhookUploadMedia(r *WebhookUploadMediaRequest) (out WebhookUploadMediaResponse, err error) {
er := GenericResponse{}
err = req.Request{
BaseURL: DefaultAPI,
URL: "/cgi-bin/webhook/upload_media",
Method: http.MethodPost,
Query: map[string]string{
"key": r.Key,
"type": "file",
},
Context: r.Context,
Body: r.File,
Options: []interface{}{req.JSONDecode},
}.With(r.Request).Fetch(&er, &out)
if err == nil {
err = er.AsError()
}
return
}