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 8.9KB

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