forked from shenghui0779/sdk-go
/
media.go
524 lines (438 loc) · 13.5 KB
/
media.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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
package offia
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"os"
"path/filepath"
"github.com/liniu/gochat/urls"
"github.com/liniu/gochat/wx"
)
// MediaType 素材类型
type MediaType string
// 微信支持的素材类型
const (
MediaImage MediaType = "image" // 图片
MediaVoice MediaType = "voice" // 音频
MediaVideo MediaType = "video" // 视频
MediaThumb MediaType = "thumb" // 缩略图
)
// ResultMediaUpload 临时素材上传结果
type ResultMediaUpload struct {
Type string `json:"type"`
MediaID string `json:"media_id"`
CreatedAt int64 `json:"created_at"`
}
// UploadMedia 素材管理 - 上传临时素材
func UploadMedia(mediaType MediaType, mediaPath string, result *ResultMediaUpload) wx.Action {
_, filename := filepath.Split(mediaPath)
return wx.NewPostAction(urls.OffiaMediaUpload,
wx.WithQuery("type", string(mediaType)),
wx.WithUpload(func() (wx.UploadForm, error) {
path, err := filepath.Abs(filepath.Clean(mediaPath))
if err != nil {
return nil, err
}
return wx.NewUploadForm(
wx.WithFormFile("media", filename, func(w io.Writer) error {
f, err := os.Open(path)
if err != nil {
return err
}
defer f.Close()
if _, err = io.Copy(w, f); err != nil {
return err
}
return nil
}),
), nil
}),
wx.WithDecode(func(b []byte) error {
return json.Unmarshal(b, result)
}),
)
}
// UploadMediaByURL 素材管理 - 上传临时素材
func UploadMediaByURL(mediaType MediaType, filename, url string, result *ResultMediaUpload) wx.Action {
return wx.NewPostAction(urls.OffiaMediaUpload,
wx.WithQuery("type", string(mediaType)),
wx.WithUpload(func() (wx.UploadForm, error) {
return wx.NewUploadForm(
wx.WithFormFile("media", filename, func(w io.Writer) error {
resp, err := wx.HTTPGet(context.Background(), url)
if err != nil {
return err
}
if _, err = io.Copy(w, bytes.NewReader(resp)); err != nil {
return err
}
return nil
}),
), nil
}),
wx.WithDecode(func(b []byte) error {
return json.Unmarshal(b, result)
}),
)
}
// ResultMaterialAdd 永久素材新增结果
type ResultMaterialAdd struct {
MediaID string `json:"media_id"`
URL string `json:"url"`
}
// AddMaterial 素材管理 - 新增其他类型永久素材(支持图片、音频、缩略图)
func AddMaterial(mediaType MediaType, mediaPath string, result *ResultMaterialAdd) wx.Action {
_, filename := filepath.Split(mediaPath)
return wx.NewPostAction(urls.OffiaMaterialAdd,
wx.WithQuery("type", string(mediaType)),
wx.WithUpload(func() (wx.UploadForm, error) {
path, err := filepath.Abs(filepath.Clean(mediaPath))
if err != nil {
return nil, err
}
return wx.NewUploadForm(
wx.WithFormFile("media", filename, func(w io.Writer) error {
f, err := os.Open(path)
if err != nil {
return err
}
defer f.Close()
if _, err = io.Copy(w, f); err != nil {
return err
}
return nil
}),
), nil
}),
wx.WithDecode(func(b []byte) error {
return json.Unmarshal(b, result)
}),
)
}
// AddMaterialByURL 素材管理 - 新增其他类型永久素材(支持图片、音频、缩略图)
func AddMaterialByURL(mediaType MediaType, filename, url string, result *ResultMaterialAdd) wx.Action {
return wx.NewPostAction(urls.OffiaMaterialAdd,
wx.WithQuery("type", string(mediaType)),
wx.WithUpload(func() (wx.UploadForm, error) {
return wx.NewUploadForm(
wx.WithFormFile("media", filename, func(w io.Writer) error {
resp, err := wx.HTTPGet(context.Background(), url)
if err != nil {
return err
}
if _, err = io.Copy(w, bytes.NewReader(resp)); err != nil {
return err
}
return nil
}),
), nil
}),
wx.WithDecode(func(b []byte) error {
return json.Unmarshal(b, result)
}),
)
}
type ParamsMaterialGet struct {
MediaID string `json:"media_id"`
}
type ResultNewsMaterialGet struct {
NewsItem []*MaterialNewsItem `json:"news_item"`
}
type MaterialNewsItem struct {
Title string `json:"title"`
ThumbMediaID string `json:"thumb_media_id"`
ShowCoverPic int `json:"show_cover_pic"`
Author string `json:"author"`
Digest string `json:"digest"`
Content string `json:"content"`
URL string `json:"url"`
ContentSourceURL string `json:"content_source_url"`
}
// GetNewsMaterial 素材管理 - 获取永久素材(图文)
func GetNewsMaterial(mediaID string, result *ResultNewsMaterialGet) wx.Action {
params := &ParamsMaterialGet{
MediaID: mediaID,
}
return wx.NewPostAction(urls.OffiaMaterialGet,
wx.WithBody(func() ([]byte, error) {
return wx.MarshalNoEscapeHTML(params)
}),
wx.WithDecode(func(b []byte) error {
return json.Unmarshal(b, result)
}),
)
}
type ResultVideoMaterialGet struct {
Title string `json:"title"`
Description string `json:"description"`
DownURL string `json:"down_url"`
}
// GetVideoMaterial 素材管理 - 获取永久素材(视频消息)
func GetVideoMaterial(mediaID string, result *ResultVideoMaterialGet) wx.Action {
params := &ParamsMaterialGet{
MediaID: mediaID,
}
return wx.NewPostAction(urls.OffiaMaterialGet,
wx.WithBody(func() ([]byte, error) {
return wx.MarshalNoEscapeHTML(params)
}),
wx.WithDecode(func(b []byte) error {
return json.Unmarshal(b, result)
}),
)
}
type ResultOtherMaterialGet struct {
Buffer []byte
}
// GetOtherMaterial 素材管理 - 获取永久素材(其他类型的素材消息,返回的直接为素材的内容)
func GetOtherMaterial(mediaID string, result *ResultOtherMaterialGet) wx.Action {
params := &ParamsMaterialGet{
MediaID: mediaID,
}
return wx.NewPostAction(urls.OffiaMaterialGet,
wx.WithBody(func() ([]byte, error) {
return wx.MarshalNoEscapeHTML(params)
}),
wx.WithDecode(func(b []byte) error {
result.Buffer = make([]byte, len(b))
copy(result.Buffer, b)
return nil
}),
)
}
// DeleteMaterial 素材管理 - 删除永久素材
func DeleteMaterial(mediaID string) wx.Action {
return wx.NewPostAction(urls.OffiaMaterialDelete,
wx.WithBody(func() ([]byte, error) {
return wx.MarshalNoEscapeHTML(wx.M{"media_id": mediaID})
}),
)
}
// UploadImg 素材管理 - 上传图文消息内的图片(不受公众号的素材库中图片数量的100000个的限制,图片仅支持jpg/png格式,大小必须在1MB以下)
func UploadImg(imgPath string, result *ResultMaterialAdd) wx.Action {
_, filename := filepath.Split(imgPath)
return wx.NewPostAction(urls.OffiaNewsImgUpload,
wx.WithUpload(func() (wx.UploadForm, error) {
path, err := filepath.Abs(filepath.Clean(imgPath))
if err != nil {
return nil, err
}
return wx.NewUploadForm(
wx.WithFormFile("media", filename, func(w io.Writer) error {
f, err := os.Open(path)
if err != nil {
return err
}
defer f.Close()
if _, err = io.Copy(w, f); err != nil {
return err
}
return nil
}),
), nil
}),
wx.WithDecode(func(b []byte) error {
return json.Unmarshal(b, result)
}),
)
}
// UploadImgByURL 素材管理 - 上传图文消息内的图片(不受公众号的素材库中图片数量的100000个的限制,图片仅支持jpg/png格式,大小必须在1MB以下)
func UploadImgByURL(filename, url string, result *ResultMaterialAdd) wx.Action {
return wx.NewPostAction(urls.OffiaNewsImgUpload,
wx.WithUpload(func() (wx.UploadForm, error) {
return wx.NewUploadForm(
wx.WithFormFile("media", filename, func(w io.Writer) error {
resp, err := wx.HTTPGet(context.Background(), url)
if err != nil {
return err
}
if _, err = io.Copy(w, bytes.NewReader(resp)); err != nil {
return err
}
return nil
}),
), nil
}),
wx.WithDecode(func(b []byte) error {
return json.Unmarshal(b, result)
}),
)
}
// UploadVideo 素材管理 - 上传视频永久素材
func UploadVideo(videoPath, title, description string, result *ResultMaterialAdd) wx.Action {
_, filename := filepath.Split(videoPath)
return wx.NewPostAction(urls.OffiaMaterialAdd,
wx.WithQuery("type", string(MediaVideo)),
wx.WithUpload(func() (wx.UploadForm, error) {
path, err := filepath.Abs(filepath.Clean(videoPath))
if err != nil {
return nil, err
}
return wx.NewUploadForm(
wx.WithFormFile("media", filename, func(w io.Writer) error {
f, err := os.Open(path)
if err != nil {
return err
}
defer f.Close()
if _, err = io.Copy(w, f); err != nil {
return err
}
return nil
}),
wx.WithFormField("description", fmt.Sprintf(`{"title":"%s", "introduction":"%s"}`, title, description)),
), nil
}),
wx.WithDecode(func(b []byte) error {
return json.Unmarshal(b, result)
}),
)
}
// UploadVideoByURL 素材管理 - 上传视频永久素材
func UploadVideoByURL(filename, videoURL, title, description string, result *ResultMaterialAdd) wx.Action {
return wx.NewPostAction(urls.OffiaMaterialAdd,
wx.WithQuery("type", string(MediaVideo)),
wx.WithUpload(func() (wx.UploadForm, error) {
return wx.NewUploadForm(
wx.WithFormFile("media", filename, func(w io.Writer) error {
resp, err := wx.HTTPGet(context.Background(), videoURL)
if err != nil {
return err
}
if _, err = io.Copy(w, bytes.NewReader(resp)); err != nil {
return err
}
return nil
}),
wx.WithFormField("description", fmt.Sprintf(`{"title":"%s", "introduction":"%s"}`, title, description)),
), nil
}),
wx.WithDecode(func(b []byte) error {
return json.Unmarshal(b, result)
}),
)
}
// NewsArticle 文章素材
type NewsArticle struct {
Title string `json:"title"`
ThumbMediaID string `json:"thumb_media_id"`
Author string `json:"author"`
Digest string `json:"digest"`
ShowCoverPic int `json:"show_cover_pic"`
Content string `json:"content"`
ContentSourceURL string `json:"content_source_url"`
NeedOpenComment int `json:"need_open_comment,omitempty"`
OnlyFansCanComment int `json:"only_fans_can_comment,omitempty"`
}
type ParamsNewsAdd struct {
Articles []*NewsArticle `json:"articles"`
}
// AddNews 素材管理 - 新增永久图文素材(公众号的素材库保存总数量有上限:图文消息素材、图片素材上限为100000,其他类型为1000)
func AddNews(articles []*NewsArticle, result *ResultMaterialAdd) wx.Action {
params := &ParamsNewsAdd{
Articles: articles,
}
return wx.NewPostAction(urls.OffiaNewsAdd,
wx.WithBody(func() ([]byte, error) {
return wx.MarshalNoEscapeHTML(params)
}),
wx.WithDecode(func(b []byte) error {
return json.Unmarshal(b, result)
}),
)
}
type ParamsNewsUpdate struct {
MediaID string `json:"media_id"`
Index string `json:"index"`
Articles *NewsArticle `json:"articles"`
}
// UpdateNews 素材管理 - 编辑永久图文素材
func UpdateNews(mediaID, index string, article *NewsArticle) wx.Action {
params := &ParamsNewsUpdate{
MediaID: mediaID,
Index: index,
Articles: article,
}
return wx.NewPostAction(urls.OffiaNewsUpdate,
wx.WithBody(func() ([]byte, error) {
return wx.MarshalNoEscapeHTML(params)
}),
)
}
type ResultMaterialCount struct {
VoiceCount int `json:"voice_count"`
VideoCount int `json:"video_count"`
ImageCount int `json:"image_count"`
NewsCount int `json:"news_count"`
}
// GetMaterialCount 素材管理 - 获取素材总数
func GetMaterialCount(result *ResultMaterialCount) wx.Action {
return wx.NewGetAction(urls.OffiaMaterialCountGet,
wx.WithDecode(func(b []byte) error {
return json.Unmarshal(b, result)
}),
)
}
type ParamsMaterialList struct {
Type MediaType `json:"type"`
Offset int `json:"offset"`
Count int `json:"count"`
}
type ResultMaterialList struct {
TotalCount int `json:"total_count"`
ItemCount int `json:"item_count"`
Item []*MaterialListItem `json:"item"`
}
type MaterialListItem struct {
MediaID string `json:"media_id"`
Name string `json:"name"`
UpdateTime int64 `json:"update_time"`
URL string `json:"url"`
}
// ListMatertial 素材管理 - 获取素材列表(其他类型:图片、语音、视频)
func ListMatertial(mediaType MediaType, offset, count int, result *ResultMaterialList) wx.Action {
params := &ParamsMaterialList{
Type: mediaType,
Offset: offset,
Count: count,
}
return wx.NewPostAction(urls.OffiaMaterialBatchGet,
wx.WithBody(func() ([]byte, error) {
return wx.MarshalNoEscapeHTML(params)
}),
wx.WithDecode(func(b []byte) error {
return json.Unmarshal(b, result)
}),
)
}
type ResultMaterialNewsList struct {
TotalCount int `json:"total_count"`
ItemCount int `json:"item_count"`
Item []*MaterialNewsListItem `json:"item"`
}
type MaterialNewsListItem struct {
MediaID string `json:"media_id"`
UpdateTime int64 `json:"update_time"`
Content *MaterialNewsListContent `json:"content"`
}
type MaterialNewsListContent struct {
NewsItem []*MaterialNewsItem `json:"news_item"`
}
// ListMaterialNews 素材管理 - 获取素材列表(永久图文消息)
func ListMaterialNews(offset, count int, result *ResultMaterialNewsList) wx.Action {
params := &ParamsMaterialList{
Type: MediaType("news"),
Offset: offset,
Count: count,
}
return wx.NewPostAction(urls.OffiaMaterialBatchGet,
wx.WithBody(func() ([]byte, error) {
return wx.MarshalNoEscapeHTML(params)
}),
wx.WithDecode(func(b []byte) error {
return json.Unmarshal(b, result)
}),
)
}