iamai.message module#

iamai 消息。

实现了常用的基本消息 Message 和消息字段 MessageSegment 模型供适配器使用。 适配器开发者可以根据需要实现此模块中消息类的子类或定义与此不同的消息类型,但建议若可行的话应尽量使用此模块中消息类的子类。

class iamai.message.Message(*messages: List[MessageSegmentT] | MessageSegmentT | str | Mapping[str, Any])[source]#

Bases: ABC, List[MessageSegmentT]

消息。

本类是 List 的子类,并重写了 __init__() 方法, 可以直接处理 str, Mapping, MessageSegment, List[MessageSegment]。 本类重载了 ++= 运算符,可以直接和 Message, MessageSegment 等类型的对象执行取和运算。 适配器开发者需要继承本类并重写 get_segment_class() 方法。

copy() Self[source]#

返回自身的浅复制。

Returns:

自身的浅复制。

endswith(suffix: str | MessageSegmentT, start: SupportsIndex | None = None, end: SupportsIndex | None = None) bool[source]#

实现类似字符串的 endswith() 方法。

suffix 类型是 str 时,会将自身转换为 str 类型,再调用 str 类型的 endswith() 方法。 当 suffix 类型是 MessageSegment 时,startend 参数将不其作用,

会判断列表的最后一个消息字段是否和 suffix 相等。

Parameters:
  • suffix – 后缀。

  • start – 开始检查位置。

  • end – 停止检查位置。

Returns:

检查结果。

get_plain_text() str[source]#

获取消息中的纯文本部分。

Returns:

消息中的纯文本部分。

abstract classmethod get_segment_class() Type[MessageSegmentT][source]#

获取消息字段类。

Returns:

消息字段类。

is_text() bool[source]#

是否是纯文本消息。

replace(old: str, new: str, count: int = -1) Self[source]#
replace(old: MessageSegmentT, new: MessageSegmentT | None, count: int = -1) Self

实现类似字符串的 replace() 方法。

oldstr 类型时,new 也必须是 str,本方法将仅对 is_text()True 的消息字段进行处理。 当 old 为 MessageSegment 类型时,new 可以是 MessageSegmentNone,本方法将对所有消息字段进行处理,

并替换符合条件的消息字段。None 表示删除匹配到的消息字段。

Parameters:
  • old – 被匹配的字符串或消息字段。

  • new – 被替换为的字符串或消息字段。

  • count – 替换的次数。

Returns:

替换后的消息对象。

startswith(prefix: str | MessageSegmentT, start: SupportsIndex | None = None, end: SupportsIndex | None = None) bool[source]#

实现类似字符串的 startswith() 方法。

prefix 类型是 str 时,会将自身转换为 str 类型,再调用 str 类型的 startswith() 方法。 当 prefix 类型是 MessageSegment 时,startend 参数将不其作用,

会判断列表的第一个消息字段是否和 prefix 相等。

Parameters:
  • prefix – 前缀。

  • start – 开始检查位置。

  • end – 停止检查位置。

Returns:

检查结果。

class iamai.message.MessageSegment(*, type: str, data: Dict[str, Any] = None)[source]#

Bases: ABC, BaseModel, Mapping[str, Any], Generic[MessageT]

消息字段。

本类实现了所有 Mapping 类型的方法,这些方法全部是对 data 属性的操作。 本类重写了 ++= 运算符,可以直接和 Message, MessageSegment 等类型的对象执行取和运算,返回 Message 对象。 适配器开发者需要继承本类并重写 get_message_class() 方法。

type#

消息字段类型。

Type:

str

data#

消息字段内容。

Type:

Dict[str, Any]

data: Dict[str, Any]#
classmethod from_mapping(msg: Mapping[Any, Any]) Self[source]#

用于将 Mapping 转换为消息字段。

如有需要,子类可重写此方法以更改对 Mapping 的默认行为。

Parameters:

msg – 要解析为消息字段的数据。

Returns:

由 Mapping 转换的消息字段。

abstract classmethod from_str(msg: str) Self[source]#

用于将 str 转换为消息字段,子类应重写此方法。

Parameters:

msg – 要解析为消息字段的数据。

Returns:

str 转换的消息字段。

get(key: str, default: Any | None = None) Any[source]#

如果 key 存在于 data 字典中则返回 key 的值,否则返回 default

abstract classmethod get_message_class() Type[MessageT][source]#

获取消息类。

Returns:

消息类。

is_text() bool[source]#

是否是纯文本消息字段。

Returns:

是否是纯文本消息字段。

items() ItemsView[str, Any][source]#

返回由 data 字典项 ((键, 值) 对) 组成的一个新视图。

keys() KeysView[str][source]#

返回由 data 字典键组成的一个新视图。

model_computed_fields: ClassVar[dict[str, ComputedFieldInfo]] = {}#

A dictionary of computed field names and their corresponding ComputedFieldInfo objects.

model_config: ClassVar[ConfigDict] = {}#

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

model_fields: ClassVar[dict[str, FieldInfo]] = {'data': FieldInfo(annotation=Dict[str, Any], required=False, default_factory=dict), 'type': FieldInfo(annotation=str, required=True)}#

Metadata about the fields defined on the model, mapping of field names to [FieldInfo][pydantic.fields.FieldInfo].

This replaces Model.__fields__ from Pydantic V1.

type: str#
values() ValuesView[Any][source]#

返回由 data 字典值组成的一个新视图。