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.

296 line
14KB

  1. from asyncio import CancelledError
  2. import queue
  3. from concurrent.futures import Future, ThreadPoolExecutor
  4. import os
  5. import re
  6. import threading
  7. import time
  8. from channel.chat_message import ChatMessage
  9. from common.expired_dict import ExpiredDict
  10. from channel.channel import Channel
  11. from bridge.reply import *
  12. from bridge.context import *
  13. from config import conf
  14. from common.log import logger
  15. from plugins import *
  16. try:
  17. from voice.audio_convert import any_to_wav
  18. except Exception as e:
  19. pass
  20. # 抽象类, 它包含了与消息通道无关的通用处理逻辑
  21. class ChatChannel(Channel):
  22. name = None # 登录的用户名
  23. user_id = None # 登录的用户id
  24. futures = {} # 记录每个session_id提交到线程池的future对象, 用于重置会话时把没执行的future取消掉,正在执行的不会被取消
  25. sessions = {} # 用于控制并发,每个session_id同时只能有一个context在处理
  26. lock = threading.Lock() # 用于控制对sessions的访问
  27. handler_pool = ThreadPoolExecutor(max_workers=8) # 处理消息的线程池
  28. def __init__(self):
  29. _thread = threading.Thread(target=self.consume)
  30. _thread.setDaemon(True)
  31. _thread.start()
  32. # 根据消息构造context,消息内容相关的触发项写在这里
  33. def _compose_context(self, ctype: ContextType, content, **kwargs):
  34. context = Context(ctype, content)
  35. context.kwargs = kwargs
  36. # context首次传入时,origin_ctype是None,
  37. # 引入的起因是:当输入语音时,会嵌套生成两个context,第一步语音转文本,第二步通过文本生成文字回复。
  38. # origin_ctype用于第二步文本回复时,判断是否需要匹配前缀,如果是私聊的语音,就不需要匹配前缀
  39. if 'origin_ctype' not in context:
  40. context['origin_ctype'] = ctype
  41. # context首次传入时,receiver是None,根据类型设置receiver
  42. first_in = 'receiver' not in context
  43. # 群名匹配过程,设置session_id和receiver
  44. if first_in: # context首次传入时,receiver是None,根据类型设置receiver
  45. config = conf()
  46. cmsg = context['msg']
  47. if cmsg.from_user_id == self.user_id and not config.get('trigger_by_self', False):
  48. logger.debug("[WX]self message skipped")
  49. return None
  50. if context["isgroup"]:
  51. group_name = cmsg.other_user_nickname
  52. group_id = cmsg.other_user_id
  53. group_name_white_list = config.get('group_name_white_list', [])
  54. group_name_keyword_white_list = config.get('group_name_keyword_white_list', [])
  55. if any([group_name in group_name_white_list, 'ALL_GROUP' in group_name_white_list, check_contain(group_name, group_name_keyword_white_list)]):
  56. group_chat_in_one_session = conf().get('group_chat_in_one_session', [])
  57. session_id = cmsg.actual_user_id
  58. if any([group_name in group_chat_in_one_session, 'ALL_GROUP' in group_chat_in_one_session]):
  59. session_id = group_id
  60. else:
  61. return None
  62. context['session_id'] = session_id
  63. context['receiver'] = group_id
  64. else:
  65. context['session_id'] = cmsg.other_user_id
  66. context['receiver'] = cmsg.other_user_id
  67. # 消息内容匹配过程,并处理content
  68. if ctype == ContextType.TEXT:
  69. if first_in and "」\n- - - - - - -" in content: # 初次匹配 过滤引用消息
  70. logger.debug("[WX]reference query skipped")
  71. return None
  72. if context["isgroup"]: # 群聊
  73. # 校验关键字
  74. match_prefix = check_prefix(content, conf().get('group_chat_prefix'))
  75. match_contain = check_contain(content, conf().get('group_chat_keyword'))
  76. flag = False
  77. if match_prefix is not None or match_contain is not None:
  78. flag = True
  79. if match_prefix:
  80. content = content.replace(match_prefix, '', 1).strip()
  81. if context['msg'].is_at:
  82. logger.info("[WX]receive group at")
  83. if not conf().get("group_at_off", False):
  84. flag = True
  85. pattern = f'@{self.name}(\u2005|\u0020)'
  86. content = re.sub(pattern, r'', content)
  87. if not flag:
  88. if context["origin_ctype"] == ContextType.VOICE:
  89. logger.info("[WX]receive group voice, but checkprefix didn't match")
  90. return None
  91. else: # 单聊
  92. match_prefix = check_prefix(content, conf().get('single_chat_prefix'))
  93. if match_prefix is not None: # 判断如果匹配到自定义前缀,则返回过滤掉前缀+空格后的内容
  94. content = content.replace(match_prefix, '', 1).strip()
  95. elif context["origin_ctype"] == ContextType.VOICE: # 如果源消息是私聊的语音消息,允许不匹配前缀,放宽条件
  96. pass
  97. else:
  98. return None
  99. img_match_prefix = check_prefix(content, conf().get('image_create_prefix'))
  100. if img_match_prefix:
  101. content = content.replace(img_match_prefix, '', 1).strip()
  102. context.type = ContextType.IMAGE_CREATE
  103. else:
  104. context.type = ContextType.TEXT
  105. context.content = content
  106. if 'desire_rtype' not in context and conf().get('always_reply_voice'):
  107. context['desire_rtype'] = ReplyType.VOICE
  108. elif context.type == ContextType.VOICE:
  109. if 'desire_rtype' not in context and conf().get('voice_reply_voice'):
  110. context['desire_rtype'] = ReplyType.VOICE
  111. return context
  112. def _handle(self, context: Context):
  113. if context is None or not context.content:
  114. return
  115. logger.debug('[WX] ready to handle context: {}'.format(context))
  116. # reply的构建步骤
  117. reply = self._generate_reply(context)
  118. logger.debug('[WX] ready to decorate reply: {}'.format(reply))
  119. # reply的包装步骤
  120. reply = self._decorate_reply(context, reply)
  121. # reply的发送步骤
  122. self._send_reply(context, reply)
  123. def _generate_reply(self, context: Context, reply: Reply = Reply()) -> Reply:
  124. e_context = PluginManager().emit_event(EventContext(Event.ON_HANDLE_CONTEXT, {
  125. 'channel': self, 'context': context, 'reply': reply}))
  126. reply = e_context['reply']
  127. if not e_context.is_pass():
  128. logger.debug('[WX] ready to handle context: type={}, content={}'.format(context.type, context.content))
  129. if context.type == ContextType.TEXT or context.type == ContextType.IMAGE_CREATE: # 文字和图片消息
  130. reply = super().build_reply_content(context.content, context)
  131. elif context.type == ContextType.VOICE: # 语音消息
  132. cmsg = context['msg']
  133. cmsg.prepare()
  134. file_path = context.content
  135. wav_path = os.path.splitext(file_path)[0] + '.wav'
  136. try:
  137. any_to_wav(file_path, wav_path)
  138. except Exception as e: # 转换失败,直接使用mp3,对于某些api,mp3也可以识别
  139. logger.warning("[WX]any to wav error, use raw path. " + str(e))
  140. wav_path = file_path
  141. # 语音识别
  142. reply = super().build_voice_to_text(wav_path)
  143. # 删除临时文件
  144. try:
  145. os.remove(file_path)
  146. if wav_path != file_path:
  147. os.remove(wav_path)
  148. except Exception as e:
  149. pass
  150. # logger.warning("[WX]delete temp file error: " + str(e))
  151. if reply.type == ReplyType.TEXT:
  152. new_context = self._compose_context(
  153. ContextType.TEXT, reply.content, **context.kwargs)
  154. if new_context:
  155. reply = self._generate_reply(new_context)
  156. else:
  157. return
  158. else:
  159. logger.error('[WX] unknown context type: {}'.format(context.type))
  160. return
  161. return reply
  162. def _decorate_reply(self, context: Context, reply: Reply) -> Reply:
  163. if reply and reply.type:
  164. e_context = PluginManager().emit_event(EventContext(Event.ON_DECORATE_REPLY, {
  165. 'channel': self, 'context': context, 'reply': reply}))
  166. reply = e_context['reply']
  167. desire_rtype = context.get('desire_rtype')
  168. if not e_context.is_pass() and reply and reply.type:
  169. if reply.type == ReplyType.TEXT:
  170. reply_text = reply.content
  171. if desire_rtype == ReplyType.VOICE:
  172. reply = super().build_text_to_voice(reply.content)
  173. return self._decorate_reply(context, reply)
  174. if context['isgroup']:
  175. reply_text = '@' + context['msg'].actual_user_nickname + ' ' + reply_text.strip()
  176. reply_text = conf().get("group_chat_reply_prefix", "")+reply_text
  177. else:
  178. reply_text = conf().get("single_chat_reply_prefix", "")+reply_text
  179. reply.content = reply_text
  180. elif reply.type == ReplyType.ERROR or reply.type == ReplyType.INFO:
  181. reply.content = str(reply.type)+":\n" + reply.content
  182. elif reply.type == ReplyType.IMAGE_URL or reply.type == ReplyType.VOICE or reply.type == ReplyType.IMAGE:
  183. pass
  184. else:
  185. logger.error('[WX] unknown reply type: {}'.format(reply.type))
  186. return
  187. if desire_rtype and desire_rtype != reply.type and reply.type not in [ReplyType.ERROR, ReplyType.INFO]:
  188. logger.warning('[WX] desire_rtype: {}, but reply type: {}'.format(context.get('desire_rtype'), reply.type))
  189. return reply
  190. def _send_reply(self, context: Context, reply: Reply):
  191. if reply and reply.type:
  192. e_context = PluginManager().emit_event(EventContext(Event.ON_SEND_REPLY, {
  193. 'channel': self, 'context': context, 'reply': reply}))
  194. reply = e_context['reply']
  195. if not e_context.is_pass() and reply and reply.type:
  196. logger.debug('[WX] ready to send reply: {}, context: {}'.format(reply, context))
  197. self._send(reply, context)
  198. def _send(self, reply: Reply, context: Context, retry_cnt = 0):
  199. try:
  200. self.send(reply, context)
  201. except Exception as e:
  202. logger.error('[WX] sendMsg error: {}'.format(str(e)))
  203. if isinstance(e, NotImplementedError):
  204. return
  205. logger.exception(e)
  206. if retry_cnt < 2:
  207. time.sleep(3+3*retry_cnt)
  208. self._send(reply, context, retry_cnt+1)
  209. def thread_pool_callback(self, session_id):
  210. def func(worker:Future):
  211. try:
  212. worker_exception = worker.exception()
  213. if worker_exception:
  214. logger.exception("Worker return exception: {}".format(worker_exception))
  215. except CancelledError as e:
  216. logger.info("Worker cancelled, session_id = {}".format(session_id))
  217. except Exception as e:
  218. logger.exception("Worker raise exception: {}".format(e))
  219. with self.lock:
  220. self.sessions[session_id][1].release()
  221. return func
  222. def produce(self, context: Context):
  223. session_id = context['session_id']
  224. with self.lock:
  225. if session_id not in self.sessions:
  226. self.sessions[session_id] = (queue.Queue(), threading.BoundedSemaphore(1))
  227. self.sessions[session_id][0].put(context)
  228. # 消费者函数,单独线程,用于从消息队列中取出消息并处理
  229. def consume(self):
  230. while True:
  231. with self.lock:
  232. session_ids = list(self.sessions.keys())
  233. for session_id in session_ids:
  234. context_queue, semaphore = self.sessions[session_id]
  235. if semaphore.acquire(blocking = False): # 等线程处理完毕才能删除
  236. if not context_queue.empty():
  237. context = context_queue.get()
  238. logger.debug("[WX] consume context: {}".format(context))
  239. future:Future = self.handler_pool.submit(self._handle, context)
  240. future.add_done_callback(self.thread_pool_callback(session_id))
  241. if session_id not in self.futures:
  242. self.futures[session_id] = []
  243. self.futures[session_id].append(future)
  244. elif semaphore._initial_value == semaphore._value+1: # 除了当前,没有任务再申请到信号量,说明所有任务都处理完毕
  245. self.futures[session_id] = [t for t in self.futures[session_id] if not t.done()]
  246. assert len(self.futures[session_id]) == 0, "thread pool error"
  247. del self.sessions[session_id]
  248. else:
  249. semaphore.release()
  250. time.sleep(0.1)
  251. def cancel(self, session_id):
  252. with self.lock:
  253. if session_id in self.sessions:
  254. for future in self.futures[session_id]:
  255. future.cancel()
  256. self.sessions[session_id][0]=queue.Queue()
  257. def check_prefix(content, prefix_list):
  258. for prefix in prefix_list:
  259. if content.startswith(prefix):
  260. return prefix
  261. return None
  262. def check_contain(content, keyword_list):
  263. if not keyword_list:
  264. return None
  265. for ky in keyword_list:
  266. if content.find(ky) != -1:
  267. return True
  268. return None