No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

436 líneas
20KB

  1. import threading
  2. from common import kafka_helper,redis_helper,utils
  3. from urllib.parse import urlparse, unquote
  4. import json,time,re,random,os
  5. from common.log import logger, log_exception
  6. from datetime import datetime
  7. from wechat import gewe_chat
  8. from model import Models
  9. def wx_messages_process_callback(agent_tel,message):
  10. try:
  11. # print(f'手机号 {agent_tel}')
  12. wxchat = gewe_chat.wxchat
  13. msg_content = message
  14. cleaned_content = clean_json_string(msg_content)
  15. content = json.loads(cleaned_content)
  16. data = content.get("data", {})
  17. msg_type_data = data.get("msg_type", None)
  18. content_data = data.get("content", {})
  19. agent_tel = content_data.get("agent_tel", None)
  20. if msg_type_data == 'group-sending':
  21. process_group_sending(wxchat, content_data, agent_tel)
  22. except json.JSONDecodeError as e:
  23. print(f"JSON解码错误: {e}, 消息内容: {message}")
  24. except Exception as e:
  25. print(f"处理消息时发生错误: {e}, 消息内容: {message}")
  26. def process_group_sending(wxchat:gewe_chat.GeWeChatCom, content_data, agent_tel:str):
  27. # 获取登录信息
  28. hash_key = f"__AI_OPS_WX__:LOGININFO:{agent_tel}"
  29. logininfo = redis_helper.redis_helper.get_hash(hash_key)
  30. if not logininfo:
  31. logger.warning(f"未找到 {agent_tel} 的登录信息")
  32. return
  33. token_id = logininfo.get('tokenId')
  34. app_id = logininfo.get('appId')
  35. agent_wxid = logininfo.get('wxid')
  36. # 获取联系人列表并计算交集
  37. hash_key = f"__AI_OPS_WX__:CONTACTS_BRIEF:{agent_wxid}"
  38. cache_friend_wxids_str=redis_helper.redis_helper.get_hash_field(hash_key,"data")
  39. cache_friend_wxids_list=json.loads(cache_friend_wxids_str) if cache_friend_wxids_str else []
  40. cache_friend_wxids=[f["userName"] for f in cache_friend_wxids_list]
  41. # 获取群交集
  42. hash_key = f"__AI_OPS_WX__:GROUPS_INFO:{agent_wxid}"
  43. cache_chatrooms = redis_helper.redis_helper.get_hash(hash_key)
  44. cache_chatroom_ids=cache_chatrooms.keys()
  45. wxid_contact_list_content_data = [c['wxid'] for c in content_data.get("contact_list", [])]
  46. intersection_friend_wxids = list(set(cache_friend_wxids) & set(wxid_contact_list_content_data))
  47. intersection_chatroom_ids = list(set(cache_chatroom_ids) & set(wxid_contact_list_content_data))
  48. intersection_wxids=intersection_friend_wxids+intersection_chatroom_ids
  49. # 发送消息
  50. wx_content_list = content_data.get("wx_content", [])
  51. wxchat.forward_video_aeskey = ''
  52. wxchat.forward_video_cdnvideourl = ''
  53. wxchat.forward_video_length = 0
  54. for intersection_wxid in intersection_wxids:
  55. for wx_content in wx_content_list:
  56. if wx_content["type"] == "text":
  57. send_text_message(wxchat, token_id, app_id, agent_wxid, [intersection_wxid], wx_content["text"])
  58. elif wx_content["type"] == "image_url":
  59. send_image_message(wxchat, token_id, app_id, agent_wxid, [intersection_wxid], wx_content.get("image_url", {}).get("url"))
  60. elif wx_content["type"] == "tts":
  61. send_tts_message(wxchat, token_id, app_id, agent_wxid, [intersection_wxid], wx_content["text"])
  62. elif wx_content["type"] == "file":
  63. send_file_message(wxchat, token_id, app_id, agent_wxid, [intersection_wxid], wx_content.get("file_url", {}).get("url"))
  64. def send_text_message(wxchat:gewe_chat.GeWeChatCom, token_id, app_id, agent_wxid, intersection_wxids, text):
  65. for t in intersection_wxids:
  66. # 发送文本消息
  67. ret,ret_msg,res = wxchat.post_text(token_id, app_id, t, text)
  68. logger.info(f'{agent_wxid} 向 {t} 发送文字【{text}】')
  69. # 构造对话消息并发送到 Kafka
  70. input_wx_content_dialogue_message = [{"type": "text", "text": text}]
  71. input_message = utils.dialogue_message(agent_wxid, t, input_wx_content_dialogue_message)
  72. kafka_helper.kafka_client.produce_message(input_message)
  73. logger.info("发送对话 %s", input_message)
  74. # 等待随机时间
  75. time.sleep(random.uniform(5, 15))
  76. def send_image_message(wxchat:gewe_chat.GeWeChatCom, token_id, app_id, agent_wxid, intersection_wxids, image_url):
  77. aeskey, cdnthumburl, cdnthumblength, cdnthumbheight, cdnthumbwidth, length, md5 = "", "", 0, 0, 0, 0, ""
  78. for t in intersection_wxids:
  79. if t == intersection_wxids[0]:
  80. # 发送图片
  81. ret,ret_msg,res = wxchat.post_image(token_id, app_id, t, image_url)
  82. if ret==200:
  83. aeskey = res["aesKey"]
  84. cdnthumburl = res["fileId"]
  85. cdnthumblength = res["cdnThumbLength"]
  86. cdnthumbheight = res["height"]
  87. cdnthumbwidth = res["width"]
  88. length = res["length"]
  89. md5 = res["md5"]
  90. logger.info(f'{agent_wxid} 向 {t} 发送图片【{image_url}】{ret_msg}')
  91. else:
  92. logger.warning(f'{agent_wxid} 向 {t} 发送图片【{image_url}】{ret_msg}')
  93. else:
  94. if aeskey !="":
  95. # 转发图片
  96. res,ret,ret_msg= wxchat.forward_image(token_id, app_id, t, aeskey, cdnthumburl, cdnthumblength, cdnthumbheight, cdnthumbwidth, length, md5)
  97. logger.info(f'{agent_wxid} 向 {t} 转发图片【{image_url}】{ret_msg}')
  98. else:
  99. # 发送图片
  100. ret,ret_msg,res = wxchat.post_image(token_id, app_id, t, image_url)
  101. if ret==200:
  102. aeskey = res["aesKey"]
  103. cdnthumburl = res["fileId"]
  104. cdnthumblength = res["cdnThumbLength"]
  105. cdnthumbheight = res["height"]
  106. cdnthumbwidth = res["width"]
  107. length = res["length"]
  108. md5 = res["md5"]
  109. logger.info(f'{agent_wxid} 向 {t} 发送图片【{image_url}】{ret_msg}')
  110. else:
  111. logger.warning(f'{agent_wxid} 向 {t} 发送图片【{image_url}】{ret_msg}')
  112. # 构造对话消息并发送到 Kafka
  113. wx_content_dialogue_message = [{"type": "image_url", "image_url": {"url": image_url}}]
  114. input_message = utils.dialogue_message(agent_wxid, t, wx_content_dialogue_message)
  115. kafka_helper.kafka_client.produce_message(input_message)
  116. logger.info("发送对话 %s", input_message)
  117. # 等待随机时间
  118. time.sleep(random.uniform(5, 15))
  119. def send_tts_message(wxchat:gewe_chat.GeWeChatCom, token_id, app_id, agent_wxid, intersection_wxids, text):
  120. voice_during,voice_url=utils.wx_voice(text)
  121. for t in intersection_wxids:
  122. # 发送送语音消息
  123. if voice_url:
  124. ret,ret_msg,res = wxchat.post_voice(token_id, app_id, t, voice_url,voice_during)
  125. if ret==200:
  126. logger.info(f'{agent_wxid} 向 {t} 发送语音文本【{text}】{ret_msg}')
  127. # 构造对话消息并发送到 Kafka
  128. input_wx_content_dialogue_message = [{"type": "text", "text": text}]
  129. input_message = utils.dialogue_message(agent_wxid, t, input_wx_content_dialogue_message)
  130. kafka_helper.kafka_client.produce_message(input_message)
  131. logger.info("发送对话 %s", input_message)
  132. else:
  133. logger.warning((f'{agent_wxid} 向 {t} 发送语音文本【{text}】{ret_msg}'))
  134. else:
  135. logger.warning((f'{agent_wxid} 向 {t} 发送语音文本【{text}】出错'))
  136. # 等待随机时间
  137. time.sleep(random.uniform(5, 15))
  138. def send_file_message(wxchat:gewe_chat.GeWeChatCom, token_id, app_id, agent_wxid, intersection_wxids, file_url):
  139. parsed_url = urlparse(file_url)
  140. path = parsed_url.path
  141. # 从路径中提取文件名
  142. filename = path.split('/')[-1]
  143. # 获取扩展名
  144. _, ext = os.path.splitext(filename)
  145. if ext == '.mp4':
  146. send_video_message(wxchat, token_id, app_id, agent_wxid, intersection_wxids, file_url)
  147. else:
  148. send_other_file_message(wxchat, token_id, app_id, agent_wxid, intersection_wxids, file_url)
  149. #time.sleep(random.uniform(5, 15))
  150. def send_video_message(wxchat:gewe_chat.GeWeChatCom, token_id, app_id, agent_wxid, intersection_wxids, file_url):
  151. for t in intersection_wxids:
  152. # 发送视频消息
  153. parsed_url = urlparse(file_url)
  154. filename = os.path.basename(parsed_url.path)
  155. tmp_file_path = os.path.join(os.getcwd(),'tmp', filename) # 拼接完整路径
  156. thumbnail_path=tmp_file_path.replace('.mp4','.jpg')
  157. if wxchat.forward_video_aeskey == '':
  158. video_thumb_url,video_duration =utils.download_video_and_get_thumbnail(file_url,thumbnail_path)
  159. print(f'视频缩略图 {video_thumb_url} 时长 {video_duration}')
  160. ret,ret_msg,res = wxchat.post_video(token_id, app_id, t, file_url,video_thumb_url,video_duration)
  161. if ret==200:
  162. wxchat.forward_video_aeskey = res["aesKey"]
  163. wxchat.forward_video_cdnvideourl = res["cdnThumbUrl"]
  164. wxchat.forward_video_length = res["length"]
  165. else:
  166. ret,ret_msg,res = wxchat.forward_video(token_id, app_id, t, wxchat.forward_video_aeskey, wxchat.forward_video_cdnvideourl, wxchat.forward_video_length)
  167. print('转发视频')
  168. if ret==200:
  169. logger.info(f'{agent_wxid} 向 {t} 发送视频【{file_url}】{ret_msg}')
  170. # 构造对话消息并发送到 Kafka
  171. input_wx_content_dialogue_message = [{"type": "file", "file_url": {"url": file_url}}]
  172. input_message = utils.dialogue_message(agent_wxid, t, input_wx_content_dialogue_message)
  173. kafka_helper.kafka_client.produce_message(input_message)
  174. logger.info("发送对话 %s", input_message)
  175. else:
  176. logger.warning((f'{agent_wxid} 向 {t} 发送视频【{file_url}】{ret_msg}'))
  177. # 等待随机时间
  178. time.sleep(random.uniform(5, 15))
  179. def send_other_file_message(wxchat:gewe_chat.GeWeChatCom, token_id, app_id, agent_wxid, intersection_wxids, file_url):
  180. print('send_otherfile_message')
  181. def clean_json_string(json_str):
  182. # 删除所有控制字符(非打印字符),包括换行符、回车符等
  183. return re.sub(r'[\x00-\x1f\x7f]', '', json_str)
  184. # 启动 Kafka 消费者线程
  185. def start_kafka_consumer_thread():
  186. agent_tel=os.environ.get('tel', '18029274615')
  187. # consumer_thread = threading.Thread(target=kafka_helper.kafka_client.consume_messages, args=(agent_tel,wx_messages_process_callback,))
  188. consumer_thread = threading.Thread(target=kafka_helper.kafka_client.consume_messages, args=(ops_messages_process,))
  189. consumer_thread.daemon = True # 设置为守护线程,应用退出时会自动结束
  190. consumer_thread.start()
  191. def login_or_reconnect(wxchat:gewe_chat.GeWeChatCom, token_id, app_id, region_id,agent_token_id,hash_key, is_reconnect=False, max_retries=5):
  192. """
  193. 封装微信登录或重连的逻辑
  194. """
  195. agent_tel=hash_key.split(":")[-1]
  196. retry_count = 0
  197. while retry_count < max_retries:
  198. retry_count += 1
  199. if is_reconnect:
  200. logger.info("尝试重连...")
  201. else:
  202. logger.info("获取二维码进行登录...")
  203. qr_code = wxchat.get_login_qr_code(token_id, app_id,region_id)
  204. base64_string = qr_code.get('qrImgBase64')
  205. uuid = qr_code.get('uuid')
  206. if not uuid:
  207. logger.error(f"uuid获取二维码失败: {qr_code}")
  208. wxchat.release_login_lock(token_id)
  209. break
  210. app_id = app_id or qr_code.get('appId')
  211. start_time = time.time()
  212. qr_code_urls= wxchat.qrCallback(uuid, base64_string)
  213. # 构造 Kafka 消息发送二维码
  214. k_message=utils.login_qrcode_message(token_id,agent_tel,base64_string,qr_code_urls)
  215. kafka_helper.kafka_client.produce_message(k_message)
  216. while True:
  217. now = time.time()
  218. # 如果登录超时,重新获取二维码
  219. if now- start_time > 150: #150 秒 二维码失效
  220. break
  221. logger.info(f"{token_id} 使用 {app_id},等待扫码登录,二维码有效时间 {150 - int(now - start_time)} 秒")
  222. captch_code = wxchat.get_login_wx_captch_code_from_cache(token_id)
  223. captch_code= captch_code if captch_code else ''
  224. ret,msg,res = wxchat.check_login(token_id, app_id, uuid,captch_code)
  225. if ret == 200:
  226. flag = res.get('status')
  227. # 构造 Kafka 消息发送登录状态
  228. # todo
  229. if flag == 2:
  230. logger.info(f"登录成功: {res}")
  231. head_img_url=res.get('headImgUrl','')
  232. login_info = res.get('loginInfo', {})
  233. wxid=login_info.get('wxid',agent_tel)
  234. login_info.update({'appId': app_id, 'uuid': uuid, 'tokenId': token_id,'status': 1,'headImgUrl':head_img_url,'regionId':region_id})
  235. cache_login_info=redis_helper.redis_helper.get_hash(hash_key)
  236. if 'appId' not in cache_login_info:
  237. login_info.update({"create_at":int(time.time()),"modify_at":int(time.time())})
  238. # 默认配置
  239. config=Models.AgentConfig.model_validate({
  240. "chatroomIdWhiteList": [],
  241. "agentTokenId": agent_token_id,
  242. "agentEnabled": False,
  243. "addContactsFromChatroomIdWhiteList": [],
  244. "chatWaitingMsgEnabled": True
  245. })
  246. else:
  247. login_info.update({"modify_at":int(time.time())})
  248. # 已有配置
  249. config_cache=wxchat.get_wxchat_config_from_cache(wxid)
  250. config=Models.AgentConfig.model_validate(config_cache)
  251. cleaned_login_info = {k: (v if v is not None else '') for k, v in login_info.items()}
  252. # 保存配置信息
  253. config_dict=config.model_dump()
  254. wxchat.save_wxchat_config(wxid,config_dict)
  255. # 保存登录信息
  256. redis_helper.redis_helper.set_hash(hash_key, cleaned_login_info)
  257. wxchat.release_login_lock(token_id)
  258. return login_info
  259. else:
  260. logger.info(f"登录检查中: {ret}-{msg}-{res}")
  261. time.sleep(5)
  262. logger.error(f"登录失败,二维码生成 {max_retries} 次")
  263. wxchat.release_login_lock(token_id)
  264. def fetch_and_save_contacts(wxchat:gewe_chat.GeWeChatCom, token_id, app_id, hash_key):
  265. """
  266. 获取联系人列表并保存到缓存
  267. """
  268. ret,msg,contacts_list = wxchat.fetch_contacts_list(token_id, app_id)
  269. friend_wxids = contacts_list['friends'][3:] # 可以调整截取范围
  270. wxid = redis_helper.redis_helper.get_hash_field(hash_key, 'wxid')
  271. wxchat.save_contacts_brief_to_cache(token_id, app_id, wxid, friend_wxids)
  272. print(f'微信ID {wxid} 登录APPID {app_id} 成功,联系人已保存')
  273. def wx_login(wxchat:gewe_chat.GeWeChatCom,tel,token_id,region_id,agent_token_id):
  274. hash_key = f"__AI_OPS_WX__:LOGININFO:{tel}"
  275. login_info = redis_helper.redis_helper.get_hash(hash_key)
  276. if not login_info:
  277. login_info = login_or_reconnect(wxchat, token_id, '', region_id,agent_token_id,hash_key)
  278. else:
  279. app_id = login_info.get('appId')
  280. token_id = login_info.get('tokenId')
  281. wxid= login_info.get('wxid')
  282. # 检查是否已经登录
  283. is_online = wxchat.check_online(token_id, app_id)
  284. if is_online:
  285. logger.info(f'微信ID {wxid} 在APPID {app_id} 已经在线')
  286. else:
  287. # 尝试重连
  288. res = wxchat.reconnection(token_id, app_id)
  289. if res.get('ret') == 200:
  290. logger.info(f'微信ID {wxid} 在APPID {app_id} 重连成功')
  291. else:
  292. print("重连失败,重新登录...")
  293. login_info = login_or_reconnect(wxchat, token_id, app_id, region_id,agent_token_id,hash_key, is_reconnect=True)
  294. if login_info:
  295. fetch_and_save_contacts(wxchat, token_id, login_info.get('appId'), hash_key)
  296. def login_wx_captch_code_process(wxchat:gewe_chat.GeWeChatCom,message):
  297. msg_content = message
  298. cleaned_content = clean_json_string(msg_content)
  299. content = json.loads(cleaned_content)
  300. data = content.get("data", {})
  301. msg_type_data = data.get("msg_type", None)
  302. content_data = data.get("content", {})
  303. token_id = content_data.get("token_id", None)
  304. captch_code = content_data.get("captch_code", None)
  305. wxchat.save_login_wx_captch_code_to_cache(token_id,captch_code)
  306. def ops_messages_process(message):
  307. try:
  308. wxchat = gewe_chat.wxchat
  309. #print(message)
  310. # logger.info(f"接收到kafka消息: {json.dumps(message, separators=(',', ':'), ensure_ascii=False)}")
  311. logger.info(f"接收到kafka消息: {json.dumps(json.loads(message), ensure_ascii=False)}")
  312. msg_content = message
  313. cleaned_content = clean_json_string(msg_content)
  314. content = json.loads(cleaned_content)
  315. data = content.get("data", {})
  316. msg_type_data = data.get("msg_type", None)
  317. content_data = data.get("content", {})
  318. if msg_type_data=="login":
  319. # tel=content_data.get('tel', '18029274615')
  320. # token_id=content_data.get('token_id', 'f828cb3c-1039-489f-b9ae-7494d1778a15')
  321. # region_id=content_data.get('region_id', '440000')
  322. # agent_token_id=content_data.get('agent_token_id', '')
  323. tel=content_data.get('tel', '18733438393')
  324. token_id=content_data.get('token_id', 'c50b7d57-2efa-4a53-8c11-104a06d1e1fa')
  325. region_id=content_data.get('region_id', '440000')
  326. agent_token_id=content_data.get('agent_token_id', 'sk-fAOIdANeGXjWKW5mFybnsNZZGYU2lFLmqVY9rVFaFmjiOaWt3tcWMi')
  327. loginfo=gewe_chat.wxchat.get_login_info_from_cache(tel)
  328. print(loginfo)
  329. status=loginfo.get('status','0')
  330. if status=='1':
  331. logger.info(f'手机号{tel},wx_token{token_id} 已经微信登录,终止登录流程')
  332. return
  333. flag=gewe_chat.wxchat.acquire_login_lock(token_id,800)
  334. if flag:
  335. thread = threading.Thread(target=wx_login, args=(wxchat,tel,token_id,region_id,agent_token_id))
  336. thread.daemon = True
  337. thread.start()
  338. else:
  339. logger.info(f'手机号{tel}, wx_token{token_id} 登录进行中,稍后再试')
  340. elif msg_type_data == 'group-sending':
  341. agent_tel=content_data.get('agent_tel', '18029274615')
  342. # 使用线程处理
  343. #wx_messages_process_callback(agent_tel,message)
  344. thread = threading.Thread(target=wx_messages_process_callback, args=(agent_tel,message,))
  345. thread.daemon = True
  346. thread.start()
  347. elif msg_type_data == 'login_wx_captch_code':
  348. thread = threading.Thread(target=login_wx_captch_code_process, args=(wxchat,message,))
  349. thread.daemon = True
  350. thread.start()
  351. else:
  352. print(f'未处理息类型 {msg_type_data}')
  353. except Exception as e:
  354. print(f"处理消息时发生错误: {e}, 消息内容: {message}")