mirror of
https://github.com/nonebot/nonebot2.git
synced 2024-11-28 08:12:14 +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:
|
||||
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,6 +188,22 @@ class MessageSerializer:
|
||||
message: Message
|
||||
|
||||
def serialize(self) -> Tuple[str, str]:
|
||||
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)
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user