From 3ba5b8dfd4bbcb6c29b501e5df49826eb1022091 Mon Sep 17 00:00:00 2001 From: H Vs Date: Sun, 27 Apr 2025 17:54:19 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BE=AE=E4=BF=A1=E7=A7=81=E8=81=8A=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E6=B6=88=E6=81=AF=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/endpoints/pipeline_endpoint.py | 20 +++++++++++++++++++- services/gewe_service.py | 24 ++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/app/endpoints/pipeline_endpoint.py b/app/endpoints/pipeline_endpoint.py index ea868a3..164b850 100644 --- a/app/endpoints/pipeline_endpoint.py +++ b/app/endpoints/pipeline_endpoint.py @@ -186,6 +186,7 @@ async def handle_add_messages_async(request: Request,token_id,msg,wxid): 42: handle_name_card_async, 43: handle_video_async, 49: handle_xml_async, + 51: handle_file_message_async, 37: handle_add_friend_notice_async, 10002: handle_10002_msg_async, 10000: handle_10000_msg_async @@ -1098,7 +1099,7 @@ async def handle_name_card_async(request: Request,token_id,app_id, wxid,msg_data logger.error(f"未知错误: {e}") async def handle_video_async(request: Request,token_id,app_id, wxid,msg_data,from_wxid, to_wxid): - logger.info('视频消息') + logger.info(f'{wxid} 视频消息') try: msg_content_xml=msg_data["Content"]["string"] wx_video_url=await request.app.state.gewe_service.download_video_msg_async(token_id,app_id,msg_content_xml) @@ -1243,6 +1244,23 @@ async def handle_xml_invite_group_async (request: Request,token_id,app_id, wxid, else: logger.warning(f'群聊邀请,同意加入群聊失败 {msg} {data}') +async def handle_file_message_async(request: Request,token_id,app_id, wxid,msg_data,from_wxid, to_wxid): + logger.info(f'{wxid} 微信文件消息处理') + try: + msg_content_xml=msg_data["Content"]["string"] + callback_to_user=from_wxid + wx_file_url=await request.app.state.gewe_service.download_file_msg_async(token_id,app_id,msg_content_xml) + if not wx_file_url: + logger.warning(f'处理微信文件消息异常') + return + file_url=url_file_to_oss(wx_file_url) + wx_content_dialogue_message = [{"type": "file", "file_url": {"url":file_url}}] + k_message = dialogue_message(callback_to_user,wxid, wx_content_dialogue_message) + await request.app.state.kafka_service.send_message_async(k_message) + logger.info("发送对话 %s",k_message) + except Exception as e: + logger.error(f"出现错误: {e}") + async def handle_add_friend_notice_async(request: Request,token_id,app_id, wxid,msg_data,from_wxid, to_wxid): ''' 好友添加请求通知 diff --git a/services/gewe_service.py b/services/gewe_service.py index 4ecaf57..0efa0f9 100644 --- a/services/gewe_service.py +++ b/services/gewe_service.py @@ -708,6 +708,30 @@ class GeWeService: logger.warning(f'下载视频失败 {response.status} {response.reason}') return False + async def download_file_msg_async(self, token_id: str, app_id: str, xml: str): + data = { + "appId": app_id, + "xml": xml + } + headers = { + 'X-GEWE-TOKEN': token_id, + 'Content-Type': 'application/json' + } + url = f'{self.base_url}/v2/api/message/downloadFile' + async with aiohttp.ClientSession() as session: + async with session.post(url, json=data, headers=headers) as response: + if response.ok: + data = await response.json() + if data['ret'] == 200: + return data['data']['fileUrl'] + else: + logger.warning(f'下载视频失败 {data.get("ret", None),data.get("msg", None), data.get("data", None)}') + return False + else: + logger.warning(f'下载视频失败 {response.status} {response.reason}') + return False + + ############################### 群模块 ############################### async def get_chatroom_info_async(self, token_id, app_id, chatroom_id): '''