customservice.py 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. # -*- coding: utf-8 -*-
  2. from __future__ import absolute_import, unicode_literals
  3. import hashlib
  4. import time
  5. import datetime
  6. from six.moves.urllib.parse import quote
  7. from optionaldict import optionaldict
  8. from library import to_binary
  9. from library.wechatpy.client.api.base import BaseWeChatAPI
  10. class WeChatCustomService(BaseWeChatAPI):
  11. def add_account(self, account, nickname, password):
  12. """
  13. 添加客服账号
  14. 详情请参考
  15. http://mp.weixin.qq.com/wiki/1/70a29afed17f56d537c833f89be979c9.html
  16. :param account: 完整客服账号,格式为:账号前缀@公众号微信号
  17. :param nickname: 客服昵称,最长6个汉字或12个英文字符
  18. :param password: 客服账号登录密码
  19. :return: 返回的 JSON 数据包
  20. """
  21. password = to_binary(password)
  22. password = hashlib.md5(password).hexdigest()
  23. return self._post(
  24. 'https://api.weixin.qq.com/customservice/kfaccount/add',
  25. data={
  26. 'kf_account': account,
  27. 'nickname': nickname,
  28. 'password': password
  29. }
  30. )
  31. def update_account(self, account, nickname, password):
  32. """
  33. 更新客服账号
  34. 详情请参考
  35. http://mp.weixin.qq.com/wiki/1/70a29afed17f56d537c833f89be979c9.html
  36. :param account: 完整客服账号,格式为:账号前缀@公众号微信号
  37. :param nickname: 客服昵称,最长6个汉字或12个英文字符
  38. :param password: 客服账号登录密码
  39. :return: 返回的 JSON 数据包
  40. """
  41. password = to_binary(password)
  42. password = hashlib.md5(password).hexdigest()
  43. return self._post(
  44. 'https://api.weixin.qq.com/customservice/kfaccount/update',
  45. data={
  46. 'kf_account': account,
  47. 'nickname': nickname,
  48. 'password': password
  49. }
  50. )
  51. def delete_account(self, account):
  52. """
  53. 删除客服账号
  54. 详情请参考
  55. http://mp.weixin.qq.com/wiki/1/70a29afed17f56d537c833f89be979c9.html
  56. :param account: 完整客服账号,格式为:账号前缀@公众号微信号
  57. :return: 返回的 JSON 数据包
  58. """
  59. params_data = [
  60. 'access_token={0}'.format(quote(self.access_token)),
  61. 'kf_account={0}'.format(quote(to_binary(account), safe=b'/@')),
  62. ]
  63. params = '&'.join(params_data)
  64. return self._get(
  65. 'https://api.weixin.qq.com/customservice/kfaccount/del',
  66. params=params
  67. )
  68. def get_accounts(self):
  69. """
  70. 获取客服账号列表
  71. 详情请参考
  72. http://mp.weixin.qq.com/wiki/1/70a29afed17f56d537c833f89be979c9.html
  73. :return: 客服账号列表
  74. """
  75. res = self._get(
  76. 'customservice/getkflist',
  77. result_processor=lambda x: x['kf_list']
  78. )
  79. return res
  80. def upload_headimg(self, account, media_file):
  81. """
  82. 上传客服账号头像
  83. 详情请参考
  84. http://mp.weixin.qq.com/wiki/1/70a29afed17f56d537c833f89be979c9.html
  85. :param account: 完整客服账号
  86. :param media_file: 要上传的头像文件,一个 File-Object
  87. :return: 返回的 JSON 数据包
  88. """
  89. return self._post(
  90. 'https://api.weixin.qq.com/customservice/kfaccount/uploadheadimg',
  91. params={
  92. 'kf_account': account
  93. },
  94. files={
  95. 'media': media_file
  96. }
  97. )
  98. def get_online_accounts(self):
  99. """
  100. 获取在线客服接待信息
  101. 详情请参考
  102. http://mp.weixin.qq.com/wiki/9/6fff6f191ef92c126b043ada035cc935.html
  103. :return: 客服接待信息列表
  104. """
  105. res = self._get(
  106. 'customservice/getonlinekflist',
  107. result_processor=lambda x: x['kf_online_list']
  108. )
  109. return res
  110. def create_session(self, openid, account, text=None):
  111. """
  112. 多客服创建会话
  113. 详情请参考
  114. http://mp.weixin.qq.com/wiki/2/6c20f3e323bdf5986cfcb33cbd3b829a.html
  115. :param openid: 客户 openid
  116. :param account: 完整客服账号
  117. :param text: 附加信息,可选
  118. :return: 返回的 JSON 数据包
  119. """
  120. data = optionaldict(
  121. openid=openid,
  122. kf_account=account,
  123. text=text
  124. )
  125. return self._post(
  126. 'https://api.weixin.qq.com/customservice/kfsession/create',
  127. data=data
  128. )
  129. def close_session(self, openid, account, text=None):
  130. """
  131. 多客服关闭会话
  132. 详情请参考
  133. http://mp.weixin.qq.com/wiki/2/6c20f3e323bdf5986cfcb33cbd3b829a.html
  134. :param openid: 客户 openid
  135. :param account: 完整客服账号
  136. :param text: 附加信息,可选
  137. :return: 返回的 JSON 数据包
  138. """
  139. data = optionaldict(
  140. openid=openid,
  141. kf_account=account,
  142. text=text
  143. )
  144. return self._post(
  145. 'https://api.weixin.qq.com/customservice/kfsession/close',
  146. data=data
  147. )
  148. def get_session(self, openid):
  149. """
  150. 获取客户的会话状态
  151. 详情请参考
  152. http://mp.weixin.qq.com/wiki/2/6c20f3e323bdf5986cfcb33cbd3b829a.html
  153. :param openid: 客户 openid
  154. :return: 返回的 JSON 数据包
  155. """
  156. return self._get(
  157. 'https://api.weixin.qq.com/customservice/kfsession/getsession',
  158. params={'openid': openid}
  159. )
  160. def get_session_list(self, account):
  161. """
  162. 获取客服的会话列表
  163. 详情请参考
  164. http://mp.weixin.qq.com/wiki/2/6c20f3e323bdf5986cfcb33cbd3b829a.html
  165. :param account: 完整客服账号
  166. :return: 客服的会话列表
  167. """
  168. res = self._get(
  169. 'https://api.weixin.qq.com/customservice/kfsession/getsessionlist',
  170. params={'kf_account': account},
  171. result_processor=lambda x: x['sessionlist']
  172. )
  173. return res
  174. def get_wait_case(self):
  175. """
  176. 获取未接入会话列表
  177. 详情请参考
  178. http://mp.weixin.qq.com/wiki/2/6c20f3e323bdf5986cfcb33cbd3b829a.html
  179. :return: 返回的 JSON 数据包
  180. """
  181. return self._get(
  182. 'https://api.weixin.qq.com/customservice/kfsession/getwaitcase'
  183. )
  184. def get_records(self, start_time, end_time, msgid=1,
  185. number=10000):
  186. """
  187. 获取客服聊天记录
  188. :param start_time: 查询开始时间,UNIX 时间戳
  189. :param end_time: 查询结束时间,UNIX 时间戳,每次查询不能跨日查询
  190. :param msgid: 消息id顺序从小到大,从1开始
  191. :param number: 每次获取条数,最多10000条
  192. :return: 返回的 JSON 数据包
  193. """
  194. if isinstance(start_time, datetime.datetime):
  195. start_time = time.mktime(start_time.timetuple())
  196. if isinstance(end_time, datetime.datetime):
  197. end_time = time.mktime(end_time.timetuple())
  198. record_data = {
  199. 'starttime': int(start_time),
  200. 'endtime': int(end_time),
  201. 'msgid': msgid,
  202. 'number': number
  203. }
  204. res = self._post(
  205. 'https://api.weixin.qq.com/customservice/msgrecord/getmsglist',
  206. data=record_data,
  207. )
  208. return res