2023-02-22 23:32:48 +08:00
|
|
|
from typing import Type, Union, Mapping, Iterable, Optional
|
2021-12-12 18:19:08 +08:00
|
|
|
|
2021-12-16 23:22:25 +08:00
|
|
|
from pydantic import create_model
|
2021-12-12 18:19:08 +08:00
|
|
|
|
2023-02-22 23:32:48 +08:00
|
|
|
from nonebot.adapters import Event, Message, MessageSegment
|
2021-12-16 23:22:25 +08:00
|
|
|
|
|
|
|
|
2022-02-10 13:15:59 +08:00
|
|
|
def escape_text(s: str, *, escape_comma: bool = True) -> str:
|
|
|
|
s = s.replace("&", "&").replace("[", "[").replace("]", "]")
|
|
|
|
if escape_comma:
|
|
|
|
s = s.replace(",", ",")
|
|
|
|
return s
|
|
|
|
|
|
|
|
|
|
|
|
def make_fake_message():
|
2023-04-04 21:42:01 +08:00
|
|
|
class FakeMessageSegment(MessageSegment["FakeMessage"]):
|
2021-12-20 00:28:02 +08:00
|
|
|
@classmethod
|
|
|
|
def get_message_class(cls):
|
|
|
|
return FakeMessage
|
|
|
|
|
|
|
|
def __str__(self) -> str:
|
2022-01-02 14:29:04 +08:00
|
|
|
return self.data["text"] if self.type == "text" else f"[fake:{self.type}]"
|
2021-12-20 00:28:02 +08:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def text(cls, text: str):
|
|
|
|
return cls("text", {"text": text})
|
|
|
|
|
2022-02-06 18:55:19 +08:00
|
|
|
@staticmethod
|
|
|
|
def image(url: str):
|
|
|
|
return FakeMessageSegment("image", {"url": url})
|
2022-01-02 14:29:04 +08:00
|
|
|
|
2022-02-17 15:06:26 +08:00
|
|
|
@staticmethod
|
2022-02-17 15:35:39 +08:00
|
|
|
def nested(content: "FakeMessage"):
|
|
|
|
return FakeMessageSegment("node", {"content": content})
|
2022-02-17 15:06:26 +08:00
|
|
|
|
2021-12-20 00:28:02 +08:00
|
|
|
def is_text(self) -> bool:
|
2022-01-02 14:29:04 +08:00
|
|
|
return self.type == "text"
|
2021-12-20 00:28:02 +08:00
|
|
|
|
2023-04-04 21:42:01 +08:00
|
|
|
class FakeMessage(Message[FakeMessageSegment]):
|
2021-12-20 00:28:02 +08:00
|
|
|
@classmethod
|
|
|
|
def get_segment_class(cls):
|
|
|
|
return FakeMessageSegment
|
|
|
|
|
|
|
|
@staticmethod
|
2022-01-17 00:29:09 +08:00
|
|
|
def _construct(msg: Union[str, Iterable[Mapping]]):
|
|
|
|
if isinstance(msg, str):
|
|
|
|
yield FakeMessageSegment.text(msg)
|
|
|
|
else:
|
|
|
|
for seg in msg:
|
|
|
|
yield FakeMessageSegment(**seg)
|
|
|
|
return
|
2021-12-20 00:28:02 +08:00
|
|
|
|
2023-04-04 21:42:01 +08:00
|
|
|
def __add__(
|
|
|
|
self, other: Union[str, FakeMessageSegment, Iterable[FakeMessageSegment]]
|
|
|
|
):
|
2022-02-10 13:15:59 +08:00
|
|
|
other = escape_text(other) if isinstance(other, str) else other
|
|
|
|
return super().__add__(other)
|
|
|
|
|
2021-12-20 00:28:02 +08:00
|
|
|
return FakeMessage
|
|
|
|
|
|
|
|
|
2021-12-16 23:22:25 +08:00
|
|
|
def make_fake_event(
|
2023-02-22 23:32:48 +08:00
|
|
|
_base: Optional[Type[Event]] = None,
|
2021-12-16 23:22:25 +08:00
|
|
|
_type: str = "message",
|
|
|
|
_name: str = "test",
|
|
|
|
_description: str = "test",
|
2022-09-29 16:56:06 +08:00
|
|
|
_user_id: Optional[str] = "test",
|
2022-01-20 03:16:04 +08:00
|
|
|
_session_id: Optional[str] = "test",
|
2023-02-22 23:32:48 +08:00
|
|
|
_message: Optional[Message] = None,
|
2021-12-16 23:22:25 +08:00
|
|
|
_to_me: bool = True,
|
|
|
|
**fields,
|
2023-02-22 23:32:48 +08:00
|
|
|
) -> Type[Event]:
|
2022-03-20 19:40:43 +08:00
|
|
|
_Fake = create_model("_Fake", __base__=_base or Event, **fields)
|
2021-12-16 23:22:25 +08:00
|
|
|
|
|
|
|
class FakeEvent(_Fake):
|
|
|
|
def get_type(self) -> str:
|
|
|
|
return _type
|
|
|
|
|
|
|
|
def get_event_name(self) -> str:
|
|
|
|
return _name
|
|
|
|
|
|
|
|
def get_event_description(self) -> str:
|
|
|
|
return _description
|
|
|
|
|
|
|
|
def get_user_id(self) -> str:
|
2022-09-29 16:56:06 +08:00
|
|
|
if _user_id is not None:
|
|
|
|
return _user_id
|
|
|
|
raise NotImplementedError
|
2021-12-16 23:22:25 +08:00
|
|
|
|
|
|
|
def get_session_id(self) -> str:
|
2022-01-20 03:16:04 +08:00
|
|
|
if _session_id is not None:
|
|
|
|
return _session_id
|
|
|
|
raise NotImplementedError
|
2021-12-16 23:22:25 +08:00
|
|
|
|
|
|
|
def get_message(self) -> "Message":
|
|
|
|
if _message is not None:
|
|
|
|
return _message
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
def is_tome(self) -> bool:
|
|
|
|
return _to_me
|
|
|
|
|
|
|
|
class Config:
|
|
|
|
extra = "forbid"
|
|
|
|
|
|
|
|
return FakeEvent
|