forked from shenghui0779/sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
media.go
52 lines (42 loc) · 1.15 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
package mp
import (
"encoding/json"
"github.com/accaolei/gochat/wx"
)
// MediaType 素材类型
type MediaType string
// 微信支持的素材类型
var MediaImage MediaType = "image" // 图片
// MediaUploadResult 临时素材上传信息
type MediaUploadResult struct {
Type string `json:"type"`
MediaID string `json:"media_id"`
CreatedAt int64 `json:"created_at"`
}
// UploadMedia 上传临时素材到微信服务器
func UploadMedia(dest *MediaUploadResult, mediaType MediaType, filename string) wx.Action {
return wx.NewAPI(MediaUploadURL,
wx.WithMethod(wx.MethodUpload),
wx.WithQuery("type", string(mediaType)),
wx.WithUploadForm("media", filename, nil),
wx.WithDecode(func(resp []byte) error {
return json.Unmarshal(resp, dest)
}),
)
}
// Media 临时素材
type Media struct {
Buffer []byte
}
// GetMedia 获取客服消息内的临时素材
func GetMedia(dest *Media, mediaID string) wx.Action {
return wx.NewAPI(MediaGetURL,
wx.WithMethod(wx.MethodGet),
wx.WithQuery("media_id", mediaID),
wx.WithDecode(func(resp []byte) error {
dest.Buffer = make([]byte, len(resp))
copy(dest.Buffer, resp)
return nil
}),
)
}