customer_acquisition.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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. err = util.DecodeWithError(response, result, "ListLink")
  53. return result, err
  54. }
  55. type (
  56. // GetCustomerAcquisitionRequest 获取获客链接详情请求
  57. GetCustomerAcquisitionRequest struct {
  58. LinkID string `json:"link_id"`
  59. }
  60. // GetCustomerAcquisitionResponse 获取获客链接详情响应
  61. GetCustomerAcquisitionResponse struct {
  62. util.CommonError
  63. Link Link `json:"link"`
  64. Range CustomerAcquisitionRange `json:"range"`
  65. SkipVerify bool `json:"skip_verify"`
  66. }
  67. // Link 获客链接
  68. Link struct {
  69. LinkID string `json:"link_id"`
  70. LinkName string `json:"link_name"`
  71. URL string `json:"url"`
  72. CreateTime int64 `json:"create_time"`
  73. }
  74. // CustomerAcquisitionRange 该获客链接使用范围
  75. CustomerAcquisitionRange struct {
  76. UserList []string `json:"user_list"`
  77. DepartmentList []int64 `json:"department_list"`
  78. }
  79. )
  80. // GetCustomerAcquisition 获客助手--获取获客链接详情
  81. // see https://developer.work.weixin.qq.com/document/path/97297
  82. func (r *Client) GetCustomerAcquisition(req *GetCustomerAcquisitionRequest) (*GetCustomerAcquisitionResponse, error) {
  83. var (
  84. accessToken string
  85. err error
  86. )
  87. if accessToken, err = r.GetAccessToken(); err != nil {
  88. return nil, err
  89. }
  90. var response []byte
  91. if response, err = util.PostJSON(fmt.Sprintf(getCustomerAcquisitionURL, accessToken), req); err != nil {
  92. return nil, err
  93. }
  94. result := &GetCustomerAcquisitionResponse{}
  95. err = util.DecodeWithError(response, result, "GetCustomerAcquisition")
  96. return result, err
  97. }
  98. type (
  99. // CreateCustomerAcquisitionLinkRequest 创建获客链接请求
  100. CreateCustomerAcquisitionLinkRequest struct {
  101. LinkName string `json:"link_name"`
  102. Range CustomerAcquisitionRange `json:"range"`
  103. SkipVerify bool `json:"skip_verify"`
  104. }
  105. // CreateCustomerAcquisitionLinkResponse 创建获客链接响应
  106. CreateCustomerAcquisitionLinkResponse struct {
  107. util.CommonError
  108. Link Link `json:"link"`
  109. }
  110. )
  111. // CreateCustomerAcquisitionLink 获客助手--创建获客链接
  112. // see https://developer.work.weixin.qq.com/document/path/97297
  113. func (r *Client) CreateCustomerAcquisitionLink(req *CreateCustomerAcquisitionLinkRequest) (*CreateCustomerAcquisitionLinkResponse, error) {
  114. var (
  115. accessToken string
  116. err error
  117. )
  118. if accessToken, err = r.GetAccessToken(); err != nil {
  119. return nil, err
  120. }
  121. var response []byte
  122. if response, err = util.PostJSON(fmt.Sprintf(createCustomerAcquisitionLinkURL, accessToken), req); err != nil {
  123. return nil, err
  124. }
  125. result := &CreateCustomerAcquisitionLinkResponse{}
  126. err = util.DecodeWithError(response, result, "CreateCustomerAcquisitionLink")
  127. return result, err
  128. }
  129. type (
  130. // UpdateCustomerAcquisitionLinkRequest 编辑获客链接请求
  131. UpdateCustomerAcquisitionLinkRequest struct {
  132. LinkID string `json:"link_id"`
  133. LinkName string `json:"link_name"`
  134. Range CustomerAcquisitionRange `json:"range"`
  135. SkipVerify bool `json:"skip_verify"`
  136. }
  137. // UpdateCustomerAcquisitionLinkResponse 编辑获客链接响应
  138. UpdateCustomerAcquisitionLinkResponse struct {
  139. util.CommonError
  140. }
  141. )
  142. // UpdateCustomerAcquisitionLink 获客助手--编辑获客链接
  143. // see https://developer.work.weixin.qq.com/document/path/97297
  144. func (r *Client) UpdateCustomerAcquisitionLink(req *UpdateCustomerAcquisitionLinkRequest) (*UpdateCustomerAcquisitionLinkResponse, error) {
  145. var (
  146. accessToken string
  147. err error
  148. )
  149. if accessToken, err = r.GetAccessToken(); err != nil {
  150. return nil, err
  151. }
  152. var response []byte
  153. if response, err = util.PostJSON(fmt.Sprintf(updateCustomerAcquisitionLinkURL, accessToken), req); err != nil {
  154. return nil, err
  155. }
  156. result := &UpdateCustomerAcquisitionLinkResponse{}
  157. err = util.DecodeWithError(response, result, "UpdateCustomerAcquisitionLink")
  158. return result, err
  159. }
  160. type (
  161. // DeleteCustomerAcquisitionLinkRequest 删除获客链接请求
  162. DeleteCustomerAcquisitionLinkRequest struct {
  163. LinkID string `json:"link_id"`
  164. }
  165. // DeleteCustomerAcquisitionLinkResponse 删除获客链接响应
  166. DeleteCustomerAcquisitionLinkResponse struct {
  167. util.CommonError
  168. }
  169. )
  170. // DeleteCustomerAcquisitionLink 获客助手--删除获客链接
  171. // see https://developer.work.weixin.qq.com/document/path/97297
  172. func (r *Client) DeleteCustomerAcquisitionLink(req *DeleteCustomerAcquisitionLinkRequest) (*DeleteCustomerAcquisitionLinkResponse, error) {
  173. var (
  174. accessToken string
  175. err error
  176. )
  177. if accessToken, err = r.GetAccessToken(); err != nil {
  178. return nil, err
  179. }
  180. var response []byte
  181. if response, err = util.PostJSON(fmt.Sprintf(deleteCustomerAcquisitionLinkURL, accessToken), req); err != nil {
  182. return nil, err
  183. }
  184. result := &DeleteCustomerAcquisitionLinkResponse{}
  185. err = util.DecodeWithError(response, result, "DeleteCustomerAcquisitionLink")
  186. return result, err
  187. }
  188. type (
  189. // GetCustomerInfoWithCustomerAcquisitionLinkRequest 获取由获客链接添加的客户信息请求
  190. GetCustomerInfoWithCustomerAcquisitionLinkRequest struct {
  191. LinkID string `json:"link_id"`
  192. Limit int64 `json:"limit"`
  193. Cursor string `json:"cursor"`
  194. }
  195. // GetCustomerInfoWithCustomerAcquisitionLinkResponse 获取由获客链接添加的客户信息响应
  196. GetCustomerInfoWithCustomerAcquisitionLinkResponse struct {
  197. util.CommonError
  198. CustomerList []CustomerList `json:"customer_list"`
  199. NextCursor string `json:"next_cursor"`
  200. }
  201. // CustomerList 客户列表
  202. CustomerList struct {
  203. ExternalUserid string `json:"external_userid"`
  204. Userid string `json:"userid"`
  205. ChatStatus int64 `json:"chat_status"`
  206. State string `json:"state"`
  207. }
  208. )
  209. // GetCustomerInfoWithCustomerAcquisitionLink 获客助手--获取由获客链接添加的客户信息
  210. // see https://developer.work.weixin.qq.com/document/path/97298
  211. func (r *Client) GetCustomerInfoWithCustomerAcquisitionLink(req *GetCustomerInfoWithCustomerAcquisitionLinkRequest) (*GetCustomerInfoWithCustomerAcquisitionLinkResponse, error) {
  212. var (
  213. accessToken string
  214. err error
  215. )
  216. if accessToken, err = r.GetAccessToken(); err != nil {
  217. return nil, err
  218. }
  219. var response []byte
  220. if response, err = util.PostJSON(fmt.Sprintf(getCustomerInfoWithCustomerAcquisitionLinkURL, accessToken), req); err != nil {
  221. return nil, err
  222. }
  223. result := &GetCustomerInfoWithCustomerAcquisitionLinkResponse{}
  224. err = util.DecodeWithError(response, result, "GetCustomerInfoWithCustomerAcquisitionLink")
  225. return result, err
  226. }
  227. type (
  228. // CustomerAcquisitionQuotaResponse 查询剩余使用量响应
  229. CustomerAcquisitionQuotaResponse struct {
  230. util.CommonError
  231. Total int64 `json:"total"`
  232. Balance int64 `json:"balance"`
  233. QuotaList []QuotaList `json:"quota_list"`
  234. }
  235. // QuotaList 额度
  236. QuotaList struct {
  237. ExpireDate int64 `json:"expire_date"`
  238. Balance int64 `json:"balance"`
  239. }
  240. )
  241. // CustomerAcquisitionQuota 获客助手额度管理与使用统计--查询剩余使用量
  242. // see https://developer.work.weixin.qq.com/document/path/97375
  243. func (r *Client) CustomerAcquisitionQuota() (*CustomerAcquisitionQuotaResponse, error) {
  244. var (
  245. accessToken string
  246. err error
  247. )
  248. if accessToken, err = r.GetAccessToken(); err != nil {
  249. return nil, err
  250. }
  251. var response []byte
  252. if response, err = util.HTTPGet(fmt.Sprintf(customerAcquisitionQuotaURL, accessToken)); err != nil {
  253. return nil, err
  254. }
  255. result := &CustomerAcquisitionQuotaResponse{}
  256. err = util.DecodeWithError(response, result, "CustomerAcquisitionQuota")
  257. return result, err
  258. }
  259. type (
  260. // CustomerAcquisitionStatisticRequest 查询链接使用详情请求
  261. CustomerAcquisitionStatisticRequest struct {
  262. LinkID string `json:"link_id"`
  263. StartTime int64 `json:"start_time"`
  264. EndTime int64 `json:"end_time"`
  265. }
  266. // CustomerAcquisitionStatisticResponse 查询链接使用详情响应
  267. CustomerAcquisitionStatisticResponse struct {
  268. util.CommonError
  269. ClickLinkCustomerCnt int64 `json:"click_link_customer_cnt"`
  270. NewCustomerCnt int64 `json:"new_customer_cnt"`
  271. }
  272. )
  273. // CustomerAcquisitionStatistic 获客助手额度管理与使用统计--查询链接使用详情
  274. // see https://developer.work.weixin.qq.com/document/path/97375
  275. func (r *Client) CustomerAcquisitionStatistic(req *CustomerAcquisitionStatisticRequest) (*CustomerAcquisitionStatisticResponse, error) {
  276. var (
  277. accessToken string
  278. err error
  279. )
  280. if accessToken, err = r.GetAccessToken(); err != nil {
  281. return nil, err
  282. }
  283. var response []byte
  284. if response, err = util.PostJSON(fmt.Sprintf(customerAcquisitionStatisticURL, accessToken), req); err != nil {
  285. return nil, err
  286. }
  287. result := &CustomerAcquisitionStatisticResponse{}
  288. err = util.DecodeWithError(response, result, "CustomerAcquisitionStatistic")
  289. return result, err
  290. }