From 3c0495fc512cc0e8070fa0e8850c1de27484eca9 Mon Sep 17 00:00:00 2001 From: xiaolou Date: Fri, 27 Mar 2026 00:17:26 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E9=92=89=E9=92=89?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E6=B6=88=E6=81=AF=E8=A7=A3=E6=9E=90=E5=A4=B1?= =?UTF-8?q?=E6=95=88=E9=97=AE=E9=A2=98=EF=BC=88=E4=BC=98=E5=8C=96=20downlo?= =?UTF-8?q?adCode=20=E6=8F=90=E5=8F=96=E9=80=BB=E8=BE=91=EF=BC=89=20(#2080?= =?UTF-8?q?)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix: resolve dingtalk file parsing issue by extracting downloadCode from content * style: fix ruff format trailing whitespace --------- Co-authored-by: RockChinQ --- src/langbot/libs/dingtalk_api/api.py | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/src/langbot/libs/dingtalk_api/api.py b/src/langbot/libs/dingtalk_api/api.py index 533bcfe8..7ed502d9 100644 --- a/src/langbot/libs/dingtalk_api/api.py +++ b/src/langbot/libs/dingtalk_api/api.py @@ -272,15 +272,30 @@ class DingTalkClient: message_data['Type'] = 'audio' elif incoming_message.message_type == 'file': - down_list = incoming_message.get_down_list() - if len(down_list) >= 2: - message_data['File'] = await self.get_file_url(down_list[0]) - message_data['Name'] = down_list[1] + # 获取原始数据字典并提取嵌套的文件信息 + raw_data = incoming_message.to_dict() + file_info = raw_data.get('content', {}) + + # 兼容处理:如果 content 仍为 JSON 字符串则进行解析 + if isinstance(file_info, str): + try: + file_info = json.loads(file_info) + except (json.JSONDecodeError, TypeError): + file_info = {} + + download_code = file_info.get('downloadCode') + file_name = file_info.get('fileName') + + if download_code and file_name: + # 转换 downloadCode 为可下载的真实 URL + message_data['File'] = await self.get_file_url(download_code) + message_data['Name'] = file_name else: if self.logger: - await self.logger.error(f'get_down_list() returned fewer than 2 elements: {down_list}') + await self.logger.error(f'Failed to extract file info from message content: {file_info}') message_data['File'] = None message_data['Name'] = None + message_data['Type'] = 'file' copy_message_data = message_data.copy()