Custom.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | WeChatDeveloper
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2014~2023 ThinkAdmin [ thinkadmin.top ]
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: https://thinkadmin.top
  8. // +----------------------------------------------------------------------
  9. // | 开源协议 ( https://mit-license.org )
  10. // | 免责声明 ( https://thinkadmin.top/disclaimer )
  11. // +----------------------------------------------------------------------
  12. // | gitee 代码仓库:https://gitee.com/zoujingli/WeChatDeveloper
  13. // | github 代码仓库:https://github.com/zoujingli/WeChatDeveloper
  14. // +----------------------------------------------------------------------
  15. namespace WeChat;
  16. use WeChat\Contracts\BasicWeChat;
  17. use WeChat\Contracts\Tools;
  18. /**
  19. * 客服消息处理
  20. * Class Custom
  21. * @package WeChat
  22. */
  23. class Custom extends BasicWeChat
  24. {
  25. /**
  26. * 添加客服帐号
  27. * @param string $kf_account 客服账号
  28. * @param string $nickname 客服昵称
  29. * @return array
  30. * @throws Exceptions\InvalidResponseException
  31. * @throws Exceptions\LocalCacheException
  32. */
  33. public function addAccount($kf_account, $nickname)
  34. {
  35. $data = ['kf_account' => $kf_account, 'nickname' => $nickname];
  36. $url = "https://api.weixin.qq.com/customservice/kfaccount/add?access_token=ACCESS_TOKEN";
  37. $this->registerApi($url, __FUNCTION__, func_get_args());
  38. return $this->httpPostForJson($url, $data);
  39. }
  40. /**
  41. * 修改客服帐号
  42. * @param string $kf_account 客服账号
  43. * @param string $nickname 客服昵称
  44. * @return array
  45. * @throws Exceptions\InvalidResponseException
  46. * @throws Exceptions\LocalCacheException
  47. */
  48. public function updateAccount($kf_account, $nickname)
  49. {
  50. $data = ['kf_account' => $kf_account, 'nickname' => $nickname];
  51. $url = "https://api.weixin.qq.com/customservice/kfaccount/update?access_token=ACCESS_TOKEN";
  52. $this->registerApi($url, __FUNCTION__, func_get_args());
  53. return $this->httpPostForJson($url, $data);
  54. }
  55. /**
  56. * 删除客服帐号
  57. * @param string $kf_account 客服账号
  58. * @return array
  59. * @throws Exceptions\InvalidResponseException
  60. * @throws Exceptions\LocalCacheException
  61. */
  62. public function deleteAccount($kf_account)
  63. {
  64. $data = ['kf_account' => $kf_account];
  65. $url = "https://api.weixin.qq.com/customservice/kfaccount/del?access_token=ACCESS_TOKEN";
  66. $this->registerApi($url, __FUNCTION__, func_get_args());
  67. return $this->httpPostForJson($url, $data);
  68. }
  69. /**
  70. * 邀请绑定客服帐号
  71. * @param string $kf_account 完整客服帐号,格式为:帐号前缀@公众号微信号
  72. * @param string $invite_wx 接收绑定邀请的客服微信号
  73. * @return array
  74. * @throws Exceptions\InvalidResponseException
  75. * @throws Exceptions\LocalCacheException
  76. */
  77. public function inviteWorker($kf_account, $invite_wx)
  78. {
  79. $url = 'https://api.weixin.qq.com/customservice/kfaccount/inviteworker?access_token=ACCESS_TOKEN';
  80. return $this->callPostApi($url, ['kf_account' => $kf_account, 'invite_wx' => $invite_wx]);
  81. }
  82. /**
  83. * 获取所有客服账号
  84. * @return array
  85. * @throws Exceptions\InvalidResponseException
  86. * @throws Exceptions\LocalCacheException
  87. */
  88. public function getAccountList()
  89. {
  90. $url = "https://api.weixin.qq.com/cgi-bin/customservice/getkflist?access_token=ACCESS_TOKEN";
  91. $this->registerApi($url, __FUNCTION__, func_get_args());
  92. return $this->httpGetForJson($url);
  93. }
  94. /**
  95. * 设置客服帐号的头像
  96. * @param string $kf_account 客户账号
  97. * @param string $image 头像文件位置
  98. * @return array
  99. * @throws Exceptions\InvalidResponseException
  100. * @throws Exceptions\LocalCacheException
  101. */
  102. public function uploadHeadimg($kf_account, $image)
  103. {
  104. $url = "http://api.weixin.qq.com/customservice/kfaccount/uploadheadimg?access_token=ACCESS_TOKEN&kf_account={$kf_account}";
  105. $this->registerApi($url, __FUNCTION__, func_get_args());
  106. return $this->httpPostForJson($url, ['media' => Tools::createCurlFile($image)]);
  107. }
  108. /**
  109. * 客服接口-发消息
  110. * @param array $data
  111. * @return array
  112. * @throws Exceptions\InvalidResponseException
  113. * @throws Exceptions\LocalCacheException
  114. */
  115. public function send(array $data)
  116. {
  117. $url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=ACCESS_TOKEN";
  118. $this->registerApi($url, __FUNCTION__, func_get_args());
  119. return $this->httpPostForJson($url, $data);
  120. }
  121. /**
  122. * 客服输入状态
  123. * @param string $openid 普通用户(openid)
  124. * @param string $command Typing:正在输入,CancelTyping:取消正在输入
  125. * @return array
  126. * @throws Exceptions\InvalidResponseException
  127. * @throws Exceptions\LocalCacheException
  128. */
  129. public function typing($openid, $command = 'Typing')
  130. {
  131. $url = "https://api.weixin.qq.com/cgi-bin/message/custom/typing?access_token=ACCESS_TOKEN";
  132. $this->registerApi($url, __FUNCTION__, func_get_args());
  133. return $this->httpPostForJson($url, ['touser' => $openid, 'command' => $command]);
  134. }
  135. /**
  136. * 根据标签进行群发【订阅号与服务号认证后均可用】
  137. * @param array $data
  138. * @return array
  139. * @throws Exceptions\InvalidResponseException
  140. * @throws Exceptions\LocalCacheException
  141. */
  142. public function massSendAll(array $data)
  143. {
  144. $url = "https://api.weixin.qq.com/cgi-bin/message/mass/sendall?access_token=ACCESS_TOKEN";
  145. $this->registerApi($url, __FUNCTION__, func_get_args());
  146. return $this->httpPostForJson($url, $data);
  147. }
  148. /**
  149. * 根据OpenID列表群发【订阅号不可用,服务号认证后可用】
  150. * @param array $data
  151. * @return array
  152. * @throws Exceptions\InvalidResponseException
  153. * @throws Exceptions\LocalCacheException
  154. */
  155. public function massSend(array $data)
  156. {
  157. $url = "https://api.weixin.qq.com/cgi-bin/message/mass/send?access_token=ACCESS_TOKEN";
  158. $this->registerApi($url, __FUNCTION__, func_get_args());
  159. return $this->httpPostForJson($url, $data);
  160. }
  161. /**
  162. * 删除群发【订阅号与服务号认证后均可用】
  163. * @param integer $msg_id 发送出去的消息ID
  164. * @param null|integer $article_idx 要删除的文章在图文消息中的位置,第一篇编号为1,该字段不填或填0会删除全部文章
  165. * @return array
  166. * @throws Exceptions\InvalidResponseException
  167. * @throws Exceptions\LocalCacheException
  168. */
  169. public function massDelete($msg_id, $article_idx = null)
  170. {
  171. $data = ['msg_id' => $msg_id];
  172. is_null($article_idx) || $data['article_idx'] = $article_idx;
  173. $url = "https://api.weixin.qq.com/cgi-bin/message/mass/delete?access_token=ACCESS_TOKEN";
  174. $this->registerApi($url, __FUNCTION__, func_get_args());
  175. return $this->httpPostForJson($url, $data);
  176. }
  177. /**
  178. * 预览接口【订阅号与服务号认证后均可用】
  179. * @param array $data
  180. * @return array
  181. * @throws Exceptions\InvalidResponseException
  182. * @throws Exceptions\LocalCacheException
  183. */
  184. public function massPreview(array $data)
  185. {
  186. $url = "https://api.weixin.qq.com/cgi-bin/message/mass/preview?access_token=ACCESS_TOKEN";
  187. $this->registerApi($url, __FUNCTION__, func_get_args());
  188. return $this->httpPostForJson($url, $data);
  189. }
  190. /**
  191. * 查询群发消息发送状态【订阅号与服务号认证后均可用】
  192. * @param integer $msg_id 群发消息后返回的消息id
  193. * @return array
  194. * @throws Exceptions\InvalidResponseException
  195. * @throws Exceptions\LocalCacheException
  196. */
  197. public function massGet($msg_id)
  198. {
  199. $url = "https://api.weixin.qq.com/cgi-bin/message/mass/get?access_token=ACCESS_TOKEN";
  200. $this->registerApi($url, __FUNCTION__, func_get_args());
  201. return $this->httpPostForJson($url, ['msg_id' => $msg_id]);
  202. }
  203. /**
  204. * 获取群发速度
  205. * @return array
  206. * @throws Exceptions\InvalidResponseException
  207. * @throws Exceptions\LocalCacheException
  208. */
  209. public function massGetSeed()
  210. {
  211. $url = "https://api.weixin.qq.com/cgi-bin/message/mass/speed/get?access_token=ACCESS_TOKEN";
  212. $this->registerApi($url, __FUNCTION__, func_get_args());
  213. return $this->httpPostForJson($url, []);
  214. }
  215. /**
  216. * 设置群发速度
  217. * @param integer $speed 群发速度的级别
  218. * @return array
  219. * @throws Exceptions\InvalidResponseException
  220. * @throws Exceptions\LocalCacheException
  221. */
  222. public function massSetSeed($speed)
  223. {
  224. $url = "https://api.weixin.qq.com/cgi-bin/message/mass/speed/set?access_token=ACCESS_TOKEN";
  225. $this->registerApi($url, __FUNCTION__, func_get_args());
  226. return $this->httpPostForJson($url, ['speed' => $speed]);
  227. }
  228. }