account.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. package kf
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "github.com/silenceper/wechat/v2/util"
  6. )
  7. const (
  8. // 添加客服账号
  9. accountAddAddr = "https://qyapi.weixin.qq.com/cgi-bin/kf/account/add?access_token=%s"
  10. // 删除客服账号
  11. accountDelAddr = "https://qyapi.weixin.qq.com/cgi-bin/kf/account/del?access_token=%s"
  12. // 修改客服账号
  13. accountUpdateAddr = "https://qyapi.weixin.qq.com/cgi-bin/kf/account/update?access_token=%s"
  14. // 获取客服账号列表
  15. accountListAddr = "https://qyapi.weixin.qq.com/cgi-bin/kf/account/list?access_token=%s"
  16. // 获取客服账号链接
  17. addContactWayAddr = "https://qyapi.weixin.qq.com/cgi-bin/kf/add_contact_way?access_token=%s"
  18. )
  19. // AccountAddOptions 添加客服账号请求参数
  20. type AccountAddOptions struct {
  21. Name string `json:"name"` // 客服帐号名称, 不多于16个字符
  22. MediaID string `json:"media_id"` // 客服头像临时素材。可以调用上传临时素材接口获取, 不多于128个字节
  23. }
  24. // AccountAddSchema 添加客服账号响应内容
  25. type AccountAddSchema struct {
  26. util.CommonError
  27. OpenKFID string `json:"open_kfid"` // 新创建的客服张号ID
  28. }
  29. // AccountAdd 添加客服账号
  30. // see https://developer.work.weixin.qq.com/document/path/94662
  31. func (r *Client) AccountAdd(options AccountAddOptions) (info AccountAddSchema, err error) {
  32. var (
  33. accessToken string
  34. data []byte
  35. )
  36. if accessToken, err = r.ctx.GetAccessToken(); err != nil {
  37. return
  38. }
  39. if data, err = util.PostJSON(fmt.Sprintf(accountAddAddr, accessToken), options); err != nil {
  40. return
  41. }
  42. if err = json.Unmarshal(data, &info); err != nil {
  43. return
  44. }
  45. if info.ErrCode != 0 {
  46. return info, NewSDKErr(info.ErrCode, info.ErrMsg)
  47. }
  48. return info, nil
  49. }
  50. // AccountDelOptions 删除客服账号请求参数
  51. type AccountDelOptions struct {
  52. OpenKFID string `json:"open_kfid"` // 客服帐号ID, 不多于64字节
  53. }
  54. // AccountDel 删除客服账号
  55. // see https://developer.work.weixin.qq.com/document/path/94663
  56. func (r *Client) AccountDel(options AccountDelOptions) (info util.CommonError, err error) {
  57. var (
  58. accessToken string
  59. data []byte
  60. )
  61. if accessToken, err = r.ctx.GetAccessToken(); err != nil {
  62. return
  63. }
  64. if data, err = util.PostJSON(fmt.Sprintf(accountDelAddr, accessToken), options); err != nil {
  65. return
  66. }
  67. if err = json.Unmarshal(data, &info); err != nil {
  68. return
  69. }
  70. if info.ErrCode != 0 {
  71. return info, NewSDKErr(info.ErrCode, info.ErrMsg)
  72. }
  73. return info, nil
  74. }
  75. // AccountUpdateOptions 修改客服账号请求参数
  76. type AccountUpdateOptions struct {
  77. OpenKFID string `json:"open_kfid"` // 客服帐号ID, 不多于64字节
  78. Name string `json:"name"` // 客服帐号名称, 不多于16个字符
  79. MediaID string `json:"media_id"` // 客服头像临时素材。可以调用上传临时素材接口获取, 不多于128个字节
  80. }
  81. // AccountUpdate 修改客服账号
  82. // see https://developer.work.weixin.qq.com/document/path/94664
  83. func (r *Client) AccountUpdate(options AccountUpdateOptions) (info util.CommonError, err error) {
  84. var (
  85. accessToken string
  86. data []byte
  87. )
  88. if accessToken, err = r.ctx.GetAccessToken(); err != nil {
  89. return
  90. }
  91. if data, err = util.PostJSON(fmt.Sprintf(accountUpdateAddr, accessToken), options); err != nil {
  92. return
  93. }
  94. if err = json.Unmarshal(data, &info); err != nil {
  95. return
  96. }
  97. if info.ErrCode != 0 {
  98. return info, NewSDKErr(info.ErrCode, info.ErrMsg)
  99. }
  100. return info, nil
  101. }
  102. // AccountInfoSchema 客服详情
  103. type AccountInfoSchema struct {
  104. OpenKFID string `json:"open_kfid"` // 客服帐号ID
  105. Name string `json:"name"` // 客服帐号名称
  106. Avatar string `json:"avatar"` // 客服头像URL
  107. ManagePrivilege bool `json:"manage_privilege"` // 当前调用接口的应用身份,是否有该客服账号的管理权限(编辑客服账号信息、分配会话和收发消息)
  108. }
  109. // AccountListSchema 获取客服账号列表响应内容
  110. type AccountListSchema struct {
  111. util.CommonError
  112. AccountList []AccountInfoSchema `json:"account_list"` // 客服账号列表
  113. }
  114. // AccountList 获取客服账号列表
  115. func (r *Client) AccountList() (info AccountListSchema, err error) {
  116. var (
  117. accessToken string
  118. data []byte
  119. )
  120. if accessToken, err = r.ctx.GetAccessToken(); err != nil {
  121. return
  122. }
  123. if data, err = util.HTTPGet(fmt.Sprintf(accountListAddr, accessToken)); err != nil {
  124. return
  125. }
  126. if err = json.Unmarshal(data, &info); err != nil {
  127. return
  128. }
  129. if info.ErrCode != 0 {
  130. return info, NewSDKErr(info.ErrCode, info.ErrMsg)
  131. }
  132. return info, nil
  133. }
  134. // AccountPagingRequest 分页获取客服账号列表请求
  135. type AccountPagingRequest struct {
  136. Offset int `json:"offset"`
  137. Limit int `json:"limit"`
  138. }
  139. // AccountPaging 分页获取客服账号列表
  140. // see https://developer.work.weixin.qq.com/document/path/94661
  141. func (r *Client) AccountPaging(req *AccountPagingRequest) (*AccountListSchema, error) {
  142. var (
  143. accessToken string
  144. err error
  145. )
  146. if accessToken, err = r.ctx.GetAccessToken(); err != nil {
  147. return nil, err
  148. }
  149. var response []byte
  150. if response, err = util.PostJSON(fmt.Sprintf(accountListAddr, accessToken), req); err != nil {
  151. return nil, err
  152. }
  153. result := &AccountListSchema{}
  154. err = util.DecodeWithError(response, result, "AccountPaging")
  155. return result, err
  156. }
  157. // AddContactWayOptions 获取客服账号链接
  158. // 1.若scene非空,返回的客服链接开发者可拼接scene_param=SCENE_PARAM参数使用,用户进入会话事件会将SCENE_PARAM原样返回。其中SCENE_PARAM需要urlencode,且长度不能超过128字节。
  159. // 如 https://work.weixin.qq.com/kf/kfcbf8f8d07ac7215f?enc_scene=ENCGFSDF567DF&scene_param=a%3D1%26b%3D2
  160. // 2.历史调用接口返回的客服链接(包含encScene=XXX参数),不支持scene_param参数。
  161. // 3.返回的客服链接,不能修改或复制参数到其他链接使用。否则进入会话事件参数校验不通过,导致无法回调。
  162. type AddContactWayOptions struct {
  163. OpenKFID string `json:"open_kfid"` // 客服帐号ID, 不多于64字节
  164. Scene string `json:"scene"` // 场景值,字符串类型,由开发者自定义, 不多于32字节, 字符串取值范围(正则表达式):[0-9a-zA-Z_-]*
  165. }
  166. // AddContactWaySchema 获取客服账号链接响应内容
  167. type AddContactWaySchema struct {
  168. util.CommonError
  169. URL string `json:"url"` // 客服链接,开发者可将该链接嵌入到H5页面中,用户点击链接即可向对应的微信客服帐号发起咨询。开发者也可根据该url自行生成需要的二维码图片
  170. }
  171. // AddContactWay 获取客服账号链接
  172. // see https://developer.work.weixin.qq.com/document/path/94665
  173. func (r *Client) AddContactWay(options AddContactWayOptions) (info AddContactWaySchema, err error) {
  174. var (
  175. accessToken string
  176. data []byte
  177. )
  178. if accessToken, err = r.ctx.GetAccessToken(); err != nil {
  179. return
  180. }
  181. if data, err = util.PostJSON(fmt.Sprintf(addContactWayAddr, accessToken), options); err != nil {
  182. return
  183. }
  184. if err = json.Unmarshal(data, &info); err != nil {
  185. return
  186. }
  187. if info.ErrCode != 0 {
  188. return info, NewSDKErr(info.ErrCode, info.ErrMsg)
  189. }
  190. return info, nil
  191. }