customer_acquisition.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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. // customerAcquisitionGetChatInfo 获取成员多次收消息详情
  24. customerAcquisitionGetChatInfoURL = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/customer_acquisition/get_chat_info?access_token=%s"
  25. )
  26. type (
  27. // ListLinkRequest 获取获客链接列表请求
  28. ListLinkRequest struct {
  29. Limit int `json:"limit"`
  30. Cursor string `json:"cursor"`
  31. }
  32. // ListLinkResponse 获取获客链接列表响应
  33. ListLinkResponse struct {
  34. util.CommonError
  35. LinkIDList []string `json:"link_id_list"`
  36. NextCursor string `json:"next_cursor"`
  37. }
  38. )
  39. // ListLink 获客助手--获取获客链接列表
  40. // see https://developer.work.weixin.qq.com/document/path/97297
  41. func (r *Client) ListLink(req *ListLinkRequest) (*ListLinkResponse, error) {
  42. var (
  43. accessToken string
  44. err error
  45. )
  46. if accessToken, err = r.GetAccessToken(); err != nil {
  47. return nil, err
  48. }
  49. var response []byte
  50. if response, err = util.PostJSON(fmt.Sprintf(listLinkURL, accessToken), req); err != nil {
  51. return nil, err
  52. }
  53. result := &ListLinkResponse{}
  54. err = util.DecodeWithError(response, result, "ListLink")
  55. return result, err
  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. err = util.DecodeWithError(response, result, "GetCustomerAcquisition")
  98. return result, err
  99. }
  100. type (
  101. // CreateCustomerAcquisitionLinkRequest 创建获客链接请求
  102. CreateCustomerAcquisitionLinkRequest struct {
  103. LinkName string `json:"link_name"`
  104. Range CustomerAcquisitionRange `json:"range"`
  105. SkipVerify bool `json:"skip_verify"`
  106. }
  107. // CreateCustomerAcquisitionLinkResponse 创建获客链接响应
  108. CreateCustomerAcquisitionLinkResponse struct {
  109. util.CommonError
  110. Link Link `json:"link"`
  111. }
  112. )
  113. // CreateCustomerAcquisitionLink 获客助手--创建获客链接
  114. // see https://developer.work.weixin.qq.com/document/path/97297
  115. func (r *Client) CreateCustomerAcquisitionLink(req *CreateCustomerAcquisitionLinkRequest) (*CreateCustomerAcquisitionLinkResponse, error) {
  116. var (
  117. accessToken string
  118. err error
  119. )
  120. if accessToken, err = r.GetAccessToken(); err != nil {
  121. return nil, err
  122. }
  123. var response []byte
  124. if response, err = util.PostJSON(fmt.Sprintf(createCustomerAcquisitionLinkURL, accessToken), req); err != nil {
  125. return nil, err
  126. }
  127. result := &CreateCustomerAcquisitionLinkResponse{}
  128. err = util.DecodeWithError(response, result, "CreateCustomerAcquisitionLink")
  129. return result, err
  130. }
  131. type (
  132. // UpdateCustomerAcquisitionLinkRequest 编辑获客链接请求
  133. UpdateCustomerAcquisitionLinkRequest struct {
  134. LinkID string `json:"link_id"`
  135. LinkName string `json:"link_name"`
  136. Range CustomerAcquisitionRange `json:"range"`
  137. SkipVerify bool `json:"skip_verify"`
  138. }
  139. // UpdateCustomerAcquisitionLinkResponse 编辑获客链接响应
  140. UpdateCustomerAcquisitionLinkResponse struct {
  141. util.CommonError
  142. }
  143. )
  144. // UpdateCustomerAcquisitionLink 获客助手--编辑获客链接
  145. // see https://developer.work.weixin.qq.com/document/path/97297
  146. func (r *Client) UpdateCustomerAcquisitionLink(req *UpdateCustomerAcquisitionLinkRequest) (*UpdateCustomerAcquisitionLinkResponse, error) {
  147. var (
  148. accessToken string
  149. err error
  150. )
  151. if accessToken, err = r.GetAccessToken(); err != nil {
  152. return nil, err
  153. }
  154. var response []byte
  155. if response, err = util.PostJSON(fmt.Sprintf(updateCustomerAcquisitionLinkURL, accessToken), req); err != nil {
  156. return nil, err
  157. }
  158. result := &UpdateCustomerAcquisitionLinkResponse{}
  159. err = util.DecodeWithError(response, result, "UpdateCustomerAcquisitionLink")
  160. return result, err
  161. }
  162. type (
  163. // DeleteCustomerAcquisitionLinkRequest 删除获客链接请求
  164. DeleteCustomerAcquisitionLinkRequest struct {
  165. LinkID string `json:"link_id"`
  166. }
  167. // DeleteCustomerAcquisitionLinkResponse 删除获客链接响应
  168. DeleteCustomerAcquisitionLinkResponse struct {
  169. util.CommonError
  170. }
  171. )
  172. // DeleteCustomerAcquisitionLink 获客助手--删除获客链接
  173. // see https://developer.work.weixin.qq.com/document/path/97297
  174. func (r *Client) DeleteCustomerAcquisitionLink(req *DeleteCustomerAcquisitionLinkRequest) (*DeleteCustomerAcquisitionLinkResponse, error) {
  175. var (
  176. accessToken string
  177. err error
  178. )
  179. if accessToken, err = r.GetAccessToken(); err != nil {
  180. return nil, err
  181. }
  182. var response []byte
  183. if response, err = util.PostJSON(fmt.Sprintf(deleteCustomerAcquisitionLinkURL, accessToken), req); err != nil {
  184. return nil, err
  185. }
  186. result := &DeleteCustomerAcquisitionLinkResponse{}
  187. err = util.DecodeWithError(response, result, "DeleteCustomerAcquisitionLink")
  188. return result, err
  189. }
  190. type (
  191. // GetCustomerInfoWithCustomerAcquisitionLinkRequest 获取由获客链接添加的客户信息请求
  192. GetCustomerInfoWithCustomerAcquisitionLinkRequest struct {
  193. LinkID string `json:"link_id"`
  194. Limit int64 `json:"limit"`
  195. Cursor string `json:"cursor"`
  196. }
  197. // GetCustomerInfoWithCustomerAcquisitionLinkResponse 获取由获客链接添加的客户信息响应
  198. GetCustomerInfoWithCustomerAcquisitionLinkResponse struct {
  199. util.CommonError
  200. CustomerList []CustomerList `json:"customer_list"`
  201. NextCursor string `json:"next_cursor"`
  202. }
  203. // CustomerList 客户列表
  204. CustomerList struct {
  205. ExternalUserid string `json:"external_userid"`
  206. Userid string `json:"userid"`
  207. ChatStatus int64 `json:"chat_status"`
  208. State string `json:"state"`
  209. }
  210. )
  211. // GetCustomerInfoWithCustomerAcquisitionLink 获客助手--获取由获客链接添加的客户信息
  212. // see https://developer.work.weixin.qq.com/document/path/97298
  213. func (r *Client) GetCustomerInfoWithCustomerAcquisitionLink(req *GetCustomerInfoWithCustomerAcquisitionLinkRequest) (*GetCustomerInfoWithCustomerAcquisitionLinkResponse, error) {
  214. var (
  215. accessToken string
  216. err error
  217. )
  218. if accessToken, err = r.GetAccessToken(); err != nil {
  219. return nil, err
  220. }
  221. var response []byte
  222. if response, err = util.PostJSON(fmt.Sprintf(getCustomerInfoWithCustomerAcquisitionLinkURL, accessToken), req); err != nil {
  223. return nil, err
  224. }
  225. result := &GetCustomerInfoWithCustomerAcquisitionLinkResponse{}
  226. err = util.DecodeWithError(response, result, "GetCustomerInfoWithCustomerAcquisitionLink")
  227. return result, err
  228. }
  229. type (
  230. // CustomerAcquisitionQuotaResponse 查询剩余使用量响应
  231. CustomerAcquisitionQuotaResponse struct {
  232. util.CommonError
  233. Total int64 `json:"total"`
  234. Balance int64 `json:"balance"`
  235. QuotaList []QuotaList `json:"quota_list"`
  236. }
  237. // QuotaList 额度
  238. QuotaList struct {
  239. ExpireDate int64 `json:"expire_date"`
  240. Balance int64 `json:"balance"`
  241. }
  242. )
  243. // CustomerAcquisitionQuota 获客助手额度管理与使用统计--查询剩余使用量
  244. // see https://developer.work.weixin.qq.com/document/path/97375
  245. func (r *Client) CustomerAcquisitionQuota() (*CustomerAcquisitionQuotaResponse, error) {
  246. var (
  247. accessToken string
  248. err error
  249. )
  250. if accessToken, err = r.GetAccessToken(); err != nil {
  251. return nil, err
  252. }
  253. var response []byte
  254. if response, err = util.HTTPGet(fmt.Sprintf(customerAcquisitionQuotaURL, accessToken)); err != nil {
  255. return nil, err
  256. }
  257. result := &CustomerAcquisitionQuotaResponse{}
  258. err = util.DecodeWithError(response, result, "CustomerAcquisitionQuota")
  259. return result, err
  260. }
  261. type (
  262. // CustomerAcquisitionStatisticRequest 查询链接使用详情请求
  263. CustomerAcquisitionStatisticRequest struct {
  264. LinkID string `json:"link_id"`
  265. StartTime int64 `json:"start_time"`
  266. EndTime int64 `json:"end_time"`
  267. }
  268. // CustomerAcquisitionStatisticResponse 查询链接使用详情响应
  269. CustomerAcquisitionStatisticResponse struct {
  270. util.CommonError
  271. ClickLinkCustomerCnt int64 `json:"click_link_customer_cnt"`
  272. NewCustomerCnt int64 `json:"new_customer_cnt"`
  273. }
  274. )
  275. // CustomerAcquisitionStatistic 获客助手额度管理与使用统计--查询链接使用详情
  276. // see https://developer.work.weixin.qq.com/document/path/97375
  277. func (r *Client) CustomerAcquisitionStatistic(req *CustomerAcquisitionStatisticRequest) (*CustomerAcquisitionStatisticResponse, error) {
  278. var (
  279. accessToken string
  280. err error
  281. )
  282. if accessToken, err = r.GetAccessToken(); err != nil {
  283. return nil, err
  284. }
  285. var response []byte
  286. if response, err = util.PostJSON(fmt.Sprintf(customerAcquisitionStatisticURL, accessToken), req); err != nil {
  287. return nil, err
  288. }
  289. result := &CustomerAcquisitionStatisticResponse{}
  290. err = util.DecodeWithError(response, result, "CustomerAcquisitionStatistic")
  291. return result, err
  292. }
  293. type (
  294. // GetChatInfoRequest 获取成员多次收消息详情请求
  295. GetChatInfoRequest struct {
  296. ChatKey string `json:"chat_key"`
  297. }
  298. // GetChatInfoResponse 获取成员多次收消息详情响应
  299. GetChatInfoResponse struct {
  300. util.CommonError
  301. UserID string `json:"userid"`
  302. ExternalUserID string `json:"external_userid"`
  303. ChatInfo ChatInfo `json:"chat_info"`
  304. }
  305. // ChatInfo 聊天信息
  306. ChatInfo struct {
  307. RecvMsgCnt int64 `json:"recv_msg_cnt"` // 成员收到的此客户的消息次数
  308. LinkID string `json:"link_id"` // 成员添加客户的获客链接id
  309. State string `json:"state"` // 成员添加客户的state
  310. }
  311. )
  312. // GetChatInfo 获取成员多次收消息详情
  313. // see https://developer.work.weixin.qq.com/document/path/100130
  314. func (r *Client) GetChatInfo(req *GetChatInfoRequest) (*GetChatInfoResponse, error) {
  315. var (
  316. accessToken string
  317. err error
  318. )
  319. if accessToken, err = r.GetAccessToken(); err != nil {
  320. return nil, err
  321. }
  322. var response []byte
  323. if response, err = util.PostJSON(fmt.Sprintf(customerAcquisitionGetChatInfoURL, accessToken), req); err != nil {
  324. return nil, err
  325. }
  326. result := &GetChatInfoResponse{}
  327. err = util.DecodeWithError(response, result, "GetChatInfo")
  328. return result, err
  329. }