mirror of
https://github.com/nonebot/nonebot2.git
synced 2025-01-19 17:58:26 +08:00
⚡️ remove unnecessary code
This commit is contained in:
parent
603a63a629
commit
e3c3e370cb
@ -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
|
||||
}
|
||||
}
|
||||
|
@ -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:
|
||||
|
@ -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)
|
||||
]
|
||||
|
Loading…
Reference in New Issue
Block a user