-
Notifications
You must be signed in to change notification settings - Fork 94
/
media.go
69 lines (60 loc) · 2.09 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
package wechat
import (
"encoding/json"
"fmt"
"github.com/esap/wechat/util"
)
const (
// WXAPIMediaUpload 临时素材上传
WXAPIMediaUpload = "media/upload?access_token=%s&type=%s"
// WXAPIMediaGet 临时素材下载
WXAPIMediaGet = "media/get?access_token=%s&media_id=%s"
// WXAPIMediaGetJssdk 高清语言素材下载
WXAPIMediaGetJssdk = "media/get/jssdk?access_token=%s&media_id=%s"
)
// Media 上传回复体
type Media struct {
WxErr
Type string `json:"type"`
MediaID string `json:"media_id"`
ThumbMediaId string `json:"thumb_media_id"`
CreatedAt interface{} `json:"created_at"` // 企业微信是string,服务号是int,采用interface{}统一接收
}
// MediaUpload 临时素材上传,mediaType选项如下:
// TypeImage = "image"
// TypeVoice = "voice"
// TypeVideo = "video"
// TypeFile = "file" // 仅企业微信可用
func (s *Server) MediaUpload(mediaType string, filename string) (media Media, err error) {
uri := fmt.Sprintf(s.RootUrl+WXAPIMediaUpload, s.GetAccessToken(), mediaType)
var b []byte
b, err = util.PostFile("media", filename, uri)
if err != nil {
return
}
if err = json.Unmarshal(b, &media); err != nil {
return
}
err = media.Error()
return
}
// GetMedia 下载临时素材
func (s *Server) GetMedia(filename, mediaId string) error {
url := fmt.Sprintf(s.RootUrl+WXAPIMediaGet, s.GetAccessToken(), mediaId)
return util.GetFile(filename, url)
}
// GetMediaBytes 下载临时素材,返回body字节
func (s *Server) GetMediaBytes(mediaId string) ([]byte, error) {
url := fmt.Sprintf(s.RootUrl+WXAPIMediaGet, s.GetAccessToken(), mediaId)
return util.GetBody(url)
}
// GetJsMedia 下载高清语言素材(通过JSSDK上传)
func (s *Server) GetJsMedia(filename, mediaId string) error {
url := fmt.Sprintf(s.RootUrl+WXAPIMediaGetJssdk, s.GetAccessToken(), mediaId)
return util.GetFile(filename, url)
}
// GetJsMediaBytes 下载高清语言素材,返回body字节
func (s *Server) GetJsMediaBytes(mediaId string) ([]byte, error) {
url := fmt.Sprintf(s.RootUrl+WXAPIMediaGetJssdk, s.GetAccessToken(), mediaId)
return util.GetBody(url)
}