From e3c3e370cbd80560a8a3ecaa666c6f4cebdca339 Mon Sep 17 00:00:00 2001 From: StarHeartHunt Date: Tue, 6 Jul 2021 21:12:49 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=EF=B8=8F=20remove=20unnecessary=20cod?= =?UTF-8?q?e?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../nonebot/adapters/feishu/bot.py | 9 ++------ .../nonebot/adapters/feishu/event.py | 8 +++---- .../nonebot/adapters/feishu/message.py | 23 ++++++++----------- 3 files changed, 15 insertions(+), 25 deletions(-) diff --git a/packages/nonebot-adapter-feishu/nonebot/adapters/feishu/bot.py b/packages/nonebot-adapter-feishu/nonebot/adapters/feishu/bot.py index 2535a4d9..255f4747 100644 --- a/packages/nonebot-adapter-feishu/nonebot/adapters/feishu/bot.py +++ b/packages/nonebot-adapter-feishu/nonebot/adapters/feishu/bot.py @@ -290,12 +290,7 @@ class Bot(BaseBot): raise ValueError( "Cannot guess `receive_id` and `receive_id_type` to reply!") - if isinstance(message, MessageSegment): - msg_type = message.type - elif isinstance(message, Message): - msg_type = message[0].type - else: - msg_type = "text" + msg_type, content = MessageSerializer(msg).serialize() params = { "query": { @@ -303,7 +298,7 @@ class Bot(BaseBot): }, "body": { "receive_id": receive_id, - "content": MessageSerializer(Message(message)).serialize(), + "content": content, "msg_type": msg_type } } diff --git a/packages/nonebot-adapter-feishu/nonebot/adapters/feishu/event.py b/packages/nonebot-adapter-feishu/nonebot/adapters/feishu/event.py index 47cecd9e..9f1edd9a 100644 --- a/packages/nonebot-adapter-feishu/nonebot/adapters/feishu/event.py +++ b/packages/nonebot-adapter-feishu/nonebot/adapters/feishu/event.py @@ -1,14 +1,14 @@ import inspect import json -from typing import Any, List, Literal, Optional, Type, Union +from typing import Any, List, Literal, Optional, Type from pygtrie import StringTrie from pydantic import BaseModel, root_validator, Field from nonebot.adapters import Event as BaseEvent from nonebot.typing import overrides -from .message import Message, MessageDeserializer +from .message import Message, MessageDeserializer, MessageSerializer class EventHeader(BaseModel): @@ -143,7 +143,7 @@ class MessageEvent(Event): f"Message[{super().get_type()}]" f" {self.event.message.message_id} from {self.get_user_id()}" f"@[{self.event.message.chat_type}:{self.event.message.chat_id}]" - f" {self.event.message.content}") + f" {str(self.get_message()) and MessageSerializer(self.get_message()).serialize()}") @overrides(Event) def get_message(self) -> Message: @@ -155,7 +155,7 @@ class MessageEvent(Event): @overrides(Event) def get_user_id(self) -> str: - return self.event.sender.sender_id.union_id + return self.event.sender.sender_id.user_id @overrides(Event) def get_session_id(self) -> str: diff --git a/packages/nonebot-adapter-feishu/nonebot/adapters/feishu/message.py b/packages/nonebot-adapter-feishu/nonebot/adapters/feishu/message.py index f3bfa292..111b9ce0 100644 --- a/packages/nonebot-adapter-feishu/nonebot/adapters/feishu/message.py +++ b/packages/nonebot-adapter-feishu/nonebot/adapters/feishu/message.py @@ -186,12 +186,8 @@ class MessageSerializer: """ message: Message - def serialize(self): - for segment in self.message: - if segment.type == "post": - raise NotImplementedError - else: - return json.dumps(segment.data) + def serialize(self) -> Tuple[str, str]: + return self.message[0].type, json.dumps(self.message[0].data) @dataclass @@ -199,13 +195,13 @@ class MessageDeserializer: """ 飞书 协议 Message 反序列化器。 """ - data: Dict[str, Any] type: str + data: Dict[str, Any] - def deserialize(self): + def deserialize(self) -> Message: print(self.type, self.data) if self.type == "post": - return self._parse_rich_text(self.data) + return Message(self._parse_rich_text(self.data)) else: return Message(MessageSegment(self.type, self.data)) @@ -221,8 +217,7 @@ class MessageDeserializer: tag = element.get("tag") yield tag, element - temp = Message() - for type_, data in _iter_message(message_data): - temp += MessageSegment(type_, data) - - return temp + return [ + MessageSegment(type_, data) + for type_, data in _iter_message(message_data) + ]