customer_acquisition.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. package externalcontact
  2. import (
  3. "fmt"
  4. "github.com/silenceper/wechat/v2/util"
  5. )
  6. const (
  7. // listLinkUrl 获取获客链接列表
  8. listLinkURL = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/customer_acquisition/list_link?access_token=%s"
  9. // getCustomerAcquisition 获取获客链接详情
  10. getCustomerAcquisitionURL = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/customer_acquisition/get?access_token=%s"
  11. // createCustomerAcquisitionLink 创建获客链接
  12. createCustomerAcquisitionLinkURL = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/customer_acquisition/create_link?access_token=%s"
  13. // updateCustomerAcquisitionLink 编辑获客链接
  14. updateCustomerAcquisitionLinkURL = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/customer_acquisition/update_link?access_token=%s"
  15. // deleteCustomerAcquisitionLink 删除获客链接
  16. deleteCustomerAcquisitionLinkURL = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/customer_acquisition/delete_link?access_token=%s"
  17. // getCustomerInfoWithCustomerAcquisitionLinkURL 获取由获客链接添加的客户信息
  18. getCustomerInfoWithCustomerAcquisitionLinkURL = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/customer_acquisition/customer?access_token=%s"
  19. // customerAcquisitionQuota 查询剩余使用量
  20. customerAcquisitionQuotaURL = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/customer_acquisition_quota?access_token=%s"
  21. // customerAcquisitionStatistic 查询链接使用详情
  22. customerAcquisitionStatisticURL = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/customer_acquisition/statistic?access_token=%s"
  23. )
  24. type (
  25. // ListLinkRequest 获取获客链接列表请求
  26. ListLinkRequest struct {
  27. Limit int `json:"limit"`
  28. Cursor string `json:"cursor"`
  29. }
  30. // ListLinkResponse 获取获客链接列表响应
  31. ListLinkResponse struct {
  32. util.CommonError
  33. LinkIDList []string `json:"link_id_list"`
  34. NextCursor string `json:"next_cursor"`
  35. }
  36. )
  37. // ListLink 获客助手--获取获客链接列表
  38. // see https://developer.work.weixin.qq.com/document/path/97297
  39. func (r *Client) ListLink(req *ListLinkRequest) (*ListLinkResponse, error) {
  40. var (
  41. accessToken string
  42. err error
  43. )
  44. if accessToken, err = r.GetAccessToken(); err != nil {
  45. return nil, err
  46. }
  47. var response []byte
  48. if response, err = util.PostJSON(fmt.Sprintf(listLinkURL, accessToken), req); err != nil {
  49. return nil, err
  50. }
  51. result := &ListLinkResponse{}
  52. if err = util.DecodeWithError(response, result, "ListLink"); err != nil {
  53. return nil, err
  54. }
  55. return result, nil
  56. }
  57. type (
  58. // GetCustomerAcquisitionRequest 获取获客链接详情请求
  59. GetCustomerAcquisitionRequest struct {
  60. LinkID string `json:"link_id"`
  61. }
  62. // GetCustomerAcquisitionResponse 获取获客链接详情响应
  63. GetCustomerAcquisitionResponse struct {
  64. util.CommonError
  65. Link Link `json:"link"`
  66. Range CustomerAcquisitionRange `json:"range"`
  67. SkipVerify bool `json:"skip_verify"`
  68. }
  69. // Link 获客链接
  70. Link struct {
  71. LinkID string `json:"link_id"`
  72. LinkName string `json:"link_name"`
  73. URL string `json:"url"`
  74. CreateTime int64 `json:"create_time"`
  75. }
  76. // CustomerAcquisitionRange 该获客链接使用范围
  77. CustomerAcquisitionRange struct {
  78. UserList []string `json:"user_list"`
  79. DepartmentList []int64 `json:"department_list"`
  80. }
  81. )
  82. // GetCustomerAcquisition 获客助手--获取获客链接详情
  83. // see https://developer.work.weixin.qq.com/document/path/97297
  84. func (r *Client) GetCustomerAcquisition(req *GetCustomerAcquisitionRequest) (*GetCustomerAcquisitionResponse, error) {
  85. var (
  86. accessToken string
  87. err error
  88. )
  89. if accessToken, err = r.GetAccessToken(); err != nil {
  90. return nil, err
  91. }
  92. var response []byte
  93. if response, err = util.PostJSON(fmt.Sprintf(getCustomerAcquisitionURL, accessToken), req); err != nil {
  94. return nil, err
  95. }
  96. result := &GetCustomerAcquisitionResponse{}
  97. if err = util.DecodeWithError(response, result, "GetCustomerAcquisition"); err != nil {
  98. return nil, err
  99. }
  100. return result, nil
  101. }
  102. type (
  103. // CreateCustomerAcquisitionLinkRequest 创建获客链接请求
  104. CreateCustomerAcquisitionLinkRequest struct {
  105. LinkName string `json:"link_name"`
  106. Range CustomerAcquisitionRange `json:"range"`
  107. SkipVerify bool `json:"skip_verify"`
  108. }
  109. // CreateCustomerAcquisitionLinkResponse 创建获客链接响应
  110. CreateCustomerAcquisitionLinkResponse struct {
  111. util.CommonError
  112. Link Link `json:"link"`
  113. }
  114. )
  115. // CreateCustomerAcquisitionLink 获客助手--创建获客链接
  116. // see https://developer.work.weixin.qq.com/document/path/97297
  117. func (r *Client) CreateCustomerAcquisitionLink(req *CreateCustomerAcquisitionLinkRequest) (*CreateCustomerAcquisitionLinkResponse, error) {
  118. var (
  119. accessToken string
  120. err error
  121. )
  122. if accessToken, err = r.GetAccessToken(); err != nil {
  123. return nil, err
  124. }
  125. var response []byte
  126. if response, err = util.PostJSON(fmt.Sprintf(createCustomerAcquisitionLinkURL, accessToken), req); err != nil {
  127. return nil, err
  128. }
  129. result := &CreateCustomerAcquisitionLinkResponse{}
  130. if err = util.DecodeWithError(response, result, "CreateCustomerAcquisitionLink"); err != nil {
  131. return nil, err
  132. }
  133. return result, nil
  134. }
  135. type (
  136. // UpdateCustomerAcquisitionLinkRequest 编辑获客链接请求
  137. UpdateCustomerAcquisitionLinkRequest struct {
  138. LinkID string `json:"link_id"`
  139. LinkName string `json:"link_name"`
  140. Range CustomerAcquisitionRange `json:"range"`
  141. SkipVerify bool `json:"skip_verify"`
  142. }
  143. // UpdateCustomerAcquisitionLinkResponse 编辑获客链接响应
  144. UpdateCustomerAcquisitionLinkResponse struct {
  145. util.CommonError
  146. }
  147. )
  148. // UpdateCustomerAcquisitionLink 获客助手--编辑获客链接
  149. // see https://developer.work.weixin.qq.com/document/path/97297
  150. func (r *Client) UpdateCustomerAcquisitionLink(req *UpdateCustomerAcquisitionLinkRequest) (*UpdateCustomerAcquisitionLinkResponse, error) {
  151. var (
  152. accessToken string
  153. err error
  154. )
  155. if accessToken, err = r.GetAccessToken(); err != nil {
  156. return nil, err
  157. }
  158. var response []byte
  159. if response, err = util.PostJSON(fmt.Sprintf(updateCustomerAcquisitionLinkURL, accessToken), req); err != nil {
  160. return nil, err
  161. }
  162. result := &UpdateCustomerAcquisitionLinkResponse{}
  163. if err = util.DecodeWithError(response, result, "UpdateCustomerAcquisitionLink"); err != nil {
  164. return nil, err
  165. }
  166. return result, nil
  167. }
  168. type (
  169. // DeleteCustomerAcquisitionLinkRequest 删除获客链接请求
  170. DeleteCustomerAcquisitionLinkRequest struct {
  171. LinkID string `json:"link_id"`
  172. }
  173. // DeleteCustomerAcquisitionLinkResponse 删除获客链接响应
  174. DeleteCustomerAcquisitionLinkResponse struct {
  175. util.CommonError
  176. }
  177. )
  178. // DeleteCustomerAcquisitionLink 获客助手--删除获客链接
  179. // see https://developer.work.weixin.qq.com/document/path/97297
  180. func (r *Client) DeleteCustomerAcquisitionLink(req *DeleteCustomerAcquisitionLinkRequest) (*DeleteCustomerAcquisitionLinkResponse, error) {
  181. var (
  182. accessToken string
  183. err error
  184. )
  185. if accessToken, err = r.GetAccessToken(); err != nil {
  186. return nil, err
  187. }
  188. var response []byte
  189. if response, err = util.PostJSON(fmt.Sprintf(deleteCustomerAcquisitionLinkURL, accessToken), req); err != nil {
  190. return nil, err
  191. }
  192. result := &DeleteCustomerAcquisitionLinkResponse{}
  193. if err = util.DecodeWithError(response, result, "DeleteCustomerAcquisitionLink"); err != nil {
  194. return nil, err
  195. }
  196. return result, nil
  197. }
  198. type (
  199. // GetCustomerInfoWithCustomerAcquisitionLinkRequest 获取由获客链接添加的客户信息请求
  200. GetCustomerInfoWithCustomerAcquisitionLinkRequest struct {
  201. LinkID string `json:"link_id"`
  202. Limit int64 `json:"limit"`
  203. Cursor string `json:"cursor"`
  204. }
  205. // GetCustomerInfoWithCustomerAcquisitionLinkResponse 获取由获客链接添加的客户信息响应
  206. GetCustomerInfoWithCustomerAcquisitionLinkResponse struct {
  207. util.CommonError
  208. CustomerList []CustomerList `json:"customer_list"`
  209. NextCursor string `json:"next_cursor"`
  210. }
  211. // CustomerList 客户列表
  212. CustomerList struct {
  213. ExternalUserid string `json:"external_userid"`
  214. Userid string `json:"userid"`
  215. ChatStatus int64 `json:"chat_status"`
  216. State string `json:"state"`
  217. }
  218. )
  219. // GetCustomerInfoWithCustomerAcquisitionLink 获客助手--获取由获客链接添加的客户信息
  220. // see https://developer.work.weixin.qq.com/document/path/97298
  221. func (r *Client) GetCustomerInfoWithCustomerAcquisitionLink(req *GetCustomerInfoWithCustomerAcquisitionLinkRequest) (*GetCustomerInfoWithCustomerAcquisitionLinkResponse, error) {
  222. var (
  223. accessToken string
  224. err error
  225. )
  226. if accessToken, err = r.GetAccessToken(); err != nil {
  227. return nil, err
  228. }
  229. var response []byte
  230. if response, err = util.PostJSON(fmt.Sprintf(getCustomerInfoWithCustomerAcquisitionLinkURL, accessToken), req); err != nil {
  231. return nil, err
  232. }
  233. result := &GetCustomerInfoWithCustomerAcquisitionLinkResponse{}
  234. if err = util.DecodeWithError(response, result, "GetCustomerInfoWithCustomerAcquisitionLink"); err != nil {
  235. return nil, err
  236. }
  237. return result, nil
  238. }
  239. type (
  240. // CustomerAcquisitionQuotaResponse 查询剩余使用量响应
  241. CustomerAcquisitionQuotaResponse struct {
  242. util.CommonError
  243. Total int64 `json:"total"`
  244. Balance int64 `json:"balance"`
  245. QuotaList []QuotaList `json:"quota_list"`
  246. }
  247. // QuotaList 额度
  248. QuotaList struct {
  249. ExpireDate int64 `json:"expire_date"`
  250. Balance int64 `json:"balance"`
  251. }
  252. )
  253. // CustomerAcquisitionQuota 获客助手额度管理与使用统计--查询剩余使用量
  254. // see https://developer.work.weixin.qq.com/document/path/97375
  255. func (r *Client) CustomerAcquisitionQuota() (*CustomerAcquisitionQuotaResponse, error) {
  256. var (
  257. accessToken string
  258. err error
  259. )
  260. if accessToken, err = r.GetAccessToken(); err != nil {
  261. return nil, err
  262. }
  263. var response []byte
  264. if response, err = util.HTTPGet((fmt.Sprintf(customerAcquisitionQuotaURL, accessToken))); err != nil {
  265. return nil, err
  266. }
  267. result := &CustomerAcquisitionQuotaResponse{}
  268. if err = util.DecodeWithError(response, result, "CustomerAcquisitionQuota"); err != nil {
  269. return nil, err
  270. }
  271. return result, nil
  272. }
  273. type (
  274. // CustomerAcquisitionStatisticRequest 查询链接使用详情请求
  275. CustomerAcquisitionStatisticRequest struct {
  276. LinkID string `json:"link_id"`
  277. StartTime int64 `json:"start_time"`
  278. EndTime int64 `json:"end_time"`
  279. }
  280. // CustomerAcquisitionStatisticResponse 查询链接使用详情响应
  281. CustomerAcquisitionStatisticResponse struct {
  282. util.CommonError
  283. ClickLinkCustomerCnt int64 `json:"click_link_customer_cnt"`
  284. NewCustomerCnt int64 `json:"new_customer_cnt"`
  285. }
  286. )
  287. // CustomerAcquisitionStatistic 获客助手额度管理与使用统计--查询链接使用详情
  288. // see https://developer.work.weixin.qq.com/document/path/97375
  289. func (r *Client) CustomerAcquisitionStatistic(req *CustomerAcquisitionStatisticRequest) (*CustomerAcquisitionStatisticResponse, error) {
  290. var (
  291. accessToken string
  292. err error
  293. )
  294. if accessToken, err = r.GetAccessToken(); err != nil {
  295. return nil, err
  296. }
  297. var response []byte
  298. if response, err = util.PostJSON(fmt.Sprintf(customerAcquisitionStatisticURL, accessToken), req); err != nil {
  299. return nil, err
  300. }
  301. result := &CustomerAcquisitionStatisticResponse{}
  302. if err = util.DecodeWithError(response, result, "CustomerAcquisitionStatistic"); err != nil {
  303. return nil, err
  304. }
  305. return result, nil
  306. }