forked from shenghui0779/sdk-go
/
media.go
109 lines (88 loc) · 2.4 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
package minip
import (
"bytes"
"context"
"encoding/json"
"io"
"os"
"path/filepath"
"github.com/liniu/gochat/urls"
"github.com/liniu/gochat/wx"
)
// MediaType 素材类型
type MediaType string
// 微信支持的素材类型
const MediaImage MediaType = "image" // 图片
// ResultMediaUpload 临时素材上传结果
type ResultMediaUpload struct {
Type MediaType `json:"type"`
MediaID string `json:"media_id"`
CreatedAt int64 `json:"created_at"`
}
// UploadTempMedia 客服消息 - 上传临时素材到微信服务器
func UploadTempMedia(mediaType MediaType, mediaPath string, result *ResultMediaUpload) wx.Action {
_, filename := filepath.Split(mediaPath)
return wx.NewPostAction(urls.MinipMediaUpload,
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)
}),
)
}
// UploadTempMediaByURL 客服消息 - 上传临时素材到微信服务器
func UploadTempMediaByURL(mediaType MediaType, filename, url string, result *ResultMediaUpload) wx.Action {
return wx.NewPostAction(urls.MinipMediaUpload,
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)
}),
)
}
// Media 临时素材
type Media struct {
Buffer []byte
}
// GetTempMedia 客服消息 - 获取客服消息内的临时素材
func GetTempMedia(mediaID string, media *Media) wx.Action {
return wx.NewGetAction(urls.MinipMediaGet,
wx.WithQuery("media_id", mediaID),
wx.WithDecode(func(b []byte) error {
media.Buffer = make([]byte, len(b))
copy(media.Buffer, b)
return nil
}),
)
}