2021-02-07 11:52:50 +08:00
|
|
|
from datetime import datetime
|
|
|
|
from typing import Any, Optional
|
2021-01-29 21:19:13 +08:00
|
|
|
|
2021-11-22 23:21:26 +08:00
|
|
|
from pydantic import Field, BaseModel
|
2021-01-29 21:19:13 +08:00
|
|
|
|
|
|
|
from nonebot.typing import overrides
|
|
|
|
|
2021-01-30 06:10:04 +08:00
|
|
|
from ..message import MessageChain
|
2021-02-01 00:01:31 +08:00
|
|
|
from .base import Event, GroupChatInfo, PrivateChatInfo
|
2021-01-30 06:10:04 +08:00
|
|
|
|
2021-01-29 21:19:13 +08:00
|
|
|
|
2021-02-07 11:52:50 +08:00
|
|
|
class MessageSource(BaseModel):
|
|
|
|
id: int
|
|
|
|
time: datetime
|
|
|
|
|
|
|
|
|
2021-01-29 21:19:13 +08:00
|
|
|
class MessageEvent(Event):
|
2021-02-01 00:01:31 +08:00
|
|
|
"""消息事件基类"""
|
2021-11-22 23:21:26 +08:00
|
|
|
|
|
|
|
message_chain: MessageChain = Field(alias="messageChain")
|
2021-02-07 11:52:50 +08:00
|
|
|
source: Optional[MessageSource] = None
|
2021-01-30 20:40:00 +08:00
|
|
|
sender: Any
|
2021-01-29 21:19:13 +08:00
|
|
|
|
|
|
|
@overrides(Event)
|
|
|
|
def get_message(self) -> MessageChain:
|
|
|
|
return self.message_chain
|
|
|
|
|
|
|
|
@overrides(Event)
|
|
|
|
def get_plaintext(self) -> str:
|
2021-01-30 21:51:51 +08:00
|
|
|
return self.message_chain.extract_plain_text()
|
2021-01-29 21:19:13 +08:00
|
|
|
|
|
|
|
@overrides(Event)
|
|
|
|
def get_user_id(self) -> str:
|
2021-01-30 20:40:00 +08:00
|
|
|
raise NotImplementedError
|
2021-01-29 21:19:13 +08:00
|
|
|
|
|
|
|
@overrides(Event)
|
|
|
|
def get_session_id(self) -> str:
|
2021-01-30 20:40:00 +08:00
|
|
|
raise NotImplementedError
|
2021-01-29 21:19:13 +08:00
|
|
|
|
|
|
|
|
|
|
|
class GroupMessage(MessageEvent):
|
2021-02-01 00:01:31 +08:00
|
|
|
"""群消息事件"""
|
2021-11-22 23:21:26 +08:00
|
|
|
|
2021-02-01 00:01:31 +08:00
|
|
|
sender: GroupChatInfo
|
2021-02-01 16:31:51 +08:00
|
|
|
to_me: bool = False
|
2021-01-30 20:40:00 +08:00
|
|
|
|
|
|
|
@overrides(MessageEvent)
|
|
|
|
def get_session_id(self) -> str:
|
2021-11-22 23:21:26 +08:00
|
|
|
return f"group_{self.sender.group.id}_" + self.get_user_id()
|
2021-01-29 21:19:13 +08:00
|
|
|
|
2021-02-01 16:31:51 +08:00
|
|
|
@overrides(MessageEvent)
|
|
|
|
def get_user_id(self) -> str:
|
|
|
|
return str(self.sender.id)
|
|
|
|
|
|
|
|
@overrides(MessageEvent)
|
|
|
|
def is_tome(self) -> bool:
|
|
|
|
return self.to_me
|
|
|
|
|
2021-01-29 21:19:13 +08:00
|
|
|
|
|
|
|
class FriendMessage(MessageEvent):
|
2021-02-01 00:01:31 +08:00
|
|
|
"""好友消息事件"""
|
2021-11-22 23:21:26 +08:00
|
|
|
|
2021-02-01 00:01:31 +08:00
|
|
|
sender: PrivateChatInfo
|
2021-01-29 21:19:13 +08:00
|
|
|
|
2021-01-30 20:40:00 +08:00
|
|
|
@overrides(MessageEvent)
|
|
|
|
def get_user_id(self) -> str:
|
|
|
|
return str(self.sender.id)
|
|
|
|
|
2021-02-01 16:31:51 +08:00
|
|
|
@overrides(MessageEvent)
|
2021-01-30 20:40:00 +08:00
|
|
|
def get_session_id(self) -> str:
|
2021-11-22 23:21:26 +08:00
|
|
|
return "friend_" + self.get_user_id()
|
2021-01-30 20:40:00 +08:00
|
|
|
|
2021-02-01 16:31:51 +08:00
|
|
|
@overrides(MessageEvent)
|
|
|
|
def is_tome(self) -> bool:
|
|
|
|
return True
|
|
|
|
|
2021-01-29 21:19:13 +08:00
|
|
|
|
|
|
|
class TempMessage(MessageEvent):
|
2021-02-01 00:01:31 +08:00
|
|
|
"""临时会话消息事件"""
|
2021-11-22 23:21:26 +08:00
|
|
|
|
2021-02-01 00:01:31 +08:00
|
|
|
sender: GroupChatInfo
|
2021-01-30 20:40:00 +08:00
|
|
|
|
2021-02-01 16:31:51 +08:00
|
|
|
@overrides(MessageEvent)
|
2021-01-30 20:40:00 +08:00
|
|
|
def get_session_id(self) -> str:
|
2021-11-22 23:21:26 +08:00
|
|
|
return f"temp_{self.sender.group.id}_" + self.get_user_id()
|
2021-02-01 16:31:51 +08:00
|
|
|
|
|
|
|
@overrides(MessageEvent)
|
|
|
|
def is_tome(self) -> bool:
|
|
|
|
return True
|