You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

wechat_channel.py 7.8KB

2 vuotta sitten
2 vuotta sitten
2 vuotta sitten
1 vuosi sitten
2 vuotta sitten
2 vuotta sitten
2 vuotta sitten
1 vuosi sitten
2 vuotta sitten
2 vuotta sitten
2 vuotta sitten
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. # encoding:utf-8
  2. """
  3. wechat channel
  4. """
  5. import itchat
  6. import json
  7. from itchat.content import *
  8. from channel.channel import Channel
  9. from concurrent.futures import ThreadPoolExecutor
  10. from common.log import logger
  11. from common.tmp_dir import TmpDir
  12. from config import conf
  13. import requests
  14. import io
  15. thread_pool = ThreadPoolExecutor(max_workers=8)
  16. @itchat.msg_register(TEXT)
  17. def handler_single_msg(msg):
  18. WechatChannel().handle_text(msg)
  19. return None
  20. @itchat.msg_register(TEXT, isGroupChat=True)
  21. def handler_group_msg(msg):
  22. WechatChannel().handle_group(msg)
  23. return None
  24. @itchat.msg_register(VOICE)
  25. def handler_single_voice(msg):
  26. WechatChannel().handle_voice(msg)
  27. return None
  28. class WechatChannel(Channel):
  29. def __init__(self):
  30. pass
  31. def startup(self):
  32. # login by scan QRCode
  33. itchat.auto_login(enableCmdQR=2)
  34. # start message listener
  35. itchat.run()
  36. def handle_voice(self, msg):
  37. if conf().get('speech_recognition') != True :
  38. return
  39. logger.debug("[WX]receive voice msg: " + msg['FileName'])
  40. thread_pool.submit(self._do_handle_voice, msg)
  41. def _do_handle_voice(self, msg):
  42. fileName = TmpDir().path() + msg['FileName']
  43. msg.download(fileName)
  44. content = super().build_voice_to_text(fileName)
  45. self._handle_single_msg(msg, content, conf().get('voice_reply_voice'))
  46. def handle_text(self, msg):
  47. logger.debug("[WX]receive text msg: " + json.dumps(msg, ensure_ascii=False))
  48. content = msg['Text']
  49. self._handle_single_msg(msg, content, False)
  50. def _handle_single_msg(self, msg, content, reply_voice=False):
  51. from_user_id = msg['FromUserName']
  52. to_user_id = msg['ToUserName'] # 接收人id
  53. other_user_id = msg['User']['UserName'] # 对手方id
  54. match_prefix = self.check_prefix(content, conf().get('single_chat_prefix'))
  55. if "」\n- - - - - - - - - - - - - - -" in content:
  56. logger.debug("[WX]reference query skipped")
  57. return
  58. if from_user_id == other_user_id and match_prefix is not None:
  59. # 好友向自己发送消息
  60. if match_prefix != '':
  61. str_list = content.split(match_prefix, 1)
  62. if len(str_list) == 2:
  63. content = str_list[1].strip()
  64. img_match_prefix = self.check_prefix(content, conf().get('image_create_prefix'))
  65. if img_match_prefix:
  66. content = content.split(img_match_prefix, 1)[1].strip()
  67. thread_pool.submit(self._do_send_img, content, from_user_id)
  68. elif reply_voice:
  69. thread_pool.submit(self._do_send_voice, content, from_user_id)
  70. else :
  71. thread_pool.submit(self._do_send_text, content, from_user_id)
  72. elif to_user_id == other_user_id and match_prefix:
  73. # 自己给好友发送消息
  74. str_list = content.split(match_prefix, 1)
  75. if len(str_list) == 2:
  76. content = str_list[1].strip()
  77. img_match_prefix = self.check_prefix(content, conf().get('image_create_prefix'))
  78. if img_match_prefix:
  79. content = content.split(img_match_prefix, 1)[1].strip()
  80. thread_pool.submit(self._do_send_img, content, to_user_id)
  81. elif reply_voice:
  82. thread_pool.submit(self._do_send_voice, content, to_user_id)
  83. else:
  84. thread_pool.submit(self._do_send_text, content, to_user_id)
  85. def handle_group(self, msg):
  86. logger.debug("[WX]receive group msg: " + json.dumps(msg, ensure_ascii=False))
  87. group_name = msg['User'].get('NickName', None)
  88. group_id = msg['User'].get('UserName', None)
  89. if not group_name:
  90. return ""
  91. origin_content = msg['Content']
  92. content = msg['Content']
  93. content_list = content.split(' ', 1)
  94. context_special_list = content.split('\u2005', 1)
  95. if len(context_special_list) == 2:
  96. content = context_special_list[1]
  97. elif len(content_list) == 2:
  98. content = content_list[1]
  99. if "」\n- - - - - - - - - - - - - - -" in content:
  100. logger.debug("[WX]reference query skipped")
  101. return ""
  102. config = conf()
  103. match_prefix = (msg['IsAt'] and not config.get("group_at_off", False)) or self.check_prefix(origin_content, config.get('group_chat_prefix')) \
  104. or self.check_contain(origin_content, config.get('group_chat_keyword'))
  105. if ('ALL_GROUP' in config.get('group_name_white_list') or group_name in config.get('group_name_white_list') or self.check_contain(group_name, config.get('group_name_keyword_white_list'))) and match_prefix:
  106. img_match_prefix = self.check_prefix(content, conf().get('image_create_prefix'))
  107. if img_match_prefix:
  108. content = content.split(img_match_prefix, 1)[1].strip()
  109. thread_pool.submit(self._do_send_img, content, group_id)
  110. else:
  111. thread_pool.submit(self._do_send_group, content, msg)
  112. def send(self, msg, receiver):
  113. itchat.send(msg, toUserName=receiver)
  114. logger.info('[WX] sendMsg={}, receiver={}'.format(msg, receiver))
  115. def _do_send_voice(self, query, reply_user_id):
  116. try:
  117. if not query:
  118. return
  119. context = dict()
  120. context['from_user_id'] = reply_user_id
  121. reply_text = super().build_reply_content(query, context)
  122. if reply_text:
  123. replyFile = super().build_text_to_voice(reply_text)
  124. itchat.send_file(replyFile, toUserName=reply_user_id)
  125. logger.info('[WX] sendFile={}, receiver={}'.format(replyFile, reply_user_id))
  126. except Exception as e:
  127. logger.exception(e)
  128. def _do_send_text(self, query, reply_user_id):
  129. try:
  130. if not query:
  131. return
  132. context = dict()
  133. context['from_user_id'] = reply_user_id
  134. reply_text = super().build_reply_content(query, context)
  135. if reply_text:
  136. self.send(conf().get("single_chat_reply_prefix") + reply_text, reply_user_id)
  137. except Exception as e:
  138. logger.exception(e)
  139. def _do_send_img(self, query, reply_user_id):
  140. try:
  141. if not query:
  142. return
  143. context = dict()
  144. context['type'] = 'IMAGE_CREATE'
  145. img_url = super().build_reply_content(query, context)
  146. if not img_url:
  147. return
  148. # 图片下载
  149. pic_res = requests.get(img_url, stream=True)
  150. image_storage = io.BytesIO()
  151. for block in pic_res.iter_content(1024):
  152. image_storage.write(block)
  153. image_storage.seek(0)
  154. # 图片发送
  155. itchat.send_image(image_storage, reply_user_id)
  156. logger.info('[WX] sendImage, receiver={}'.format(reply_user_id))
  157. except Exception as e:
  158. logger.exception(e)
  159. def _do_send_group(self, query, msg):
  160. if not query:
  161. return
  162. context = dict()
  163. context['from_user_id'] = msg['ActualUserName']
  164. reply_text = super().build_reply_content(query, context)
  165. if reply_text:
  166. reply_text = '@' + msg['ActualNickName'] + ' ' + reply_text.strip()
  167. self.send(conf().get("group_chat_reply_prefix", "") + reply_text, msg['User']['UserName'])
  168. def check_prefix(self, content, prefix_list):
  169. for prefix in prefix_list:
  170. if content.startswith(prefix):
  171. return prefix
  172. return None
  173. def check_contain(self, content, keyword_list):
  174. if not keyword_list:
  175. return None
  176. for ky in keyword_list:
  177. if content.find(ky) != -1:
  178. return True
  179. return None