1
0
mirror of https://github.com/silenceper/wechat.git synced 2026-02-04 12:52:27 +08:00

Feature/upload attachment (#720)

* feat(work): add UploadAttachment API

add UploadAttachment API

* feat(work): add UploadAttachment API

add UploadAttachment API

* feat(work): add UploadAttachment API

add UploadAttachment API
This commit is contained in:
曹晶
2023-09-24 10:43:11 +08:00
committed by GitHub
parent 9e810be88a
commit 4a2c44c7c8

View File

@@ -11,6 +11,8 @@ const (
uploadImgURL = "https://qyapi.weixin.qq.com/cgi-bin/media/uploadimg?access_token=%s"
// uploadTempFile 上传临时素材
uploadTempFile = "https://qyapi.weixin.qq.com/cgi-bin/media/upload?access_token=%s&type=%s"
// uploadAttachment 上传附件资源
uploadAttachment = "https://qyapi.weixin.qq.com/cgi-bin/media/upload_attachment?access_token=%s&media_type=%s&attachment_type=%d"
)
// UploadImgResponse 上传图片响应
@@ -27,6 +29,14 @@ type UploadTempFileResponse struct {
Type string `json:"type"`
}
// UploadAttachmentResponse 上传资源附件响应
type UploadAttachmentResponse struct {
util.CommonError
MediaID string `json:"media_id"`
CreateAt int64 `json:"created_at"`
Type string `json:"type"`
}
// UploadImg 上传图片
// @see https://developer.work.weixin.qq.com/document/path/90256
func (r *Client) UploadImg(filename string) (*UploadImgResponse, error) {
@@ -69,3 +79,26 @@ func (r *Client) UploadTempFile(filename string, mediaType string) (*UploadTempF
}
return result, nil
}
// UploadAttachment 上传附件资源
// @see https://developer.work.weixin.qq.com/document/path/95098
// @mediaType 媒体文件类型分别有图片image、视频video、普通文件file
// @attachment_type 附件类型不同的附件类型用于不同的场景。1朋友圈2:商品图册
func (r *Client) UploadAttachment(filename string, mediaType string, attachmentType int) (*UploadAttachmentResponse, error) {
var (
accessToken string
err error
)
if accessToken, err = r.GetAccessToken(); err != nil {
return nil, err
}
var response []byte
if response, err = util.PostFile("media", filename, fmt.Sprintf(uploadAttachment, accessToken, mediaType, attachmentType)); err != nil {
return nil, err
}
result := &UploadAttachmentResponse{}
if err = util.DecodeWithError(response, result, "UploadAttachment"); err != nil {
return nil, err
}
return result, nil
}