選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

biz.py 22KB

4ヶ月前
2ヶ月前
3ヶ月前
4ヶ月前
3ヶ月前
4ヶ月前
2ヶ月前
4ヶ月前
3ヶ月前
4ヶ月前
3ヶ月前
2ヶ月前
3ヶ月前
3ヶ月前
3ヶ月前
3ヶ月前
3ヶ月前
3ヶ月前
3ヶ月前
2ヶ月前
2ヶ月前
4ヶ月前
3ヶ月前
4ヶ月前
3ヶ月前
4ヶ月前
3ヶ月前
4ヶ月前
3ヶ月前
4ヶ月前
3ヶ月前
4ヶ月前
3ヶ月前
4ヶ月前
2ヶ月前
2ヶ月前
2ヶ月前
4ヶ月前
3ヶ月前
3ヶ月前
4ヶ月前
3ヶ月前
2ヶ月前
3ヶ月前
3ヶ月前
2ヶ月前
2ヶ月前
2ヶ月前
3ヶ月前
3ヶ月前
3ヶ月前
2ヶ月前
2ヶ月前
2ヶ月前
2ヶ月前
3ヶ月前
2ヶ月前
3ヶ月前
3ヶ月前
3ヶ月前
2ヶ月前
3ヶ月前
2ヶ月前
3ヶ月前
2ヶ月前
3ヶ月前
2ヶ月前
3ヶ月前
2ヶ月前
2ヶ月前
3ヶ月前
3ヶ月前
2ヶ月前
2ヶ月前
2ヶ月前
2ヶ月前
2ヶ月前
2ヶ月前
2ヶ月前
2ヶ月前
2ヶ月前
2ヶ月前
3ヶ月前
3ヶ月前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  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_from_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. wxid=cleaned_login_info.get('wxid',agent_tel)
  289. wxchat.save_wxchat_config(wxid,config_dict)
  290. return login_info
  291. else:
  292. logger.info(f"登录检查中: {ret}-{msg}-{res}")
  293. time.sleep(5)
  294. logger.error(f"登录失败,二维码生成 {max_retries} 次")
  295. wxchat.release_login_lock(token_id)
  296. def fetch_and_save_contacts(wxchat:gewe_chat.GeWeChatCom, token_id, app_id, hash_key):
  297. """
  298. 获取联系人列表并保存到缓存
  299. """
  300. ret,msg,contacts_list = wxchat.fetch_contacts_list(token_id, app_id)
  301. friend_wxids = contacts_list['friends'][3:] # 可以调整截取范围
  302. wxid = redis_helper.redis_helper.get_hash_field(hash_key, 'wxid')
  303. wxchat.save_contacts_brief_to_cache(token_id, app_id, wxid, friend_wxids)
  304. print(f'微信ID {wxid} 登录APPID {app_id} 成功,联系人已保存')
  305. def wx_login(wxchat:gewe_chat.GeWeChatCom,tel,token_id,region_id,agent_token_id):
  306. hash_key = f"__AI_OPS_WX__:LOGININFO:{tel}"
  307. login_info = redis_helper.redis_helper.get_hash(hash_key)
  308. if not login_info:
  309. login_info = login_or_reconnect(wxchat, token_id, '', region_id,agent_token_id,hash_key)
  310. else:
  311. app_id = login_info.get('appId')
  312. token_id = login_info.get('tokenId')
  313. wxid= login_info.get('wxid')
  314. # 检查是否已经登录
  315. is_online = wxchat.check_online(token_id, app_id)
  316. if is_online:
  317. logger.info(f'微信ID {wxid} 在APPID {app_id} 已经在线')
  318. else:
  319. # 尝试重连
  320. res = wxchat.reconnection(token_id, app_id)
  321. if res.get('ret') == 200:
  322. logger.info(f'微信ID {wxid} 在APPID {app_id} 重连成功')
  323. else:
  324. print("重连失败,重新登录...")
  325. login_info = login_or_reconnect(wxchat, token_id, app_id, region_id,agent_token_id,hash_key, is_reconnect=True)
  326. if login_info:
  327. fetch_and_save_contacts(wxchat, token_id, login_info.get('appId'), hash_key)
  328. def login_wx_captch_code_process(wxchat:gewe_chat.GeWeChatCom,message):
  329. msg_content = message
  330. cleaned_content = clean_json_string(msg_content)
  331. content = json.loads(cleaned_content)
  332. data = content.get("data", {})
  333. msg_type_data = data.get("msg_type", None)
  334. content_data = data.get("content", {})
  335. token_id = content_data.get("token_id", None)
  336. captch_code = content_data.get("captch_code", None)
  337. wxchat.save_login_wx_captch_code_to_cache(token_id,captch_code)
  338. def ops_messages_process(message):
  339. try:
  340. wxchat = gewe_chat.wxchat
  341. #print(message)
  342. # logger.info(f"接收到kafka消息: {json.dumps(message, separators=(',', ':'), ensure_ascii=False)}")
  343. logger.info(f"接收到kafka消息: {json.dumps(json.loads(message), ensure_ascii=False)}")
  344. msg_content = message
  345. cleaned_content = clean_json_string(msg_content)
  346. content = json.loads(cleaned_content)
  347. data = content.get("data", {})
  348. msg_type_data = data.get("msg_type", None)
  349. content_data = data.get("content", {})
  350. if msg_type_data=="login":
  351. # tel=content_data.get('tel', '18029274615')
  352. # token_id=content_data.get('token_id', 'f828cb3c-1039-489f-b9ae-7494d1778a15')
  353. # region_id=content_data.get('region_id', '440000')
  354. # agent_token_id=content_data.get('agent_token_id', '')
  355. tel=content_data.get('tel', '18733438393')
  356. token_id=content_data.get('token_id', 'c50b7d57-2efa-4a53-8c11-104a06d1e1fa')
  357. region_id=content_data.get('region_id', '440000')
  358. agent_token_id=content_data.get('agent_token_id', 'sk-fAOIdANeGXjWKW5mFybnsNZZGYU2lFLmqVY9rVFaFmjiOaWt3tcWMi')
  359. loginfo=gewe_chat.wxchat.get_login_info_from_cache(tel)
  360. print(loginfo)
  361. status=loginfo.get('status','0')
  362. if status=='1':
  363. logger.info(f'手机号{tel},wx_token{token_id} 已经微信登录,终止登录流程')
  364. return
  365. flag=gewe_chat.wxchat.acquire_login_lock(token_id,800)
  366. if flag:
  367. thread = threading.Thread(target=wx_login, args=(wxchat,tel,token_id,region_id,agent_token_id))
  368. thread.daemon = True
  369. thread.start()
  370. else:
  371. logger.info(f'手机号{tel}, wx_token{token_id} 登录进行中,稍后再试')
  372. elif msg_type_data == 'group-sending':
  373. agent_tel=content_data.get('agent_tel', '18029274615')
  374. # 使用线程处理
  375. #wx_messages_process_callback(agent_tel,message)
  376. thread = threading.Thread(target=wx_messages_process_callback, args=(agent_tel,message,))
  377. thread.daemon = True
  378. thread.start()
  379. elif msg_type_data == 'login_wx_captch_code':
  380. thread = threading.Thread(target=login_wx_captch_code_process, args=(wxchat,message,))
  381. thread.daemon = True
  382. thread.start()
  383. else:
  384. print(f'未处理息类型 {msg_type_data}')
  385. except Exception as e:
  386. print(f"处理消息时发生错误: {e}, 消息内容: {message}")