diff --git a/libs/dingtalk_api/EchoHandler.py b/libs/dingtalk_api/EchoHandler.py index 00ff6c72..4baad637 100644 --- a/libs/dingtalk_api/EchoHandler.py +++ b/libs/dingtalk_api/EchoHandler.py @@ -1,4 +1,5 @@ import asyncio +import json import dingtalk_stream from dingtalk_stream import AckMessage @@ -17,7 +18,7 @@ class EchoTextHandler(dingtalk_stream.ChatbotHandler): await self.client.update_incoming_message(incoming_message) return AckMessage.STATUS_OK, 'OK' - + async def get_incoming_message(self): """异步等待消息的到来""" while self.incoming_message is None: diff --git a/libs/dingtalk_api/api.py b/libs/dingtalk_api/api.py index 905557a6..1965d33b 100644 --- a/libs/dingtalk_api/api.py +++ b/libs/dingtalk_api/api.py @@ -92,8 +92,31 @@ class DingTalkClient: base64_str = base64.b64encode(file_bytes).decode('utf-8') # 返回字符串格式 return base64_str else: - raise Exception("获取图片失败") - + raise Exception("获取文件失败") + + async def get_audio_url(self,download_code:str): + if not await self.check_access_token(): + await self.get_access_token() + url = 'https://api.dingtalk.com/v1.0/robot/messageFiles/download' + params = { + "downloadCode":download_code, + "robotCode":self.robot_code + } + headers ={ + "x-acs-dingtalk-access-token": self.access_token + } + async with httpx.AsyncClient() as client: + response = await client.post(url, headers=headers, json=params) + if response.status_code == 200: + result = response.json() + download_url = result.get("downloadUrl") + if download_url: + return await self.download_url_to_base64(download_url) + else: + raise Exception("获取音频失败") + else: + raise Exception(f"Error: {response.status_code}, {response.text}") + async def update_incoming_message(self, message): """异步更新 DingTalkClient 中的 incoming_message""" message_data = await self.get_message(message) @@ -160,10 +183,14 @@ class DingTalkClient: message_data['Picture'] = await self.download_image(incoming_message.get_image_list()[0]) message_data['Type'] = 'image' + elif incoming_message.message_type == 'audio': + message_data['Audio'] = await self.get_audio_url(incoming_message.to_dict()['content']['downloadCode']) + + message_data['Type'] = 'audio' # 删掉开头的@消息 - if message_data["Content"].startswith("@"+self.robot_name): - message_data["Content"][len("@"+self.robot_name):] + if 'Content' in message_data and message_data["Content"].startswith("@"+self.robot_name): + message_data["Content"] = message_data["Content"][len("@"+self.robot_name):] except Exception: traceback.print_exc() diff --git a/libs/dingtalk_api/dingtalkevent.py b/libs/dingtalk_api/dingtalkevent.py index 8f6bdfc1..796c7114 100644 --- a/libs/dingtalk_api/dingtalkevent.py +++ b/libs/dingtalk_api/dingtalkevent.py @@ -25,6 +25,10 @@ class DingTalkEvent(dict): @property def picture(self): return self.get("Picture","") + + @property + def audio(self): + return self.get("Audio","") @property def conversation(self): diff --git a/pkg/platform/sources/dingtalk.py b/pkg/platform/sources/dingtalk.py index 7d1fc1e0..04b5b9c8 100644 --- a/pkg/platform/sources/dingtalk.py +++ b/pkg/platform/sources/dingtalk.py @@ -38,6 +38,8 @@ class DingTalkMessageConverter(adapter.MessageConverter): yiri_msg_list.append(platform_message.Plain(text=event.content)) if event.picture: yiri_msg_list.append(platform_message.Image(base64=event.picture)) + if event.audio: + yiri_msg_list.append(platform_message.Voice(base64=event.audio)) chain = platform_message.MessageChain(yiri_msg_list) @@ -167,7 +169,7 @@ class DingTalkAdapter(adapter.MessagePlatformAdapter): self.bot.on_message("GroupMessage")(on_message) async def run_async(self): - + self.ap.logger.debug(f'钉钉机器人启动') await self.bot.start() async def kill(self) -> bool: