Skip to content

Commit

Permalink
Feature/upload attachment (#720)
Browse files Browse the repository at this point in the history
* feat(work): add UploadAttachment API

add UploadAttachment API

* feat(work): add UploadAttachment API

add UploadAttachment API

* feat(work): add UploadAttachment API

add UploadAttachment API
  • Loading branch information
Lumiaqian authored Sep 24, 2023
1 parent 9e810be commit 4a2c44c
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions work/material/media.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 上传图片响应
Expand All @@ -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) {
Expand Down Expand Up @@ -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
}

0 comments on commit 4a2c44c

Please sign in to comment.