appchat.py 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. # -*- coding: utf-8 -*-
  2. from __future__ import absolute_import, unicode_literals
  3. from optionaldict import optionaldict
  4. from wechatpy.client.api.base import BaseWeChatAPI
  5. class WeChatAppChat(BaseWeChatAPI):
  6. """
  7. https://work.weixin.qq.com/api/doc#90000/90135/90244
  8. """
  9. def create(self, chat_id=None, name=None, owner=None, user_list=None):
  10. """
  11. 创建群聊会话
  12. 详情请参考
  13. https://work.weixin.qq.com/api/doc#90000/90135/90245
  14. 限制说明:
  15. 只允许企业自建应用调用,且应用的可见范围必须是根部门;
  16. 群成员人数不可超过管理端配置的“群成员人数上限”,且最大不可超过500人;
  17. 每企业创建群数不可超过1000/天;
  18. :param chat_id: 群聊的唯一标志,不能与已有的群重复;字符串类型,最长32个字符。只允许字符0-9及字母a-zA-Z。如果不填,系统会随机生成群id
  19. :param name: 群聊名,最多50个utf8字符,超过将截断
  20. :param owner: 指定群主的id。如果不指定,系统会随机从userlist中选一人作为群主
  21. :param user_list: 会话成员列表,成员用userid来标识。至少2人,至多500人
  22. :return: 返回的 JSON 数据包
  23. """
  24. data = optionaldict(
  25. chatid=chat_id,
  26. name=name,
  27. owner=owner,
  28. userlist=user_list,
  29. )
  30. return self._post('appchat/create', data=data)
  31. def get(self, chat_id):
  32. """
  33. 获取群聊会话
  34. 详情请参考
  35. https://work.weixin.qq.com/api/doc#90000/90135/90247
  36. :param chat_id: 群聊id
  37. :return: 会话信息
  38. """
  39. res = self._get('appchat/get', params={'chatid': chat_id})
  40. return res['chat_info']
  41. def update(self, chat_id, name=None, owner=None,
  42. add_user_list=None, del_user_list=None):
  43. """
  44. 修改群聊会话
  45. 详情请参考
  46. https://work.weixin.qq.com/api/doc#90000/90135/90246
  47. :param chat_id: 群聊id
  48. :param name: 新的群聊名。若不需更新,请忽略此参数。最多50个utf8字符,超过将截断
  49. :param owner: 新群主的id。若不需更新,请忽略此参数
  50. :param add_user_list: 会话新增成员列表,成员用userid来标识
  51. :param del_user_list: 会话退出成员列表,成员用userid来标识
  52. :return: 返回的 JSON 数据包
  53. """
  54. data = optionaldict(
  55. chatid=chat_id,
  56. name=name,
  57. owner=owner,
  58. add_user_list=add_user_list,
  59. del_user_list=del_user_list,
  60. )
  61. return self._post('appchat/update', data=data)
  62. def send(self, chat_id, msg_type, **kwargs):
  63. """
  64. 应用推送消息
  65. 详情请参考:https://work.weixin.qq.com/api/doc#90000/90135/90248
  66. :param chat_id: 群聊id
  67. :param msg_type: 消息类型,可以为text/image/voice/video/file/textcard/news/mpnews/markdown
  68. :param kwargs: 具体消息类型的扩展参数
  69. :return:
  70. """
  71. data = {
  72. 'chatid': chat_id,
  73. 'safe': kwargs.get('safe') or 0
  74. }
  75. data.update(self._build_msg_content(msg_type, **kwargs))
  76. return self._post('appchat/send', data=data)
  77. def send_msg(self, chat_id, msg_type, **kwargs):
  78. """ deprecated, use `send` instead """
  79. return self.send(chat_id, msg_type, **kwargs)
  80. def send_text(self, chat_id, content, safe=0):
  81. """
  82. 发送文本消息
  83. 详情请参考:https://work.weixin.qq.com/api/doc#90000/90135/90248/文本消息/
  84. :param chat_id: 群聊id
  85. :param content: 消息内容
  86. :param safe: 表示是否是保密消息,0表示否,1表示是,默认0
  87. :return:
  88. """
  89. return self.send(chat_id, 'text', safe=safe, content=content)
  90. def _build_msg_content(self, msgtype='text', **kwargs):
  91. """
  92. 构造消息内容
  93. :param content: 消息内容,最长不超过2048个字节
  94. :param msgtype: 消息类型,可以为text/image/voice/video/file/textcard/news/mpnews/markdown
  95. :param kwargs: 具体消息类型的扩展参数
  96. :return:
  97. """
  98. data = {'msgtype': msgtype}
  99. if msgtype == 'text':
  100. data[msgtype] = {'content': kwargs.get('content')}
  101. elif msgtype == 'image' or msgtype == 'voice' or msgtype == 'file':
  102. data[msgtype] = {'media_id': kwargs.get('media_id')}
  103. elif msgtype == 'video':
  104. data[msgtype] = {
  105. 'media_id': kwargs.get('media_id'),
  106. 'title': kwargs.get('title'),
  107. 'description': kwargs.get('description')
  108. }
  109. elif msgtype == 'textcard':
  110. data[msgtype] = {
  111. 'title': kwargs.get('title'),
  112. 'description': kwargs.get('description'),
  113. 'url': kwargs.get('url'),
  114. 'btntxt': kwargs.get('btntxt'),
  115. }
  116. elif msgtype == 'news':
  117. # {
  118. # "articles" :
  119. # [
  120. # {
  121. # "title" : "中秋节礼品领取",
  122. # "description" : "今年中秋节公司有豪礼相送",
  123. # "url":"https://zhidao.baidu.com/question/2073647112026042748.html",
  124. # "picurl":"http://res.mail.qq.com/node/ww/wwopenmng/images/independent/doc/test_pic_msg1.png"
  125. # }
  126. # ]
  127. # }
  128. data[msgtype] = kwargs
  129. elif msgtype == 'mpnews':
  130. # {
  131. # "articles":[
  132. # {
  133. # "title": "地球一小时",
  134. # "thumb_media_id": "biz_get(image)",
  135. # "author": "Author",
  136. # "content_source_url": "https://work.weixin.qq.com",
  137. # "content": "3月24日20:30-21:30 \n办公区将关闭照明一小时,请各部门同事相互转告",
  138. # "digest": "3月24日20:30-21:30 \n办公区将关闭照明一小时"
  139. # }
  140. # ]
  141. # }
  142. data[msgtype] = kwargs
  143. elif msgtype == 'markdown':
  144. # {
  145. # "content": "您的会议室已经预定,稍后会同步到`邮箱`
  146. # >**事项详情**
  147. # >事 项:<font color=\"info\">开会</font>
  148. # >组织者:@miglioguan
  149. # >参与者:@miglioguan、@kunliu、@jamdeezhou、@kanexiong、@kisonwang
  150. # >
  151. # >会议室:<font color=\"info\">广州TIT 1楼 301</font>
  152. # >日 期:<font color=\"warning\">2018年5月18日</font>
  153. # >时 间:<font color=\"comment\">上午9:00-11:00</font>
  154. # >
  155. # >请准时参加会议。
  156. # >
  157. # >如需修改会议信息,请点击:[修改会议信息](https://work.weixin.qq.com)"
  158. # }
  159. data[msgtype] = kwargs
  160. else:
  161. raise TypeError('不能识别的msgtype: %s' % msgtype)
  162. return data