From 47acb63feba9f302e738117b30f7d94a72828281 Mon Sep 17 00:00:00 2001 From: wangcham Date: Tue, 1 Apr 2025 07:11:48 -0400 Subject: [PATCH] add support for markdown card in dingtalk & tg --- libs/dingtalk_api/api.py | 8 ++++++-- pkg/platform/sources/dingtalk.py | 3 ++- pkg/platform/sources/telegram.py | 3 +++ templates/platform.json | 6 ++++-- 4 files changed, 15 insertions(+), 5 deletions(-) diff --git a/libs/dingtalk_api/api.py b/libs/dingtalk_api/api.py index fa4d0421..789b2e95 100644 --- a/libs/dingtalk_api/api.py +++ b/libs/dingtalk_api/api.py @@ -10,7 +10,7 @@ import traceback class DingTalkClient: - def __init__(self, client_id: str, client_secret: str,robot_name:str,robot_code:str): + def __init__(self, client_id: str, client_secret: str,robot_name:str,robot_code:str,markdown_card:bool): """初始化 WebSocket 连接并自动启动""" self.credential = dingtalk_stream.Credential(client_id, client_secret) self.client = dingtalk_stream.DingTalkStreamClient(self.credential) @@ -26,6 +26,7 @@ class DingTalkClient: self.robot_name = robot_name self.robot_code = robot_code self.access_token_expiry_time = '' + self.markdown_card = markdown_card @@ -128,7 +129,10 @@ class DingTalkClient: async def send_message(self,content:str,incoming_message): - self.EchoTextHandler.reply_text(content,incoming_message) + if self.markdown_card: + self.EchoTextHandler.reply_markdown(title=self.robot_name+'的回答',text=content,incoming_message=incoming_message) + else: + self.EchoTextHandler.reply_text(content,incoming_message) async def get_incoming_message(self): diff --git a/pkg/platform/sources/dingtalk.py b/pkg/platform/sources/dingtalk.py index aa768039..94a7d249 100644 --- a/pkg/platform/sources/dingtalk.py +++ b/pkg/platform/sources/dingtalk.py @@ -131,7 +131,8 @@ class DingTalkAdapter(adapter.MessagePlatformAdapter): client_id=config["client_id"], client_secret=config["client_secret"], robot_name = config["robot_name"], - robot_code=config["robot_code"] + robot_code=config["robot_code"], + markdown_card=config["markdown_card"] ) async def reply_message( diff --git a/pkg/platform/sources/telegram.py b/pkg/platform/sources/telegram.py index 49822673..b8c4a4b7 100644 --- a/pkg/platform/sources/telegram.py +++ b/pkg/platform/sources/telegram.py @@ -207,6 +207,9 @@ class TelegramAdapter(adapter.MessagePlatformAdapter): "text": component['text'], } + if self.config['markdown_card'] is True: + args["parse_mode"] = "MarkdownV2" + if quote_origin: args['reply_to_message_id'] = message_source.source_platform_object.message.id diff --git a/templates/platform.json b/templates/platform.json index a2fbd36e..dae9a21a 100644 --- a/templates/platform.json +++ b/templates/platform.json @@ -88,12 +88,14 @@ "client_id":"", "client_secret":"", "robot_code":"", - "robot_name":"" + "robot_name":"", + "markdown_card":false }, { "adapter":"telegram", "enable": false, - "token":"" + "token":"", + "markdown_card":false }, { "adapter":"slack",