消息组件
消息组件
消息组件是什么
聪明的你应该注意到了,当你需要发送图文的时候
await bot.send(event,["我测你的码",Image(file="test.png")])
除了event,还需要一个MessageChain(实际上是列表),这个列表中包含复数个【消息组件】,即Text和Image,这其实就相当于你在群里发我测你的码[图片]
。
关于有哪些元素可用,下面会展示
消息链不能怎么用
就像你不能在群里将语音和文本发送到同一条消息中一样,bot也不能把本来就不能一起发送的元素放在同一个消息链。
在只有一个message_components时可以不用[]把【消息组件】括起来。
await bot.send(event,Image(file="test.png"))
可供使用的消息组件
文件
class File(MessageComponent):
comp_type: str = "file"
file: str = Field(description="文件路径")
def __init__(self, **data):
super().__init__(**data)
if self.file and not (
self.file.startswith("file://")
or self.file.startswith("base64://")
):
# 将相对路径转换为绝对路径并添加 file:// 前缀
abs_path = os.path.abspath(self.file).replace("\\", "/")
self.file = f"file://{abs_path}"
用例
await bot.send(event,File(file=path))
文本
class Text(MessageComponent):
comp_type: str = "text"
text: str = Field(description="纯文本")
def __init__(self, text: str) -> None:
BaseModel.__init__(self, text=text)
def to_cqcode(self) -> str:
return self.text
await bot.send(event,Text("你好"))
或者
await bot.send(event,"你好")
QQ表情
class Face(MessageComponent):
comp_type: str = "face"
id: str = Field(description="QQ 表情 ID")
def __init__(self, id: int | str) -> None:
BaseModel.__init__(self, id=str(id))
用例
await bot.send(event,Face(id=表情id))
图片
class Image(MessageComponent):
comp_type: str = "image"
file: str
def __init__(self, **data):
super().__init__(**data)
if self.file and not (
self.file.startswith("http://")
or self.file.startswith("file://")
or self.file.startswith("base64://")
or self.file.startswith("https://")
):
# 将相对路径转换为绝对路径并添加 file:// 前缀
abs_path = os.path.abspath(self.file).replace("\\", "/")
self.file = f"file://{abs_path}"
用例
await bot.send(event,Image(file=path)) #可以传url,base64,路径
语音
class Record(MessageComponent):
comp_type: str = "record"
file: str = Field(description="语音文件路径")
url: Annotated[Optional[str], OnlySend] = Field(default="",description="语音 URL")
def __init__(self, **data):
super().__init__(**data)
if self.file and not (
self.file.startswith("http://")
or self.file.startswith("file://")
or self.file.startswith("base64://")
or self.file.startswith("https://")
):
# 将相对路径转换为绝对路径并添加 file:// 前缀
abs_path = os.path.abspath(self.file).replace("\\", "/")
self.file = f"file://{abs_path}"
用例
await bot.send(event,Record(file=path)) #可以传url,base64,路径
视频
class Video(MessageComponent):
comp_type: str = "video"
file: Optional[str] = Field(description="视频文件路径")
url: Annotated[Optional[str], OnlyReceive] = Field(description="视频 URL")
await bot.send(event,Video(file=path)) #路径
艾特
class At(MessageComponent):
comp_type: str = "at"
qq: int = Field(description="@的 QQ 号,all 表示全体成员")
def __init__(self, qq: int) -> None:
BaseModel.__init__(self, qq=qq)
await bot.send(event,At(qq=qq号))
rps
class Rps(MessageComponent):
comp_type: str = "rps"
Dice
class Dice(MessageComponent):
comp_type: str = "dice"
shake
class Shake(MessageComponent):
comp_type: str = "shake"
poke
class Poke(MessageComponent):
comp_type: str = "poke"
type: int = Field(description="类型,见 Mirai 的 PokeMessage 类")
id: int = Field(description="ID")
name: Annotated[Optional[str], OnlyReceive] = Field(description="表情名")
Anonymous
class Anonymous(MessageComponent):
comp_type: str = "anonymous"
ignore: Annotated[Optional[bool], OnlySend] = Field(
default=None, description="可选,表示无法匿名时是否继续发送"
)
分享
class Share(MessageComponent):
comp_type: str = "share"
url: str = Field(description="URL")
title: str = Field(description="标题")
content: Optional[str] = Field(description="发送时可选,内容描述")
image: Optional[str] = Field(description="发送时可选,图片 URL")
联系人
class Contact(MessageComponent):
comp_type: str = "contact"
type: Literal["qq", "group"] = Field(description="推荐类型")
id: str = Field(description="被推荐人的 QQ 号或群号")
经纬度
class Location(MessageComponent):
comp_type: str = "location"
lat: str = Field(description="纬度")
lon: str = Field(description="经度")
title: Optional[str] = Field(description="发送时可选,标题")
content: Optional[str] = Field(description="发送时可选,内容描述")
音乐卡片
class Music(MessageComponent):
comp_type: str = "music"
type: str
id: int
卡片
class Card(MessageComponent):
comp_type: str = "music"
type: str="custom"
url: str="https://music.qq.com"
audio: str = Field(description="音频url")
title: str= Field(description="音乐标题")
image: str= Field(description="音乐封面url")
引用回复
class Reply(MessageComponent):
comp_type: str = "reply"
id: int = Field(description="回复时引用的消息 ID")
聊天记录
class Forward(MessageComponent):
comp_type: str = "forward"
id: Annotated[str, OnlyReceive] = Field(description="合并转发 ID")
def __init__(self, id: str) -> None:
BaseModel.__init__(self, id=id)
用这个👇
class Node(MessageComponent):
comp_type: str = "node"
id: str = Field(default="",description="转发的消息 ID")
user_id: str = Field(default="",description="发送者 QQ 号")
nickname: str = Field(default="",description="发送者昵称")
content: str | list[MessageComponent] = Field(description="消息内容")
xml
class Xml(MessageComponent):
comp_type: str = "xml"
data: str = Field(description="XML 内容")
def __init__(self, data: str) -> None:
BaseModel.__init__(self, data=data)
json
class Json(MessageComponent):
comp_type: str = "json"
data: str = Field(description="JSON 内容")
def __init__(self, data: str) -> None:
BaseModel.__init__(self, data=data)
__all__ = [
"Text",
"Face",
"Image",
"Record",
"Video",
"At",
"Rps",
"Dice",
"Shake",
"Poke",
"Anonymous",
"Share",
"Contact",
"Location",
"Music",
"Reply",
"Forward",
"Node",
"Xml",
]