v11.adapter¶
v11 适配器¶
- class melobot.protocols.onebot.v11.Adapter[源代码]¶
基类:
Adapter
[EventFactory
,OutputFactory
,EchoFactory
,Action
,BaseIO
,BaseIO
]- when_validate_error(validate_type: Literal['event', 'echo']) Any], <class 'Exception'>), None]] [源代码]¶
- 参数:
validate_type (Literal['event', 'echo'])
- 返回类型:
Callable[[AsyncCallable[(dict[str, typing_extensions.Any], <class 'Exception'>), None]], AsyncCallable[(dict[str, typing_extensions.Any], <class 'Exception'>), None]]
- async call_output(action: Action) tuple[ActionHandle, ...] [源代码]¶
输出行为的底层方法
- 参数:
action (Action) -- 行为对象
- 返回:
ActionHandle
元组- 返回类型:
tuple[ActionHandle, ...]
- with_echo(func: AsyncCallable[P, tuple[ActionHandle[EchoT | None], ...]]) AsyncCallable[P, tuple[ActionHandle[EchoT], ...]] [源代码]¶
- 参数:
func (AsyncCallable[~P, tuple[ActionHandle[EchoT | None], ...]])
- 返回类型:
AsyncCallable[~P, tuple[ActionHandle[EchoT], ...]]
- async send(msgs: str | Segment | Iterable[Segment] | dict | Iterable[dict]) tuple[ActionHandle[SendMsgEcho | None], ...] [源代码]¶
- 参数:
msgs (str | Segment | Iterable[Segment] | dict | Iterable[dict])
- 返回类型:
tuple[ActionHandle[SendMsgEcho | None], ...]
- async send_reply(msgs: str | Segment | Iterable[Segment] | dict | Iterable[dict]) tuple[ActionHandle[SendMsgEcho | None], ...] [源代码]¶
- 参数:
msgs (str | Segment | Iterable[Segment] | dict | Iterable[dict])
- 返回类型:
tuple[ActionHandle[SendMsgEcho | None], ...]
- async send_custom(msgs: str | Segment | Iterable[Segment] | dict | Iterable[dict], user_id: int | None = None, group_id: int | None = None) tuple[ActionHandle[SendMsgEcho | None], ...] [源代码]¶
- async send_forward(msgs: Iterable[NodeSegment]) tuple[ActionHandle[SendForwardMsgEcho | None], ...] [源代码]¶
- 参数:
msgs (Iterable[NodeSegment])
- 返回类型:
tuple[ActionHandle[SendForwardMsgEcho | None], ...]
- async send_forward_custom(msgs: Iterable[NodeSegment] | Iterable[dict], user_id: int | None = None, group_id: int | None = None) tuple[ActionHandle[SendForwardMsgEcho | None], ...] [源代码]¶
- 参数:
- 返回类型:
tuple[ActionHandle[SendForwardMsgEcho | None], ...]
- async delete_msg(msg_id: int) tuple[ActionHandle[EmptyEcho | None], ...] [源代码]¶
- 参数:
msg_id (int)
- 返回类型:
tuple[ActionHandle[EmptyEcho | None], ...]
- async get_msg(msg_id: int) tuple[ActionHandle[GetMsgEcho | None], ...] [源代码]¶
- 参数:
msg_id (int)
- 返回类型:
tuple[ActionHandle[GetMsgEcho | None], ...]
- async get_forward_msg(forward_id: str) tuple[ActionHandle[GetForwardMsgEcho | None], ...] [源代码]¶
- 参数:
forward_id (str)
- 返回类型:
tuple[ActionHandle[GetForwardMsgEcho | None], ...]
- async send_like(user_id: int, times: int = 1) tuple[ActionHandle[EmptyEcho | None], ...] [源代码]¶
- 参数:
- 返回类型:
tuple[ActionHandle[EmptyEcho | None], ...]
- async set_group_kick(group_id: int, user_id: int, later_reject: bool = False) tuple[ActionHandle[EmptyEcho | None], ...] [源代码]¶
- async set_group_ban(group_id: int, user_id: int, duration: int = 1800) tuple[ActionHandle[EmptyEcho | None], ...] [源代码]¶
- async set_group_anonymous_ban(group_id: int, anonymous: AnonymousDict, anonymous_flag: str, duration: int = 1800) tuple[ActionHandle[EmptyEcho | None], ...] [源代码]¶
- 参数:
group_id (int)
anonymous (AnonymousDict)
anonymous_flag (str)
duration (int)
- 返回类型:
tuple[ActionHandle[EmptyEcho | None], ...]
- async set_group_whole_ban(group_id: int, enable: bool = True) tuple[ActionHandle[EmptyEcho | None], ...] [源代码]¶
- 参数:
- 返回类型:
tuple[ActionHandle[EmptyEcho | None], ...]
- async set_group_admin(group_id: int, enable: bool = True) tuple[ActionHandle[EmptyEcho | None], ...] [源代码]¶
- 参数:
- 返回类型:
tuple[ActionHandle[EmptyEcho | None], ...]
- async set_group_anonymous(group_id: int, enable: bool = True) tuple[ActionHandle[EmptyEcho | None], ...] [源代码]¶
- 参数:
- 返回类型:
tuple[ActionHandle[EmptyEcho | None], ...]
- async set_group_card(group_id: int, user_id: int, card: str = '') tuple[ActionHandle[EmptyEcho | None], ...] [源代码]¶
- async set_group_name(group_id: int, name: str) tuple[ActionHandle[EmptyEcho | None], ...] [源代码]¶
- 参数:
- 返回类型:
tuple[ActionHandle[EmptyEcho | None], ...]
- async set_group_leave(group_id: int, is_dismiss: bool = False) tuple[ActionHandle[EmptyEcho | None], ...] [源代码]¶
- 参数:
- 返回类型:
tuple[ActionHandle[EmptyEcho | None], ...]
- async set_group_special_title(group_id: int, user_id: int, title: str = '', duration: int = -1) tuple[ActionHandle[EmptyEcho | None], ...] [源代码]¶
- async set_friend_add_request(add_flag: str, approve: bool = True, remark: str = '') tuple[ActionHandle[EmptyEcho | None], ...] [源代码]¶
- async set_group_add_request(add_flag: str, add_type: Literal['add', 'invite'], approve: bool = True, reason: str = '') tuple[ActionHandle[EmptyEcho | None], ...] [源代码]¶
- async get_login_info() tuple[ActionHandle[GetLoginInfoEcho], ...] [源代码]¶
- 返回类型:
tuple[ActionHandle[GetLoginInfoEcho], ...]
- async get_stranger_info(user_id: int, no_cache: bool = False) tuple[ActionHandle[GetStrangerInfoEcho], ...] [源代码]¶
- 参数:
- 返回类型:
tuple[ActionHandle[GetStrangerInfoEcho], ...]
- async get_friend_list() tuple[ActionHandle[GetFriendListEcho], ...] [源代码]¶
- 返回类型:
tuple[ActionHandle[GetFriendListEcho], ...]
- async get_group_info(group_id: int, no_cache: bool = False) tuple[ActionHandle[GetGroupInfoEcho], ...] [源代码]¶
- 参数:
- 返回类型:
tuple[ActionHandle[GetGroupInfoEcho], ...]
- async get_group_list() tuple[ActionHandle[GetGroupListEcho], ...] [源代码]¶
- 返回类型:
tuple[ActionHandle[GetGroupListEcho], ...]
- async get_group_member_info(group_id: int, user_id: int, no_cache: bool = False) tuple[ActionHandle[GetGroupMemberInfoEcho], ...] [源代码]¶
- 参数:
- 返回类型:
- async get_group_member_list(group_id: int) tuple[ActionHandle[GetGroupMemberListEcho], ...] [源代码]¶
- 参数:
group_id (int)
- 返回类型:
- async get_group_honor_info(group_id: int, type: Literal['talkative', 'performer', 'legend', 'strong_newbie', 'emotion', 'all']) tuple[ActionHandle[GetGroupHonorInfoEcho], ...] [源代码]¶
- 参数:
- 返回类型:
- async get_cookies(domain: str = '') tuple[ActionHandle[GetCookiesEcho], ...] [源代码]¶
- 参数:
domain (str)
- 返回类型:
tuple[ActionHandle[GetCookiesEcho], ...]
- async get_csrf_token() tuple[ActionHandle[GetCsrfTokenEcho], ...] [源代码]¶
- 返回类型:
tuple[ActionHandle[GetCsrfTokenEcho], ...]
- async get_credentials(domain: str = '') tuple[ActionHandle[GetCredentialsEcho], ...] [源代码]¶
- 参数:
domain (str)
- 返回类型:
tuple[ActionHandle[GetCredentialsEcho], ...]
- async get_record(file: str, out_format: str) tuple[ActionHandle[GetRecordEcho], ...] [源代码]¶
- 参数:
- 返回类型:
tuple[ActionHandle[GetRecordEcho], ...]
- async get_image(file: str) tuple[ActionHandle[GetImageEcho], ...] [源代码]¶
- 参数:
file (str)
- 返回类型:
tuple[ActionHandle[GetImageEcho], ...]
- async can_send_image() tuple[ActionHandle[CanSendImageEcho], ...] [源代码]¶
- 返回类型:
tuple[ActionHandle[CanSendImageEcho], ...]
- async can_send_record() tuple[ActionHandle[CanSendRecordEcho], ...] [源代码]¶
- 返回类型:
tuple[ActionHandle[CanSendRecordEcho], ...]
- async get_status() tuple[ActionHandle[GetStatusEcho], ...] [源代码]¶
- 返回类型:
tuple[ActionHandle[GetStatusEcho], ...]
- async get_version_info() tuple[ActionHandle[GetVersionInfoEcho], ...] [源代码]¶
- 返回类型:
tuple[ActionHandle[GetVersionInfoEcho], ...]
- async set_restart(delay: int = 0) tuple[ActionHandle[EmptyEcho | None], ...] [源代码]¶
- 参数:
delay (int)
- 返回类型:
tuple[ActionHandle[EmptyEcho | None], ...]
- async clean_cache() tuple[ActionHandle[EmptyEcho | None], ...] [源代码]¶
- 返回类型:
tuple[ActionHandle[EmptyEcho | None], ...]
v11 事件¶
- class melobot.protocols.onebot.v11.adapter.event.MessageEvent[源代码]¶
基类:
Event
- sender: _MessageSender | _GroupMessageSender¶
消息发送者
- sub_type: Literal['friend', 'group', 'other', 'normal', 'anonymous', 'notice', 'group_self'] | str¶
消息子类型
- class melobot.protocols.onebot.v11.adapter.event.PrivateMessageEvent[源代码]¶
基类:
MessageEvent
- sender: _MessageSender¶
消息发送者
- class melobot.protocols.onebot.v11.adapter.event.GroupMessageEvent[源代码]¶
基类:
MessageEvent
- sender: _GroupMessageSender¶
消息发送者
- anonymous: _MessageAnonymous | None¶
消息匿名信息段
- class melobot.protocols.onebot.v11.adapter.event.HeartBeatMetaEvent[源代码]¶
基类:
MetaEvent
- status: _MetaHeartBeatStatus¶
心跳元事件状态
- class melobot.protocols.onebot.v11.adapter.event.NoticeEvent[源代码]¶
基类:
Event
- class melobot.protocols.onebot.v11.adapter.event.GroupUploadNoticeEvent[源代码]¶
基类:
NoticeEvent
- file: _GroupUploadFile¶
文件描述对象
- class melobot.protocols.onebot.v11.adapter.event.GroupAdminNoticeEvent[源代码]¶
基类:
NoticeEvent
- class melobot.protocols.onebot.v11.adapter.event.GroupDecreaseNoticeEvent[源代码]¶
基类:
NoticeEvent
- class melobot.protocols.onebot.v11.adapter.event.GroupIncreaseNoticeEvent[源代码]¶
基类:
NoticeEvent
- class melobot.protocols.onebot.v11.adapter.event.GroupBanNoticeEvent[源代码]¶
基类:
NoticeEvent
- class melobot.protocols.onebot.v11.adapter.event.FriendAddNoticeEvent[源代码]¶
基类:
NoticeEvent
- class melobot.protocols.onebot.v11.adapter.event.GroupRecallNoticeEvent[源代码]¶
基类:
NoticeEvent
- class melobot.protocols.onebot.v11.adapter.event.FriendRecallNoticeEvent[源代码]¶
基类:
NoticeEvent
- class melobot.protocols.onebot.v11.adapter.event.NotifyNoticeEvent[源代码]¶
基类:
NoticeEvent
- class melobot.protocols.onebot.v11.adapter.event.FriendRequestEvent[源代码]¶
基类:
RequestEvent
- class melobot.protocols.onebot.v11.adapter.event.GroupRequestEvent[源代码]¶
基类:
RequestEvent
v11 消息段¶
各种消息段类型的 type 与 data 参数内容遵循 OneBot v11 原始文档规范,请自行查阅其文档。
# 例子:
seg: TextSegment = event.get_segments('text')
assert seg.type == 'text'
# 建议始终使用下标访问,而不是 get
# 使用下标,类型提示将会完美工作
assert seg.data['text'] == '<whatever>'
- class melobot.protocols.onebot.v11.adapter.segment.Segment[源代码]¶
基类:
Generic
[SegTypeT
,SegDataT
]- __init__(seg_type: SegTypeT, **seg_data: Any) None [源代码]¶
- 参数:
seg_type (SegTypeT)
seg_data (Any)
- 返回类型:
None
- classmethod add_type(seg_type_hint: type[TypeT], seg_data_hint: type[DataT]) type[_CustomSegInterface[TypeT, DataT]] [源代码]¶
- property type: SegTypeT¶
- property data: SegDataT¶
- class melobot.protocols.onebot.v11.adapter.segment.ImageSegment[源代码]¶
基类:
Segment
[Literal
['image'],_ImageSendData
|_ImageRecvData
]
- class melobot.protocols.onebot.v11.adapter.segment.ImageSendSegment[源代码]¶
基类:
ImageSegment
- __init__(*, file: str | Annotated[AnyUrl, UrlConstraints(max_length=None, allowed_schemes=['http', 'https', 'file', 'base64'], host_required=None, default_host=None, default_port=None, default_path=None)], type: Literal['flash'] | None = None, cache: Literal[0, 1] | None = None, proxy: Literal[0, 1] | None = None, timeout: int | None = None, **kwargs: Any) None [源代码]¶
- 参数:
file (str | Annotated[AnyUrl, UrlConstraints(max_length=None, allowed_schemes=['http', 'https', 'file', 'base64'], host_required=None, default_host=None, default_port=None, default_path=None)])
type (Literal['flash'] | None)
cache (Literal[0, 1] | None)
proxy (Literal[0, 1] | None)
timeout (int | None)
kwargs (Any)
- 返回类型:
None
- class melobot.protocols.onebot.v11.adapter.segment.ImageRecvSegment[源代码]¶
基类:
ImageSegment
- class melobot.protocols.onebot.v11.adapter.segment.RecordSegment[源代码]¶
基类:
Segment
[Literal
['record'],_RecordSendData
|_RecordRecvData
]
- class melobot.protocols.onebot.v11.adapter.segment.RecordSendSegment[源代码]¶
-
- __init__(*, file: str | Annotated[AnyUrl, UrlConstraints(max_length=None, allowed_schemes=['http', 'https', 'file', 'base64'], host_required=None, default_host=None, default_port=None, default_path=None)], magic: Literal[0, 1] | None = None, cache: Literal[0, 1] | None = None, proxy: Literal[0, 1] | None = None, timeout: int | None = None, **kwargs: Any) None [源代码]¶
- 参数:
file (str | Annotated[AnyUrl, UrlConstraints(max_length=None, allowed_schemes=['http', 'https', 'file', 'base64'], host_required=None, default_host=None, default_port=None, default_path=None)])
magic (Literal[0, 1] | None)
cache (Literal[0, 1] | None)
proxy (Literal[0, 1] | None)
timeout (int | None)
kwargs (Any)
- 返回类型:
None
- class melobot.protocols.onebot.v11.adapter.segment.VideoSegment[源代码]¶
基类:
Segment
[Literal
['video'],_VideoSendData
|_VideoRecvData
]
- class melobot.protocols.onebot.v11.adapter.segment.VideoSendSegment[源代码]¶
基类:
VideoSegment
- __init__(*, file: str | Annotated[AnyUrl, UrlConstraints(max_length=None, allowed_schemes=['http', 'https', 'file', 'base64'], host_required=None, default_host=None, default_port=None, default_path=None)], cache: Literal[0, 1] | None = None, proxy: Literal[0, 1] | None = None, timeout: int | None = None, **kwargs: Any) None [源代码]¶
- 参数:
- 返回类型:
None
- class melobot.protocols.onebot.v11.adapter.segment.VideoRecvSegment[源代码]¶
基类:
VideoSegment
- class melobot.protocols.onebot.v11.adapter.segment.PokeSendSegment[源代码]¶
基类:
PokeSegment
- class melobot.protocols.onebot.v11.adapter.segment.PokeRecvSegment[源代码]¶
基类:
PokeSegment
- class melobot.protocols.onebot.v11.adapter.segment.ContactSegment[源代码]¶
基类:
Segment
[Literal
['contact'],_ContactFriendData
|_ContactGroupData
]
- class melobot.protocols.onebot.v11.adapter.segment.MusicPlatformSegment[源代码]¶
基类:
MusicSegment
- class melobot.protocols.onebot.v11.adapter.segment.MusicCustomSegment[源代码]¶
基类:
MusicSegment
- class melobot.protocols.onebot.v11.adapter.segment.NodeSegment[源代码]¶
基类:
Segment
[Literal
['node'],_NodeReferData
|_NodeStdCustomDataInterface
|_NodeGocqCustomDataInterface
]
- class melobot.protocols.onebot.v11.adapter.segment.NodeReferSegment[源代码]¶
基类:
NodeSegment
- class melobot.protocols.onebot.v11.adapter.segment.NodeStdCustomSegment[源代码]¶
基类:
NodeSegment
- class melobot.protocols.onebot.v11.adapter.segment.NodeGocqCustomSegment[源代码]¶
基类:
NodeSegment
v11 行为(动作)¶
一般来说,无需手动通过这些原始的行为类构建行为,直接调用 Adapter
的输出方法即可。
但仍然提供这些接口用于高级操作。
- class melobot.protocols.onebot.v11.adapter.action.Action[源代码]¶
基类:
Action
v11 回应¶
各种回应类型的 data 参数内容遵循 OneBot v11 原始文档规范,请自行查阅其文档。
# 使用回应的例子:
handles = await adapter.with_echo(adapter.send)("Hello Melobot!")
# 当需要频繁使用时:
block_send = adapter.with_echo(adapter.send)
handles1 = await block_send("Hello Melobot!")
handles2 = await block_send("Hello OneBot!")
# 当然,或者这样:
with EchoRequireCtx().unfold(True):
handles1 = await adapter.send("Hello Melobot!")
handles2 = await adapter.send_image(
"test.jpg",
url="https://glowmem.com/static/avatar.jpg"
)
# 使用:
echo: SendMsgEcho = await handles[0]
# 建议始终使用下标访问,而不是 get
# 使用下标,类型提示将会完美工作
assert isinstance(echo.data['message_id'], int)