From 256d24718b2f90cb2dcca87564297d27696dac74 Mon Sep 17 00:00:00 2001 From: Guanchao Wang Date: Wed, 14 May 2025 22:55:16 +0800 Subject: [PATCH] fix: dingtalk & wecom problems (#1424) --- libs/dingtalk_api/api.py | 19 +++++++++++++------ libs/wecom_api/api.py | 6 +++--- pkg/platform/sources/dingtalk.py | 11 ++++++++--- 3 files changed, 24 insertions(+), 12 deletions(-) diff --git a/libs/dingtalk_api/api.py b/libs/dingtalk_api/api.py index b66d72a5..cd4ea136 100644 --- a/libs/dingtalk_api/api.py +++ b/libs/dingtalk_api/api.py @@ -115,13 +115,20 @@ class DingTalkClient: if event: await self._handle_message(event) - async def send_message(self, content: str, incoming_message): + async def send_message(self, content: str, incoming_message,at:bool): if self.markdown_card: - self.EchoTextHandler.reply_markdown( - title=self.robot_name + '的回答', - text=content, - incoming_message=incoming_message, - ) + if at: + self.EchoTextHandler.reply_markdown( + title='@'+incoming_message.sender_nick+' '+content, + text='@'+incoming_message.sender_nick+' '+content, + incoming_message=incoming_message, + ) + else: + self.EchoTextHandler.reply_markdown( + title=content, + text=content, + incoming_message=incoming_message, + ) else: self.EchoTextHandler.reply_text(content, incoming_message) diff --git a/libs/wecom_api/api.py b/libs/wecom_api/api.py index f4a62be0..9b3191c2 100644 --- a/libs/wecom_api/api.py +++ b/libs/wecom_api/api.py @@ -29,7 +29,6 @@ class WecomClient: self.access_token = '' self.secret_for_contacts = contacts_secret self.app = Quart(__name__) - self.wxcpt = WXBizMsgCrypt(self.token, self.aes, self.corpid) self.app.add_url_rule( '/callback/command', 'handle_callback', @@ -171,16 +170,17 @@ class WecomClient: timestamp = request.args.get('timestamp') nonce = request.args.get('nonce') + wxcpt = WXBizMsgCrypt(self.token, self.aes, self.corpid) if request.method == 'GET': echostr = request.args.get('echostr') - ret, reply_echo_str = self.wxcpt.VerifyURL(msg_signature, timestamp, nonce, echostr) + ret, reply_echo_str = wxcpt.VerifyURL(msg_signature, timestamp, nonce, echostr) if ret != 0: raise Exception(f'验证失败,错误码: {ret}') return reply_echo_str elif request.method == 'POST': encrypt_msg = await request.data - ret, xml_msg = self.wxcpt.DecryptMsg(encrypt_msg, msg_signature, timestamp, nonce) + ret, xml_msg = wxcpt.DecryptMsg(encrypt_msg, msg_signature, timestamp, nonce) if ret != 0: raise Exception(f'消息解密失败,错误码: {ret}') diff --git a/pkg/platform/sources/dingtalk.py b/pkg/platform/sources/dingtalk.py index 433ef836..836bd851 100644 --- a/pkg/platform/sources/dingtalk.py +++ b/pkg/platform/sources/dingtalk.py @@ -14,9 +14,14 @@ import datetime class DingTalkMessageConverter(adapter.MessageConverter): @staticmethod async def yiri2target(message_chain: platform_message.MessageChain): + content = '' + at = False for msg in message_chain: + if type(msg) is platform_message.At: + at = True if type(msg) is platform_message.Plain: - return msg.text + content += msg.text + return content,at @staticmethod async def target2yiri(event: DingTalkEvent, bot_name: str): @@ -128,8 +133,8 @@ class DingTalkAdapter(adapter.MessagePlatformAdapter): ) incoming_message = event.incoming_message - content = await DingTalkMessageConverter.yiri2target(message) - await self.bot.send_message(content, incoming_message) + content,at = await DingTalkMessageConverter.yiri2target(message) + await self.bot.send_message(content, incoming_message,at) async def send_message(self, target_type: str, target_id: str, message: platform_message.MessageChain): content = await DingTalkMessageConverter.yiri2target(message)