From 7c7994e2e2a40168219b0b130aeacb3e2d98acaa Mon Sep 17 00:00:00 2001 From: StarHeartHunt Date: Thu, 8 Jul 2021 16:12:43 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20implement=20rich=20text=20serialize?= =?UTF-8?q?r?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../nonebot/adapters/feishu/message.py | 27 ++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/packages/nonebot-adapter-feishu/nonebot/adapters/feishu/message.py b/packages/nonebot-adapter-feishu/nonebot/adapters/feishu/message.py index ecef3800..9bcc45f8 100644 --- a/packages/nonebot-adapter-feishu/nonebot/adapters/feishu/message.py +++ b/packages/nonebot-adapter-feishu/nonebot/adapters/feishu/message.py @@ -58,6 +58,15 @@ class MessageSegment(BaseMessageSegment["Message"]): def is_text(self) -> bool: return self.type == "text" + #接收消息 + @staticmethod + def at(user_id: str, user_name: str) -> "MessageSegment": + return MessageSegment("at", { + "user_id": user_id, + "user_name": user_name + }) + + #发送消息 @staticmethod def text(text: str) -> "MessageSegment": return MessageSegment("text", {"text": text}) @@ -179,7 +188,23 @@ class MessageSerializer: message: Message def serialize(self) -> Tuple[str, str]: - return self.message[0].type, json.dumps(self.message[0].data) + segments = list(self.message) + last_segment_type: str = "" + if len(segments) > 1: + msg = {"title": "", "content": [[]]} + for segment in segments: + if segment == "image": + if last_segment_type != "image": + msg["content"].append([]) + else: + if last_segment_type == "image": + msg["content"].append([]) + msg["content"][-1].append({"tag": segment.type, **segment.data}) + last_segment_type = segment.type + return "post", json.dumps(msg) + + else: + return self.message[0].type, json.dumps(self.message[0].data) @dataclass