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/core/migrations/m038_tg_dingtalk_markdown.py b/pkg/core/migrations/m038_tg_dingtalk_markdown.py new file mode 100644 index 00000000..1123c6b2 --- /dev/null +++ b/pkg/core/migrations/m038_tg_dingtalk_markdown.py @@ -0,0 +1,26 @@ +from __future__ import annotations + +from .. import migration + + +@migration.migration_class("tg-dingtalk-markdown", 38) +class TgDingtalkMarkdownMigration(migration.Migration): + """迁移""" + + async def need_migrate(self) -> bool: + """判断当前环境是否需要运行此迁移""" + + for adapter in self.ap.platform_cfg.data['platform-adapters']: + if adapter['adapter'] in ['dingtalk','telegram']: + if 'markdown_card' not in adapter: + return True + return False + + async def run(self): + """执行迁移""" + for adapter in self.ap.platform_cfg.data['platform-adapters']: + if adapter['adapter'] in ['dingtalk','telegram']: + if 'markdown_card' not in adapter: + adapter['markdown_card'] = False + await self.ap.platform_cfg.dump_config() + \ No newline at end of file diff --git a/pkg/core/stages/migrate.py b/pkg/core/stages/migrate.py index fe0dc464..a12129f2 100644 --- a/pkg/core/stages/migrate.py +++ b/pkg/core/stages/migrate.py @@ -12,7 +12,7 @@ from ..migrations import m020_wecom_config, m021_lark_config, m022_lmstudio_conf from ..migrations import m026_qqofficial_config, m027_wx_official_account_config, m028_aliyun_requester_config from ..migrations import m029_dashscope_app_api_config, m030_lark_config_cmpl, m031_dingtalk_config, m032_volcark_config from ..migrations import m033_dify_thinking_config, m034_gewechat_file_url_config, m035_wxoa_mode, m036_wxoa_loading_message -from ..migrations import m037_mcp_config +from ..migrations import m037_mcp_config, m038_tg_dingtalk_markdown @stage.stage_class("MigrationStage") 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", diff --git a/templates/schema/platform.json b/templates/schema/platform.json index 4bc6f111..3819a993 100644 --- a/templates/schema/platform.json +++ b/templates/schema/platform.json @@ -446,6 +446,11 @@ "type": "string", "default": "", "description": "钉钉的robot_name" + }, + "markdown_card": { + "type": "boolean", + "default": false, + "description": "是否使用 Markdown 卡片发送消息" } } }, @@ -466,6 +471,11 @@ "type": "string", "default": "", "description": "Telegram 的 token" + }, + "markdown_card": { + "type": "boolean", + "default": false, + "description": "是否使用 Markdown 卡片发送消息" } } }