Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

476 linhas
22KB

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