From 35506dd2bbb3b321fe9f6bda27e473d2c8021f16 Mon Sep 17 00:00:00 2001 From: fdc310 <82008029+fdc310@users.noreply.github.com> Date: Sat, 31 Jan 2026 20:31:01 +0800 Subject: [PATCH] feat: add card auto layout configuration for DingTalk adapter (#1952) * feat: add card auto layout configuration for DingTalk adapter * fix: correct card auto layout configuration key and improve related logic * fix: simplify card auto layout configuration logic in create_and_card method * fix: correct card auto layout key in DingTalk migration configuration * fix: correct migration class name for DingTalk card auto layout * fix: update migration version for DingTalk card auto layout * fix: correct key name for card auto layout in DingTalk configuration * fix: improve formatting and consistency in DingTalk card auto layout methods --- src/langbot/libs/dingtalk_api/api.py | 11 ++++++++--- .../m041_dingtalk_card_autolayout_config.py | 17 +++++++++++++++++ src/langbot/pkg/platform/sources/dingtalk.py | 5 ++++- src/langbot/pkg/platform/sources/dingtalk.yaml | 7 +++++++ 4 files changed, 36 insertions(+), 4 deletions(-) create mode 100644 src/langbot/pkg/core/migrations/m041_dingtalk_card_autolayout_config.py diff --git a/src/langbot/libs/dingtalk_api/api.py b/src/langbot/libs/dingtalk_api/api.py index e39e12bf..533bcfe8 100644 --- a/src/langbot/libs/dingtalk_api/api.py +++ b/src/langbot/libs/dingtalk_api/api.py @@ -347,10 +347,15 @@ class DingTalkClient: raise Exception(f'failed to send proactive massage to group: {traceback.format_exc()}') async def create_and_card( - self, temp_card_id: str, incoming_message: dingtalk_stream.ChatbotMessage, quote_origin: bool = False + self, + temp_card_id: str, + incoming_message: dingtalk_stream.ChatbotMessage, + quote_origin: bool = False, + card_auto_layout: bool = False, ): - content_key = 'content' - card_data = {content_key: ''} + card_data = {} + card_data['config'] = json.dumps({'autoLayout': card_auto_layout}) + card_data['content'] = '' card_instance = dingtalk_stream.AICardReplier(self.client, incoming_message) # print(card_instance) diff --git a/src/langbot/pkg/core/migrations/m041_dingtalk_card_autolayout_config.py b/src/langbot/pkg/core/migrations/m041_dingtalk_card_autolayout_config.py new file mode 100644 index 00000000..58968d4d --- /dev/null +++ b/src/langbot/pkg/core/migrations/m041_dingtalk_card_autolayout_config.py @@ -0,0 +1,17 @@ +from __future__ import annotations + +from .. import migration + + +@migration.migration_class('dingtalk_card_auto_layout', 41) +class DingTalkCardAutoLayoutMigration(migration.Migration): + """迁移""" + + async def need_migrate(self) -> bool: + """判断当前环境是否需要运行此迁移""" + return True + + async def run(self): + """执行迁移""" + self.ap.platform_cfg.data['platform-adapters']['app']['dingtalk']['card_auto_layout'] = False + await self.ap.platform_cfg.dump_config() diff --git a/src/langbot/pkg/platform/sources/dingtalk.py b/src/langbot/pkg/platform/sources/dingtalk.py index 6c4988c4..bd9d0fa3 100644 --- a/src/langbot/pkg/platform/sources/dingtalk.py +++ b/src/langbot/pkg/platform/sources/dingtalk.py @@ -231,7 +231,10 @@ class DingTalkAdapter(abstract_platform_adapter.AbstractMessagePlatformAdapter): card_template_id = self.config['card_template_id'] incoming_message = event.source_platform_object.incoming_message # message_id = incoming_message.message_id - card_instance, card_instance_id = await self.bot.create_and_card(card_template_id, incoming_message) + card_auto_layout = self.config.get('card_ auto_layout', False) + card_instance, card_instance_id = await self.bot.create_and_card( + card_template_id, incoming_message, card_auto_layout=card_auto_layout + ) self.card_instance_id_dict[message_id] = (card_instance, card_instance_id) return True diff --git a/src/langbot/pkg/platform/sources/dingtalk.yaml b/src/langbot/pkg/platform/sources/dingtalk.yaml index 70855c2b..c5cb2f85 100644 --- a/src/langbot/pkg/platform/sources/dingtalk.yaml +++ b/src/langbot/pkg/platform/sources/dingtalk.yaml @@ -56,6 +56,13 @@ spec: type: boolean required: true default: false + - name: card_auto_layout + label: + en_US: Card Auto Layout + zh_Hans: 卡片宽屏自动布局 + type: boolean + required: false + default: false - name: card_template_id label: en_US: card template id