Skip to main content

puniyu_adapter_api/
lib.rs

1//! # puniyu_adapter_api
2//!
3//! 统一的适配器 API trait 定义,描述协议层核心接口。
4//!
5//! ## 提供内容
6//!
7//! - [`AdapterApi`]:适配器基础 API trait,定义消息发送与元信息访问
8//! - [`OneBotAdapterApi`]:OneBot 协议 API trait,支持私聊与群消息
9//!
10//! ## 设计说明
11//!
12//! 每个 API 实例自包含适配器信息([`AdapterApi::adapter_info`])与账号信息([`AdapterApi::account_info`]),
13//! 实现 `OneBotAdapterApi` 后自动获得 `AdapterApi` 实现。
14//!
15
16use std::any::Any;
17
18use async_trait::async_trait;
19use puniyu_account::AccountInfo;
20use puniyu_adapter_types::{AdapterInfo, SendMsgType};
21use puniyu_common::Response;
22use puniyu_contact::ContactType;
23use puniyu_error::Result;
24use puniyu_message::Message;
25
26#[async_trait]
27pub trait AdapterApi: Any + Send + Sync {
28	async fn send_message(
29		&self,
30		contact: &ContactType<'_>,
31		message: &Message,
32	) -> Result<SendMsgType>;
33
34	fn adapter_info(&self) -> AdapterInfo;
35
36	fn account_info(&self) -> AccountInfo;
37
38	/// 调用适配器 API
39	/// 
40	/// ## 参数
41	/// - action: 以协议开头格式为协议_action, 例如onebot_send_msg
42	async fn call_api(
43		&self,
44		action: &str,
45		params: serde_json::Value,
46	) -> Result<Response<serde_json::Value>>;
47}