mirror of
https://github.com/nonebot/nonebot2.git
synced 2024-12-01 01:25:07 +08:00
✨ implement rich text serializer
This commit is contained in:
parent
f597303698
commit
7c7994e2e2
@ -58,6 +58,15 @@ class MessageSegment(BaseMessageSegment["Message"]):
|
|||||||
def is_text(self) -> bool:
|
def is_text(self) -> bool:
|
||||||
return self.type == "text"
|
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
|
@staticmethod
|
||||||
def text(text: str) -> "MessageSegment":
|
def text(text: str) -> "MessageSegment":
|
||||||
return MessageSegment("text", {"text": text})
|
return MessageSegment("text", {"text": text})
|
||||||
@ -179,7 +188,23 @@ class MessageSerializer:
|
|||||||
message: Message
|
message: Message
|
||||||
|
|
||||||
def serialize(self) -> Tuple[str, str]:
|
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
|
@dataclass
|
||||||
|
Loading…
Reference in New Issue
Block a user