Custom.php 9.0 KB

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