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.

390 lines
18KB

  1. # encoding:utf-8
  2. """
  3. wechaty channel
  4. Python Wechaty - https://github.com/wechaty/python-wechaty
  5. """
  6. import io
  7. import os
  8. import json
  9. import time
  10. import asyncio
  11. import requests
  12. import pysilk
  13. import wave
  14. from pydub import AudioSegment
  15. from typing import Optional, Union
  16. from bridge.context import Context, ContextType
  17. from wechaty_puppet import MessageType, FileBox, ScanStatus # type: ignore
  18. from wechaty import Wechaty, Contact
  19. from wechaty.user import Message, Room, MiniProgram, UrlLink
  20. from channel.channel import Channel
  21. from common.log import logger
  22. from common.tmp_dir import TmpDir
  23. from config import conf
  24. class WechatyChannel(Channel):
  25. def __init__(self):
  26. pass
  27. def startup(self):
  28. asyncio.run(self.main())
  29. async def main(self):
  30. config = conf()
  31. # 使用PadLocal协议 比较稳定(免费web协议 os.environ['WECHATY_PUPPET_SERVICE_ENDPOINT'] = '127.0.0.1:8080')
  32. token = config.get('wechaty_puppet_service_token')
  33. token = "chiaki2024"
  34. os.environ['WECHATY_PUPPET_SERVICE_TOKEN'] = token
  35. global bot
  36. bot = Wechaty()
  37. bot.on('scan', self.on_scan)
  38. bot.on('login', self.on_login)
  39. bot.on('message', self.on_message)
  40. await bot.start()
  41. async def on_login(self, contact: Contact):
  42. logger.info('[WX] login user={}'.format(contact))
  43. async def on_scan(self, status: ScanStatus, qr_code: Optional[str] = None,
  44. data: Optional[str] = None):
  45. contact = self.Contact.load(self.contact_id)
  46. logger.info('[WX] scan user={}, scan status={}, scan qr_code={}'.format(contact, status.name, qr_code))
  47. # print(f'user <{contact}> scan status: {status.name} , 'f'qr_code: {qr_code}')
  48. async def on_message(self, msg: Message):
  49. """
  50. listen for message event
  51. """
  52. from_contact = msg.talker() # 获取消息的发送者
  53. to_contact = msg.to() # 接收人
  54. room = msg.room() # 获取消息来自的群聊. 如果消息不是来自群聊, 则返回None
  55. from_user_id = from_contact.contact_id
  56. to_user_id = to_contact.contact_id # 接收人id
  57. # other_user_id = msg['User']['UserName'] # 对手方id
  58. content = msg.text()
  59. mention_content = await msg.mention_text() # 返回过滤掉@name后的消息
  60. match_prefix = self.check_prefix(content, conf().get('single_chat_prefix'))
  61. conversation: Union[Room, Contact] = from_contact if room is None else room
  62. if room is None and msg.type() == MessageType.MESSAGE_TYPE_TEXT:
  63. if not msg.is_self() and match_prefix is not None:
  64. # 好友向自己发送消息
  65. if match_prefix != '':
  66. str_list = content.split(match_prefix, 1)
  67. if len(str_list) == 2:
  68. content = str_list[1].strip()
  69. img_match_prefix = self.check_prefix(content, conf().get('image_create_prefix'))
  70. if img_match_prefix:
  71. content = content.split(img_match_prefix, 1)[1].strip()
  72. await self._do_send_img(content, from_user_id)
  73. else:
  74. await self._do_send(content, from_user_id)
  75. elif msg.is_self() and match_prefix:
  76. # 自己给好友发送消息
  77. str_list = content.split(match_prefix, 1)
  78. if len(str_list) == 2:
  79. content = str_list[1].strip()
  80. img_match_prefix = self.check_prefix(content, conf().get('image_create_prefix'))
  81. if img_match_prefix:
  82. content = content.split(img_match_prefix, 1)[1].strip()
  83. await self._do_send_img(content, to_user_id)
  84. else:
  85. await self._do_send(content, to_user_id)
  86. elif room is None and msg.type() == MessageType.MESSAGE_TYPE_AUDIO:
  87. if not msg.is_self(): # 接收语音消息
  88. # 下载语音文件
  89. voice_file = await msg.to_file_box()
  90. silk_file = TmpDir().path() + voice_file.name
  91. await voice_file.to_file(silk_file)
  92. logger.info("[WX]receive voice file: " + silk_file)
  93. # 将文件转成wav格式音频
  94. wav_file = silk_file.replace(".slk", ".wav")
  95. with open(silk_file, 'rb') as f:
  96. silk_data = f.read()
  97. pcm_data = pysilk.decode(silk_data)
  98. with wave.open(wav_file, 'wb') as wav_data:
  99. wav_data.setnchannels(1)
  100. wav_data.setsampwidth(2)
  101. wav_data.setframerate(24000)
  102. wav_data.writeframes(pcm_data)
  103. if os.path.exists(wav_file):
  104. converter_state = "true" # 转换wav成功
  105. else:
  106. converter_state = "false" # 转换wav失败
  107. logger.info("[WX]receive voice converter: " + converter_state)
  108. # 语音识别为文本
  109. query = super().build_voice_to_text(wav_file).content
  110. # 交验关键字
  111. match_prefix = self.check_prefix(query, conf().get('single_chat_prefix'))
  112. if match_prefix is not None:
  113. if match_prefix != '':
  114. str_list = query.split(match_prefix, 1)
  115. if len(str_list) == 2:
  116. query = str_list[1].strip()
  117. # 返回消息
  118. if conf().get('voice_reply_voice'):
  119. await self._do_send_voice(query, from_user_id)
  120. else:
  121. await self._do_send(query, from_user_id)
  122. else:
  123. logger.info("[WX]receive voice check prefix: " + 'False')
  124. # 清除缓存文件
  125. os.remove(wav_file)
  126. os.remove(silk_file)
  127. elif room and msg.type() == MessageType.MESSAGE_TYPE_TEXT:
  128. # 群组&文本消息
  129. room_id = room.room_id
  130. room_name = await room.topic()
  131. from_user_id = from_contact.contact_id
  132. from_user_name = from_contact.name
  133. is_at = await msg.mention_self()
  134. content = mention_content
  135. config = conf()
  136. match_prefix = (is_at and not config.get("group_at_off", False)) \
  137. or self.check_prefix(content, config.get('group_chat_prefix')) \
  138. or self.check_contain(content, config.get('group_chat_keyword'))
  139. # Wechaty判断is_at为True,返回的内容是过滤掉@之后的内容;而is_at为False,则会返回完整的内容
  140. # 故判断如果匹配到自定义前缀,则返回过滤掉前缀+空格后的内容,用于实现类似自定义+前缀触发生成AI图片的功能
  141. prefixes = config.get('group_chat_prefix')
  142. for prefix in prefixes:
  143. if content.startswith(prefix):
  144. content = content.replace(prefix, '', 1).strip()
  145. break
  146. if ('ALL_GROUP' in config.get('group_name_white_list') or room_name in config.get(
  147. 'group_name_white_list') or self.check_contain(room_name, config.get(
  148. 'group_name_keyword_white_list'))) and match_prefix:
  149. img_match_prefix = self.check_prefix(content, conf().get('image_create_prefix'))
  150. if img_match_prefix:
  151. content = content.split(img_match_prefix, 1)[1].strip()
  152. await self._do_send_group_img(content, room_id)
  153. else:
  154. await self._do_send_group(content, room_id, room_name, from_user_id, from_user_name)
  155. elif room and msg.type() == MessageType.MESSAGE_TYPE_AUDIO:
  156. # 群组&语音消息
  157. room_id = room.room_id
  158. room_name = await room.topic()
  159. from_user_id = from_contact.contact_id
  160. from_user_name = from_contact.name
  161. is_at = await msg.mention_self()
  162. config = conf()
  163. # 是否开启语音识别、群消息响应功能、群名白名单符合等条件
  164. if config.get('group_speech_recognition') and (
  165. 'ALL_GROUP' in config.get('group_name_white_list') or room_name in config.get(
  166. 'group_name_white_list') or self.check_contain(room_name, config.get(
  167. 'group_name_keyword_white_list'))):
  168. # 下载语音文件
  169. voice_file = await msg.to_file_box()
  170. silk_file = TmpDir().path() + voice_file.name
  171. await voice_file.to_file(silk_file)
  172. logger.info("[WX]receive voice file: " + silk_file)
  173. # 将文件转成wav格式音频
  174. wav_file = silk_file.replace(".slk", ".wav")
  175. with open(silk_file, 'rb') as f:
  176. silk_data = f.read()
  177. pcm_data = pysilk.decode(silk_data)
  178. with wave.open(wav_file, 'wb') as wav_data:
  179. wav_data.setnchannels(1)
  180. wav_data.setsampwidth(2)
  181. wav_data.setframerate(24000)
  182. wav_data.writeframes(pcm_data)
  183. if os.path.exists(wav_file):
  184. converter_state = "true" # 转换wav成功
  185. else:
  186. converter_state = "false" # 转换wav失败
  187. logger.info("[WX]receive voice converter: " + converter_state)
  188. # 语音识别为文本
  189. query = super().build_voice_to_text(wav_file).content
  190. # 校验关键字
  191. match_prefix = self.check_prefix(query, config.get('group_chat_prefix')) \
  192. or self.check_contain(query, config.get('group_chat_keyword'))
  193. # Wechaty判断is_at为True,返回的内容是过滤掉@之后的内容;而is_at为False,则会返回完整的内容
  194. if match_prefix is not None:
  195. # 故判断如果匹配到自定义前缀,则返回过滤掉前缀+空格后的内容,用于实现类似自定义+前缀触发生成AI图片的功能
  196. prefixes = config.get('group_chat_prefix')
  197. for prefix in prefixes:
  198. if query.startswith(prefix):
  199. query = query.replace(prefix, '', 1).strip()
  200. break
  201. # 返回消息
  202. img_match_prefix = self.check_prefix(query, conf().get('image_create_prefix'))
  203. if img_match_prefix:
  204. query = query.split(img_match_prefix, 1)[1].strip()
  205. await self._do_send_group_img(query, room_id)
  206. elif config.get('voice_reply_voice'):
  207. await self._do_send_group_voice(query, room_id, room_name, from_user_id, from_user_name)
  208. else:
  209. await self._do_send_group(query, room_id, room_name, from_user_id, from_user_name)
  210. else:
  211. logger.info("[WX]receive voice check prefix: " + 'False')
  212. # 清除缓存文件
  213. os.remove(wav_file)
  214. os.remove(silk_file)
  215. async def send(self, message: Union[str, Message, FileBox, Contact, UrlLink, MiniProgram], receiver):
  216. logger.info('[WX] sendMsg={}, receiver={}'.format(message, receiver))
  217. if receiver:
  218. contact = await bot.Contact.find(receiver)
  219. await contact.say(message)
  220. async def send_group(self, message: Union[str, Message, FileBox, Contact, UrlLink, MiniProgram], receiver):
  221. logger.info('[WX] sendMsg={}, receiver={}'.format(message, receiver))
  222. if receiver:
  223. room = await bot.Room.find(receiver)
  224. await room.say(message)
  225. async def _do_send(self, query, reply_user_id):
  226. try:
  227. if not query:
  228. return
  229. context = Context(ContextType.TEXT, query)
  230. context['session_id'] = reply_user_id
  231. reply_text = super().build_reply_content(query, context).content
  232. if reply_text:
  233. await self.send(conf().get("single_chat_reply_prefix") + reply_text, reply_user_id)
  234. except Exception as e:
  235. logger.exception(e)
  236. async def _do_send_voice(self, query, reply_user_id):
  237. try:
  238. if not query:
  239. return
  240. context = Context(ContextType.TEXT, query)
  241. context['session_id'] = reply_user_id
  242. reply_text = super().build_reply_content(query, context).content
  243. if reply_text:
  244. # 转换 mp3 文件为 silk 格式
  245. mp3_file = super().build_text_to_voice(reply_text).content
  246. silk_file = mp3_file.replace(".mp3", ".silk")
  247. # Load the MP3 file
  248. audio = AudioSegment.from_file(mp3_file, format="mp3")
  249. # Convert to WAV format
  250. audio = audio.set_frame_rate(24000).set_channels(1)
  251. wav_data = audio.raw_data
  252. sample_width = audio.sample_width
  253. # Encode to SILK format
  254. silk_data = pysilk.encode(wav_data, 24000)
  255. # Save the silk file
  256. with open(silk_file, "wb") as f:
  257. f.write(silk_data)
  258. # 发送语音
  259. t = int(time.time())
  260. file_box = FileBox.from_file(silk_file, name=str(t) + '.silk')
  261. await self.send(file_box, reply_user_id)
  262. # 清除缓存文件
  263. os.remove(mp3_file)
  264. os.remove(silk_file)
  265. except Exception as e:
  266. logger.exception(e)
  267. async def _do_send_img(self, query, reply_user_id):
  268. try:
  269. if not query:
  270. return
  271. context = Context(ContextType.IMAGE_CREATE, query)
  272. img_url = super().build_reply_content(query, context).content
  273. if not img_url:
  274. return
  275. # 图片下载
  276. # pic_res = requests.get(img_url, stream=True)
  277. # image_storage = io.BytesIO()
  278. # for block in pic_res.iter_content(1024):
  279. # image_storage.write(block)
  280. # image_storage.seek(0)
  281. # 图片发送
  282. logger.info('[WX] sendImage, receiver={}'.format(reply_user_id))
  283. t = int(time.time())
  284. file_box = FileBox.from_url(url=img_url, name=str(t) + '.png')
  285. await self.send(file_box, reply_user_id)
  286. except Exception as e:
  287. logger.exception(e)
  288. async def _do_send_group(self, query, group_id, group_name, group_user_id, group_user_name):
  289. if not query:
  290. return
  291. context = Context(ContextType.TEXT, query)
  292. group_chat_in_one_session = conf().get('group_chat_in_one_session', [])
  293. if ('ALL_GROUP' in group_chat_in_one_session or \
  294. group_name in group_chat_in_one_session or \
  295. self.check_contain(group_name, group_chat_in_one_session)):
  296. context['session_id'] = str(group_id)
  297. else:
  298. context['session_id'] = str(group_id) + '-' + str(group_user_id)
  299. reply_text = super().build_reply_content(query, context).content
  300. if reply_text:
  301. reply_text = '@' + group_user_name + ' ' + reply_text.strip()
  302. await self.send_group(conf().get("group_chat_reply_prefix", "") + reply_text, group_id)
  303. async def _do_send_group_voice(self, query, group_id, group_name, group_user_id, group_user_name):
  304. if not query:
  305. return
  306. context = Context(ContextType.TEXT, query)
  307. group_chat_in_one_session = conf().get('group_chat_in_one_session', [])
  308. if ('ALL_GROUP' in group_chat_in_one_session or \
  309. group_name in group_chat_in_one_session or \
  310. self.check_contain(group_name, group_chat_in_one_session)):
  311. context['session_id'] = str(group_id)
  312. else:
  313. context['session_id'] = str(group_id) + '-' + str(group_user_id)
  314. reply_text = super().build_reply_content(query, context).content
  315. if reply_text:
  316. reply_text = '@' + group_user_name + ' ' + reply_text.strip()
  317. # 转换 mp3 文件为 silk 格式
  318. mp3_file = super().build_text_to_voice(reply_text).content
  319. silk_file = mp3_file.replace(".mp3", ".silk")
  320. # Load the MP3 file
  321. audio = AudioSegment.from_file(mp3_file, format="mp3")
  322. # Convert to WAV format
  323. audio = audio.set_frame_rate(24000).set_channels(1)
  324. wav_data = audio.raw_data
  325. sample_width = audio.sample_width
  326. # Encode to SILK format
  327. silk_data = pysilk.encode(wav_data, 24000)
  328. # Save the silk file
  329. with open(silk_file, "wb") as f:
  330. f.write(silk_data)
  331. # 发送语音
  332. t = int(time.time())
  333. file_box = FileBox.from_file(silk_file, name=str(t) + '.silk')
  334. await self.send_group(file_box, group_id)
  335. # 清除缓存文件
  336. os.remove(mp3_file)
  337. os.remove(silk_file)
  338. async def _do_send_group_img(self, query, reply_room_id):
  339. try:
  340. if not query:
  341. return
  342. context = Context(ContextType.IMAGE_CREATE, query)
  343. img_url = super().build_reply_content(query, context).content
  344. if not img_url:
  345. return
  346. # 图片发送
  347. logger.info('[WX] sendImage, receiver={}'.format(reply_room_id))
  348. t = int(time.time())
  349. file_box = FileBox.from_url(url=img_url, name=str(t) + '.png')
  350. await self.send_group(file_box, reply_room_id)
  351. except Exception as e:
  352. logger.exception(e)
  353. def check_prefix(self, content, prefix_list):
  354. for prefix in prefix_list:
  355. if content.startswith(prefix):
  356. return prefix
  357. return None
  358. def check_contain(self, content, keyword_list):
  359. if not keyword_list:
  360. return None
  361. for ky in keyword_list:
  362. if content.find(ky) != -1:
  363. return True
  364. return None