mirror of
https://github.com/silenceper/wechat.git
synced 2026-02-13 01:02:27 +08:00
Compare commits
6 Commits
v2.1.7-rc.
...
a5776bc9f2
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a5776bc9f2 | ||
|
|
2e0708845b | ||
|
|
c1770130a0 | ||
|
|
c22a036b7f | ||
|
|
05ac7148d4 | ||
|
|
6b3532cc2d |
2
cache/redis.go
vendored
2
cache/redis.go
vendored
@@ -16,6 +16,7 @@ type Redis struct {
|
|||||||
// RedisOpts redis 连接属性
|
// RedisOpts redis 连接属性
|
||||||
type RedisOpts struct {
|
type RedisOpts struct {
|
||||||
Host string `yml:"host" json:"host"`
|
Host string `yml:"host" json:"host"`
|
||||||
|
Username string `yaml:"username" json:"username"`
|
||||||
Password string `yml:"password" json:"password"`
|
Password string `yml:"password" json:"password"`
|
||||||
Database int `yml:"database" json:"database"`
|
Database int `yml:"database" json:"database"`
|
||||||
MaxIdle int `yml:"max_idle" json:"max_idle"`
|
MaxIdle int `yml:"max_idle" json:"max_idle"`
|
||||||
@@ -28,6 +29,7 @@ func NewRedis(ctx context.Context, opts *RedisOpts) *Redis {
|
|||||||
conn := redis.NewUniversalClient(&redis.UniversalOptions{
|
conn := redis.NewUniversalClient(&redis.UniversalOptions{
|
||||||
Addrs: []string{opts.Host},
|
Addrs: []string{opts.Host},
|
||||||
DB: opts.Database,
|
DB: opts.Database,
|
||||||
|
Username: opts.Username,
|
||||||
Password: opts.Password,
|
Password: opts.Password,
|
||||||
IdleTimeout: time.Second * time.Duration(opts.IdleTimeout),
|
IdleTimeout: time.Second * time.Duration(opts.IdleTimeout),
|
||||||
MinIdleConns: opts.MaxIdle,
|
MinIdleConns: opts.MaxIdle,
|
||||||
|
|||||||
@@ -54,6 +54,8 @@ type QRCoder struct {
|
|||||||
IsHyaline bool `json:"is_hyaline,omitempty"`
|
IsHyaline bool `json:"is_hyaline,omitempty"`
|
||||||
// envVersion 要打开的小程序版本。正式版为 "release",体验版为 "trial",开发版为 "develop"
|
// envVersion 要打开的小程序版本。正式版为 "release",体验版为 "trial",开发版为 "develop"
|
||||||
EnvVersion string `json:"env_version,omitempty"`
|
EnvVersion string `json:"env_version,omitempty"`
|
||||||
|
// ShowSplashAd 控制通过该小程序码进入小程序是否展示封面广告1、默认为true,展示封面广告2、传入为false时,不展示封面广告
|
||||||
|
ShowSplashAd bool `json:"show_splash_ad,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// fetchCode 请求并返回二维码二进制数据
|
// fetchCode 请求并返回二维码二进制数据
|
||||||
|
|||||||
@@ -4,6 +4,9 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"os"
|
||||||
|
"path"
|
||||||
|
|
||||||
"github.com/silenceper/wechat/v2/officialaccount/context"
|
"github.com/silenceper/wechat/v2/officialaccount/context"
|
||||||
"github.com/silenceper/wechat/v2/util"
|
"github.com/silenceper/wechat/v2/util"
|
||||||
@@ -160,8 +163,8 @@ type resAddMaterial struct {
|
|||||||
URL string `json:"url"`
|
URL string `json:"url"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// AddMaterial 上传永久性素材(处理视频需要单独上传)
|
// AddMaterialFromReader 上传永久性素材(处理视频需要单独上传),从 io.Reader 中读取
|
||||||
func (material *Material) AddMaterial(mediaType MediaType, filename string) (mediaID string, url string, err error) {
|
func (material *Material) AddMaterialFromReader(mediaType MediaType, filePath string, reader io.Reader) (mediaID string, url string, err error) {
|
||||||
if mediaType == MediaTypeVideo {
|
if mediaType == MediaTypeVideo {
|
||||||
err = errors.New("永久视频素材上传使用 AddVideo 方法")
|
err = errors.New("永久视频素材上传使用 AddVideo 方法")
|
||||||
return
|
return
|
||||||
@@ -173,8 +176,10 @@ func (material *Material) AddMaterial(mediaType MediaType, filename string) (med
|
|||||||
}
|
}
|
||||||
|
|
||||||
uri := fmt.Sprintf("%s?access_token=%s&type=%s", addMaterialURL, accessToken, mediaType)
|
uri := fmt.Sprintf("%s?access_token=%s&type=%s", addMaterialURL, accessToken, mediaType)
|
||||||
|
// 获取文件名
|
||||||
|
filename := path.Base(filePath)
|
||||||
var response []byte
|
var response []byte
|
||||||
response, err = util.PostFile("media", filename, uri)
|
response, err = util.PostFileFromReader("media", filePath, filename, uri, reader)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -192,13 +197,24 @@ func (material *Material) AddMaterial(mediaType MediaType, filename string) (med
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// AddMaterial 上传永久性素材(处理视频需要单独上传)
|
||||||
|
func (material *Material) AddMaterial(mediaType MediaType, filename string) (mediaID string, url string, err error) {
|
||||||
|
f, err := os.Open(filename)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer func() { _ = f.Close() }()
|
||||||
|
|
||||||
|
return material.AddMaterialFromReader(mediaType, filename, f)
|
||||||
|
}
|
||||||
|
|
||||||
type reqVideo struct {
|
type reqVideo struct {
|
||||||
Title string `json:"title"`
|
Title string `json:"title"`
|
||||||
Introduction string `json:"introduction"`
|
Introduction string `json:"introduction"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// AddVideo 永久视频素材文件上传
|
// AddVideoFromReader 永久视频素材文件上传,从 io.Reader 中读取
|
||||||
func (material *Material) AddVideo(filename, title, introduction string) (mediaID string, url string, err error) {
|
func (material *Material) AddVideoFromReader(filePath, title, introduction string, reader io.Reader) (mediaID string, url string, err error) {
|
||||||
var accessToken string
|
var accessToken string
|
||||||
accessToken, err = material.GetAccessToken()
|
accessToken, err = material.GetAccessToken()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -216,16 +232,19 @@ func (material *Material) AddVideo(filename, title, introduction string) (mediaI
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
fileName := path.Base(filePath)
|
||||||
fields := []util.MultipartFormField{
|
fields := []util.MultipartFormField{
|
||||||
{
|
{
|
||||||
IsFile: true,
|
IsFile: true,
|
||||||
Fieldname: "media",
|
Fieldname: "media",
|
||||||
Filename: filename,
|
FilePath: filePath,
|
||||||
|
Filename: fileName,
|
||||||
|
FileReader: reader,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
IsFile: false,
|
IsFile: false,
|
||||||
Fieldname: "description",
|
Fieldname: "description",
|
||||||
|
Filename: fileName,
|
||||||
Value: fieldValue,
|
Value: fieldValue,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
@@ -250,6 +269,17 @@ func (material *Material) AddVideo(filename, title, introduction string) (mediaI
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// AddVideo 永久视频素材文件上传
|
||||||
|
func (material *Material) AddVideo(directory, title, introduction string) (mediaID string, url string, err error) {
|
||||||
|
f, err := os.Open(directory)
|
||||||
|
if err != nil {
|
||||||
|
return "", "", err
|
||||||
|
}
|
||||||
|
defer func() { _ = f.Close() }()
|
||||||
|
|
||||||
|
return material.AddVideoFromReader(directory, title, introduction, f)
|
||||||
|
}
|
||||||
|
|
||||||
type reqDeleteMaterial struct {
|
type reqDeleteMaterial struct {
|
||||||
MediaID string `json:"media_id"`
|
MediaID string `json:"media_id"`
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package material
|
|||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
|
|
||||||
"github.com/silenceper/wechat/v2/util"
|
"github.com/silenceper/wechat/v2/util"
|
||||||
)
|
)
|
||||||
@@ -62,6 +63,38 @@ func (material *Material) MediaUpload(mediaType MediaType, filename string) (med
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// MediaUploadFromReader 临时素材上传
|
||||||
|
func (material *Material) MediaUploadFromReader(mediaType MediaType, filename string, reader io.Reader) (media Media, err error) {
|
||||||
|
var accessToken string
|
||||||
|
accessToken, err = material.GetAccessToken()
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
uri := fmt.Sprintf("%s?access_token=%s&type=%s", mediaUploadURL, accessToken, mediaType)
|
||||||
|
|
||||||
|
var byteData []byte
|
||||||
|
byteData, err = io.ReadAll(reader)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var response []byte
|
||||||
|
response, err = util.PostFileByStream("media", filename, uri, byteData)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
err = json.Unmarshal(response, &media)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if media.ErrCode != 0 {
|
||||||
|
err = fmt.Errorf("MediaUpload error : errcode=%v , errmsg=%v", media.ErrCode, media.ErrMsg)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// GetMediaURL 返回临时素材的下载地址供用户自己处理
|
// GetMediaURL 返回临时素材的下载地址供用户自己处理
|
||||||
// NOTICE: URL 不可公开,因为含access_token 需要立即另存文件
|
// NOTICE: URL 不可公开,因为含access_token 需要立即另存文件
|
||||||
func (material *Material) GetMediaURL(mediaID string) (mediaURL string, err error) {
|
func (material *Material) GetMediaURL(mediaID string) (mediaURL string, err error) {
|
||||||
|
|||||||
@@ -61,15 +61,15 @@ func (tpl *Template) Send(msg *TemplateMessage) (msgID int64, err error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
uri := fmt.Sprintf("%s?access_token=%s", templateSendURL, accessToken)
|
var (
|
||||||
var response []byte
|
uri = fmt.Sprintf("%s?access_token=%s", templateSendURL, accessToken)
|
||||||
response, err = util.PostJSON(uri, msg)
|
response []byte
|
||||||
if err != nil {
|
)
|
||||||
|
if response, err = util.PostJSON(uri, msg); err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
var result resTemplateSend
|
var result resTemplateSend
|
||||||
err = json.Unmarshal(response, &result)
|
if err = json.Unmarshal(response, &result); err != nil {
|
||||||
if err != nil {
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if result.ErrCode != 0 {
|
if result.ErrCode != 0 {
|
||||||
@@ -103,10 +103,11 @@ func (tpl *Template) List() (templateList []*TemplateItem, err error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
uri := fmt.Sprintf("%s?access_token=%s", templateListURL, accessToken)
|
var (
|
||||||
var response []byte
|
uri = fmt.Sprintf("%s?access_token=%s", templateListURL, accessToken)
|
||||||
response, err = util.HTTPGet(uri)
|
response []byte
|
||||||
if err != nil {
|
)
|
||||||
|
if response, err = util.HTTPGet(uri); err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
var res resTemplateList
|
var res resTemplateList
|
||||||
@@ -121,22 +122,23 @@ type resTemplateAdd struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Add 添加模板.
|
// Add 添加模板.
|
||||||
func (tpl *Template) Add(shortID string) (templateID string, err error) {
|
func (tpl *Template) Add(shortID string, keyNameList []string) (templateID string, err error) {
|
||||||
var accessToken string
|
var accessToken string
|
||||||
accessToken, err = tpl.GetAccessToken()
|
accessToken, err = tpl.GetAccessToken()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
var msg = struct {
|
var (
|
||||||
ShortID string `json:"template_id_short"`
|
msg = struct {
|
||||||
}{ShortID: shortID}
|
ShortID string `json:"template_id_short"`
|
||||||
uri := fmt.Sprintf("%s?access_token=%s", templateAddURL, accessToken)
|
KeyNameList []string `json:"keyword_name_list"`
|
||||||
var response []byte
|
}{ShortID: shortID, KeyNameList: keyNameList}
|
||||||
response, err = util.PostJSON(uri, msg)
|
uri = fmt.Sprintf("%s?access_token=%s", templateAddURL, accessToken)
|
||||||
if err != nil {
|
response []byte
|
||||||
|
)
|
||||||
|
if response, err = util.PostJSON(uri, msg); err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var result resTemplateAdd
|
var result resTemplateAdd
|
||||||
err = util.DecodeWithError(response, &result, "AddTemplate")
|
err = util.DecodeWithError(response, &result, "AddTemplate")
|
||||||
return result.TemplateID, err
|
return result.TemplateID, err
|
||||||
@@ -149,14 +151,14 @@ func (tpl *Template) Delete(templateID string) (err error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
var msg = struct {
|
var (
|
||||||
TemplateID string `json:"template_id"`
|
msg = struct {
|
||||||
}{TemplateID: templateID}
|
TemplateID string `json:"template_id"`
|
||||||
|
}{TemplateID: templateID}
|
||||||
uri := fmt.Sprintf("%s?access_token=%s", templateDelURL, accessToken)
|
uri = fmt.Sprintf("%s?access_token=%s", templateDelURL, accessToken)
|
||||||
var response []byte
|
response []byte
|
||||||
response, err = util.PostJSON(uri, msg)
|
)
|
||||||
if err != nil {
|
if response, err = util.PostJSON(uri, msg); err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
return util.DecodeWithCommonError(response, "DeleteTemplate")
|
return util.DecodeWithCommonError(response, "DeleteTemplate")
|
||||||
|
|||||||
69
util/http.go
69
util/http.go
@@ -146,13 +146,40 @@ func PostJSONWithRespContentType(uri string, obj interface{}) ([]byte, string, e
|
|||||||
return responseData, contentType, err
|
return responseData, contentType, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// PostFileByStream 上传文件
|
||||||
|
func PostFileByStream(fieldName, fileName, uri string, byteData []byte) ([]byte, error) {
|
||||||
|
fields := []MultipartFormField{
|
||||||
|
{
|
||||||
|
IsFile: false,
|
||||||
|
Fieldname: fieldName,
|
||||||
|
Filename: fileName,
|
||||||
|
Value: byteData,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
return PostMultipartForm(fields, uri)
|
||||||
|
}
|
||||||
|
|
||||||
// PostFile 上传文件
|
// PostFile 上传文件
|
||||||
func PostFile(fieldName, filename, uri string) ([]byte, error) {
|
func PostFile(fieldName, filePath, uri string) ([]byte, error) {
|
||||||
fields := []MultipartFormField{
|
fields := []MultipartFormField{
|
||||||
{
|
{
|
||||||
IsFile: true,
|
IsFile: true,
|
||||||
Fieldname: fieldName,
|
Fieldname: fieldName,
|
||||||
Filename: filename,
|
FilePath: filePath,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
return PostMultipartForm(fields, uri)
|
||||||
|
}
|
||||||
|
|
||||||
|
// PostFileFromReader 上传文件,从 io.Reader 中读取
|
||||||
|
func PostFileFromReader(filedName, filePath, fileName, uri string, reader io.Reader) ([]byte, error) {
|
||||||
|
fields := []MultipartFormField{
|
||||||
|
{
|
||||||
|
IsFile: true,
|
||||||
|
Fieldname: filedName,
|
||||||
|
FilePath: filePath,
|
||||||
|
Filename: fileName,
|
||||||
|
FileReader: reader,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
return PostMultipartForm(fields, uri)
|
return PostMultipartForm(fields, uri)
|
||||||
@@ -160,10 +187,12 @@ func PostFile(fieldName, filename, uri string) ([]byte, error) {
|
|||||||
|
|
||||||
// MultipartFormField 保存文件或其他字段信息
|
// MultipartFormField 保存文件或其他字段信息
|
||||||
type MultipartFormField struct {
|
type MultipartFormField struct {
|
||||||
IsFile bool
|
IsFile bool
|
||||||
Fieldname string
|
Fieldname string
|
||||||
Value []byte
|
Value []byte
|
||||||
Filename string
|
FilePath string
|
||||||
|
Filename string
|
||||||
|
FileReader io.Reader
|
||||||
}
|
}
|
||||||
|
|
||||||
// PostMultipartForm 上传文件或其他多个字段
|
// PostMultipartForm 上传文件或其他多个字段
|
||||||
@@ -182,18 +211,24 @@ func PostMultipartForm(fields []MultipartFormField, uri string) (respBody []byte
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
fh, e := os.Open(field.Filename)
|
if field.FileReader == nil {
|
||||||
if e != nil {
|
fh, e := os.Open(field.FilePath)
|
||||||
err = fmt.Errorf("error opening file , err=%v", e)
|
if e != nil {
|
||||||
return
|
err = fmt.Errorf("error opening file , err=%v", e)
|
||||||
}
|
return
|
||||||
defer fh.Close()
|
}
|
||||||
|
_, err = io.Copy(fileWriter, fh)
|
||||||
if _, err = io.Copy(fileWriter, fh); err != nil {
|
_ = fh.Close()
|
||||||
return
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if _, err = io.Copy(fileWriter, field.FileReader); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
partWriter, e := bodyWriter.CreateFormField(field.Fieldname)
|
partWriter, e := bodyWriter.CreateFormFile(field.Fieldname, field.Filename)
|
||||||
if e != nil {
|
if e != nil {
|
||||||
err = e
|
err = e
|
||||||
return
|
return
|
||||||
@@ -215,7 +250,7 @@ func PostMultipartForm(fields []MultipartFormField, uri string) (respBody []byte
|
|||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
if resp.StatusCode != http.StatusOK {
|
if resp.StatusCode != http.StatusOK {
|
||||||
return nil, err
|
return nil, fmt.Errorf("http code error : uri=%v , statusCode=%v", uri, resp.StatusCode)
|
||||||
}
|
}
|
||||||
respBody, err = io.ReadAll(resp.Body)
|
respBody, err = io.ReadAll(resp.Body)
|
||||||
return
|
return
|
||||||
|
|||||||
@@ -33,6 +33,7 @@ type AccountAddSchema struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// AccountAdd 添加客服账号
|
// AccountAdd 添加客服账号
|
||||||
|
// see https://developer.work.weixin.qq.com/document/path/94662
|
||||||
func (r *Client) AccountAdd(options AccountAddOptions) (info AccountAddSchema, err error) {
|
func (r *Client) AccountAdd(options AccountAddOptions) (info AccountAddSchema, err error) {
|
||||||
var (
|
var (
|
||||||
accessToken string
|
accessToken string
|
||||||
@@ -59,6 +60,7 @@ type AccountDelOptions struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// AccountDel 删除客服账号
|
// AccountDel 删除客服账号
|
||||||
|
// see https://developer.work.weixin.qq.com/document/path/94663
|
||||||
func (r *Client) AccountDel(options AccountDelOptions) (info util.CommonError, err error) {
|
func (r *Client) AccountDel(options AccountDelOptions) (info util.CommonError, err error) {
|
||||||
var (
|
var (
|
||||||
accessToken string
|
accessToken string
|
||||||
@@ -86,7 +88,8 @@ type AccountUpdateOptions struct {
|
|||||||
MediaID string `json:"media_id"` // 客服头像临时素材。可以调用上传临时素材接口获取, 不多于128个字节
|
MediaID string `json:"media_id"` // 客服头像临时素材。可以调用上传临时素材接口获取, 不多于128个字节
|
||||||
}
|
}
|
||||||
|
|
||||||
// AccountUpdate 修复客服账号
|
// AccountUpdate 修改客服账号
|
||||||
|
// see https://developer.work.weixin.qq.com/document/path/94664
|
||||||
func (r *Client) AccountUpdate(options AccountUpdateOptions) (info util.CommonError, err error) {
|
func (r *Client) AccountUpdate(options AccountUpdateOptions) (info util.CommonError, err error) {
|
||||||
var (
|
var (
|
||||||
accessToken string
|
accessToken string
|
||||||
@@ -109,9 +112,10 @@ func (r *Client) AccountUpdate(options AccountUpdateOptions) (info util.CommonEr
|
|||||||
|
|
||||||
// AccountInfoSchema 客服详情
|
// AccountInfoSchema 客服详情
|
||||||
type AccountInfoSchema struct {
|
type AccountInfoSchema struct {
|
||||||
OpenKFID string `json:"open_kfid"` // 客服帐号ID
|
OpenKFID string `json:"open_kfid"` // 客服帐号ID
|
||||||
Name string `json:"name"` // 客服帐号名称
|
Name string `json:"name"` // 客服帐号名称
|
||||||
Avatar string `json:"avatar"` // 客服头像URL
|
Avatar string `json:"avatar"` // 客服头像URL
|
||||||
|
ManagePrivilege bool `json:"manage_privilege"` // 当前调用接口的应用身份,是否有该客服账号的管理权限(编辑客服账号信息、分配会话和收发消息)
|
||||||
}
|
}
|
||||||
|
|
||||||
// AccountListSchema 获取客服账号列表响应内容
|
// AccountListSchema 获取客服账号列表响应内容
|
||||||
@@ -141,6 +145,31 @@ func (r *Client) AccountList() (info AccountListSchema, err error) {
|
|||||||
return info, nil
|
return info, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// AccountPagingRequest 分页获取客服账号列表请求
|
||||||
|
type AccountPagingRequest struct {
|
||||||
|
Offset int `json:"offset"`
|
||||||
|
Limit int `json:"limit"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// AccountPaging 分页获取客服账号列表
|
||||||
|
// see https://developer.work.weixin.qq.com/document/path/94661
|
||||||
|
func (r *Client) AccountPaging(req *AccountPagingRequest) (*AccountListSchema, error) {
|
||||||
|
var (
|
||||||
|
accessToken string
|
||||||
|
err error
|
||||||
|
)
|
||||||
|
if accessToken, err = r.ctx.GetAccessToken(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
var response []byte
|
||||||
|
if response, err = util.PostJSON(fmt.Sprintf(accountListAddr, accessToken), req); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
result := &AccountListSchema{}
|
||||||
|
err = util.DecodeWithError(response, result, "AccountPaging")
|
||||||
|
return result, err
|
||||||
|
}
|
||||||
|
|
||||||
// AddContactWayOptions 获取客服账号链接
|
// AddContactWayOptions 获取客服账号链接
|
||||||
// 1.若scene非空,返回的客服链接开发者可拼接scene_param=SCENE_PARAM参数使用,用户进入会话事件会将SCENE_PARAM原样返回。其中SCENE_PARAM需要urlencode,且长度不能超过128字节。
|
// 1.若scene非空,返回的客服链接开发者可拼接scene_param=SCENE_PARAM参数使用,用户进入会话事件会将SCENE_PARAM原样返回。其中SCENE_PARAM需要urlencode,且长度不能超过128字节。
|
||||||
// 如 https://work.weixin.qq.com/kf/kfcbf8f8d07ac7215f?enc_scene=ENCGFSDF567DF&scene_param=a%3D1%26b%3D2
|
// 如 https://work.weixin.qq.com/kf/kfcbf8f8d07ac7215f?enc_scene=ENCGFSDF567DF&scene_param=a%3D1%26b%3D2
|
||||||
@@ -158,6 +187,7 @@ type AddContactWaySchema struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// AddContactWay 获取客服账号链接
|
// AddContactWay 获取客服账号链接
|
||||||
|
// see https://developer.work.weixin.qq.com/document/path/94665
|
||||||
func (r *Client) AddContactWay(options AddContactWayOptions) (info AddContactWaySchema, err error) {
|
func (r *Client) AddContactWay(options AddContactWayOptions) (info AddContactWaySchema, err error) {
|
||||||
var (
|
var (
|
||||||
accessToken string
|
accessToken string
|
||||||
|
|||||||
@@ -18,15 +18,17 @@ const (
|
|||||||
|
|
||||||
// ReceptionistOptions 添加接待人员请求参数
|
// ReceptionistOptions 添加接待人员请求参数
|
||||||
type ReceptionistOptions struct {
|
type ReceptionistOptions struct {
|
||||||
OpenKFID string `json:"open_kfid"` // 客服帐号ID
|
OpenKFID string `json:"open_kfid"` // 客服帐号ID
|
||||||
UserIDList []string `json:"userid_list"` // 接待人员userid列表。第三方应用填密文userid,即open_userid 可填充个数:1 ~ 100。超过100个需分批调用。
|
UserIDList []string `json:"userid_list"` // 接待人员userid列表。第三方应用填密文userid,即open_userid 可填充个数:1 ~ 100。超过100个需分批调用。
|
||||||
|
DepartmentIDList []int `json:"department_id_list"` // 接待人员部门id列表 可填充个数:0 ~ 100。超过100个需分批调用。
|
||||||
}
|
}
|
||||||
|
|
||||||
// ReceptionistSchema 添加接待人员响应内容
|
// ReceptionistSchema 添加接待人员响应内容
|
||||||
type ReceptionistSchema struct {
|
type ReceptionistSchema struct {
|
||||||
util.CommonError
|
util.CommonError
|
||||||
ResultList []struct {
|
ResultList []struct {
|
||||||
UserID string `json:"userid"`
|
UserID string `json:"userid"`
|
||||||
|
DepartmentID int `json:"department_id"`
|
||||||
util.CommonError
|
util.CommonError
|
||||||
} `json:"result_list"`
|
} `json:"result_list"`
|
||||||
}
|
}
|
||||||
@@ -79,8 +81,9 @@ func (r *Client) ReceptionistDel(options ReceptionistOptions) (info Receptionist
|
|||||||
type ReceptionistListSchema struct {
|
type ReceptionistListSchema struct {
|
||||||
util.CommonError
|
util.CommonError
|
||||||
ReceptionistList []struct {
|
ReceptionistList []struct {
|
||||||
UserID string `json:"userid"` // 接待人员的userid。第三方应用获取到的为密文userid,即open_userid
|
UserID string `json:"userid"` // 接待人员的userid。第三方应用获取到的为密文userid,即open_userid
|
||||||
Status int `json:"status"` // 接待人员的接待状态。0:接待中,1:停止接待。第三方应用需具有“管理帐号、分配会话和收发消息”权限才可获取
|
Status int `json:"status"` // 接待人员的接待状态。0:接待中,1:停止接待。第三方应用需具有“管理帐号、分配会话和收发消息”权限才可获取
|
||||||
|
DepartmentID int `json:"department_id"` // 接待人员部门的id
|
||||||
} `json:"servicer_list"`
|
} `json:"servicer_list"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package material
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
|
|
||||||
"github.com/silenceper/wechat/v2/util"
|
"github.com/silenceper/wechat/v2/util"
|
||||||
)
|
)
|
||||||
@@ -96,3 +97,54 @@ func (r *Client) UploadAttachment(filename string, mediaType string, attachmentT
|
|||||||
err = util.DecodeWithError(response, result, "UploadAttachment")
|
err = util.DecodeWithError(response, result, "UploadAttachment")
|
||||||
return result, err
|
return result, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// UploadTempFileFromReader 上传临时素材
|
||||||
|
// @see https://developer.work.weixin.qq.com/document/path/90253
|
||||||
|
// @mediaType 媒体文件类型,分别有图片(image)、语音(voice)、视频(video),普通文件(file)
|
||||||
|
func (r *Client) UploadTempFileFromReader(filename, mediaType string, reader io.Reader) (*UploadTempFileResponse, error) {
|
||||||
|
var (
|
||||||
|
accessToken string
|
||||||
|
err error
|
||||||
|
)
|
||||||
|
if accessToken, err = r.GetAccessToken(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
var byteData []byte
|
||||||
|
byteData, err = io.ReadAll(reader)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
var response []byte
|
||||||
|
if response, err = util.PostFileByStream("media", filename, fmt.Sprintf(uploadTempFile, accessToken, mediaType), byteData); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
result := &UploadTempFileResponse{}
|
||||||
|
err = util.DecodeWithError(response, result, "UploadTempFile")
|
||||||
|
return result, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// UploadAttachmentFromReader 上传附件资源
|
||||||
|
// @see https://developer.work.weixin.qq.com/document/path/95098
|
||||||
|
// @mediaType 媒体文件类型,分别有图片(image)、视频(video)、普通文件(file)
|
||||||
|
// @attachment_type 附件类型,不同的附件类型用于不同的场景。1:朋友圈;2:商品图册
|
||||||
|
func (r *Client) UploadAttachmentFromReader(filename, mediaType string, reader io.Reader, attachmentType int) (*UploadAttachmentResponse, error) {
|
||||||
|
var (
|
||||||
|
accessToken string
|
||||||
|
err error
|
||||||
|
)
|
||||||
|
if accessToken, err = r.GetAccessToken(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
var byteData []byte
|
||||||
|
byteData, err = io.ReadAll(reader)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
var response []byte
|
||||||
|
if response, err = util.PostFileByStream("media", filename, fmt.Sprintf(uploadAttachment, accessToken, mediaType, attachmentType), byteData); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
result := &UploadAttachmentResponse{}
|
||||||
|
err = util.DecodeWithError(response, result, "UploadAttachment")
|
||||||
|
return result, err
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user