pub struct Client { /* private fields */ }Expand description
对于Onebot V11协议API调用和事件接收的高层抽象
需要具体实现 CommunicationService 的底层服务支持
§Examples
use std::time::Duration;
use onebot_api::communication::utils::Client;
use onebot_api::communication::ws::WsService;
let ws_service = WsService::new("wss://example.com", Some("example_token".to_string())).unwrap();
let client = Client::new(ws_service, Duration::from_secs(5), None, None);
client.start_service().await.unwrap();Implementations§
Source§impl Client
impl Client
Sourcepub fn new(
service: impl IntoService,
timeout: Option<Duration>,
api_channel_cap: Option<usize>,
event_channel_cap: Option<usize>,
) -> Self
pub fn new( service: impl IntoService, timeout: Option<Duration>, api_channel_cap: Option<usize>, event_channel_cap: Option<usize>, ) -> Self
创建一个 Client 实例
§Params
service实现IntoService特征或CommunicationService特征的对象timeoutAPI请求超时时间,若为None则一直等待api_channel_capAPI请求消息通道的容量,默认为16event_channel_capEvent消息通道的容量,默认为16
Source§impl Client
impl Client
pub async fn start_service(&self) -> Result<()>
pub fn change_service( &mut self, service: impl IntoService, ) -> Box<dyn CommunicationService>
pub fn get_service(&self) -> &dyn CommunicationService
pub fn get_service_mut(&mut self) -> &mut dyn CommunicationService
Source§impl Client
impl Client
pub fn generate_id() -> String
pub async fn get_response(&self, echo: String) -> Option<APIResponse>
pub fn parse_response<T: DeserializeOwned>(response: APIResponse) -> Result<T>
pub async fn send_request( &self, action: String, params: JsonValue, echo: String, ) -> Result<(), SendError<APIRequest>>
Sourcepub async fn send_and_parse<T: DeserializeOwned>(
&self,
action: impl ToString,
params: JsonValue,
) -> Result<T>
pub async fn send_and_parse<T: DeserializeOwned>( &self, action: impl ToString, params: JsonValue, ) -> Result<T>
生成echo并发送API请求
同时等待API响应并自动解析
§Examples
use serde_json::{json, Value};
use onebot_api::communication::utils::Client;
let client: Client = /* ... */;
let response: Value = client.send_and_parse("action_name", json!({})).await.unwrap();Trait Implementations§
Source§impl APISender for Client
impl APISender for Client
fn send_private_msg<'life0, 'async_trait>(
&'life0 self,
user_id: i64,
message: Vec<SendSegment>,
auto_escape: Option<bool>,
) -> Pin<Box<dyn Future<Output = Result<i32>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn send_group_msg<'life0, 'async_trait>(
&'life0 self,
group_id: i64,
message: Vec<SendSegment>,
auto_escape: Option<bool>,
) -> Pin<Box<dyn Future<Output = Result<i32>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn send_msg<'life0, 'async_trait>(
&'life0 self,
message_type: Option<MessageType>,
user_id: i64,
group_id: i64,
message: Vec<SendSegment>,
auto_escape: Option<bool>,
) -> Pin<Box<dyn Future<Output = Result<i32>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn delete_msg<'life0, 'async_trait>(
&'life0 self,
message_id: i32,
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn get_msg<'life0, 'async_trait>(
&'life0 self,
message_id: i32,
) -> Pin<Box<dyn Future<Output = Result<GetMsgResponse>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn get_forward_msg<'life0, 'async_trait>(
&'life0 self,
id: String,
) -> Pin<Box<dyn Future<Output = Result<Vec<ReceiveSegment>>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn send_like<'life0, 'async_trait>(
&'life0 self,
user_id: i64,
times: Option<i32>,
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn set_group_kick<'life0, 'async_trait>(
&'life0 self,
group_id: i32,
user_id: i32,
reject_add_request: Option<bool>,
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn set_group_ban<'life0, 'async_trait>(
&'life0 self,
group_id: i32,
user_id: i32,
duration: Option<i32>,
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn set_group_anonymous_ban<'life0, 'async_trait>(
&'life0 self,
group_id: i32,
anonymous: Option<GroupMessageAnonymous>,
flag: Option<String>,
duration: Option<i32>,
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn set_group_whole_ban<'life0, 'async_trait>(
&'life0 self,
group_id: i32,
enable: Option<bool>,
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn set_group_admin<'life0, 'async_trait>(
&'life0 self,
group_id: i32,
user_id: i32,
enable: Option<bool>,
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn set_group_anonymous<'life0, 'async_trait>(
&'life0 self,
group_id: i32,
enable: Option<bool>,
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn set_group_card<'life0, 'async_trait>(
&'life0 self,
group_id: i32,
user_id: i32,
card: Option<String>,
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn set_group_name<'life0, 'async_trait>(
&'life0 self,
group_id: i32,
group_name: String,
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn set_group_leave<'life0, 'async_trait>(
&'life0 self,
group_id: i32,
is_dismiss: Option<bool>,
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn set_group_special_title<'life0, 'async_trait>(
&'life0 self,
group_id: i32,
user_id: i32,
special_title: Option<String>,
duration: Option<i32>,
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn set_friend_add_request<'life0, 'async_trait>(
&'life0 self,
flag: String,
approve: Option<bool>,
remark: Option<String>,
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn set_group_add_request<'life0, 'async_trait>(
&'life0 self,
flag: String,
sub_type: String,
approve: Option<bool>,
reason: Option<String>,
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn get_login_info<'life0, 'async_trait>(
&'life0 self,
) -> Pin<Box<dyn Future<Output = Result<GetLoginInfoResponse>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn get_stranger_info<'life0, 'async_trait>(
&'life0 self,
user_id: i32,
no_cache: Option<bool>,
) -> Pin<Box<dyn Future<Output = Result<GetStrangerInfoResponse>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn get_friend_list<'life0, 'async_trait>(
&'life0 self,
) -> Pin<Box<dyn Future<Output = Result<Vec<GetFriendListResponse>>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn get_group_info<'life0, 'async_trait>(
&'life0 self,
group_id: i32,
no_cache: Option<bool>,
) -> Pin<Box<dyn Future<Output = Result<GetGroupInfoResponse>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn get_group_list<'life0, 'async_trait>(
&'life0 self,
) -> Pin<Box<dyn Future<Output = Result<Vec<GetGroupInfoResponse>>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn get_group_member_info<'life0, 'async_trait>(
&'life0 self,
group_id: i32,
user_id: i32,
no_cache: Option<bool>,
) -> Pin<Box<dyn Future<Output = Result<GetGroupMemberInfoResponse>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn get_group_member_list<'life0, 'async_trait>(
&'life0 self,
group_id: i32,
) -> Pin<Box<dyn Future<Output = Result<Vec<GetGroupMemberInfoResponse>>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn get_group_honor_info<'life0, 'async_trait>(
&'life0 self,
group_id: i64,
honor_type: String,
) -> Pin<Box<dyn Future<Output = Result<GetGroupMemberInfoResponse>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn get_csrf_token<'life0, 'async_trait>(
&'life0 self,
) -> Pin<Box<dyn Future<Output = Result<i32>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn get_credentials<'life0, 'async_trait>(
&'life0 self,
domain: Option<String>,
) -> Pin<Box<dyn Future<Output = Result<GetCredentialsResponse>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn get_record<'life0, 'async_trait>(
&'life0 self,
file: String,
out_format: String,
) -> Pin<Box<dyn Future<Output = Result<String>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn get_image<'life0, 'async_trait>(
&'life0 self,
file: String,
) -> Pin<Box<dyn Future<Output = Result<String>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn can_send_image<'life0, 'async_trait>(
&'life0 self,
) -> Pin<Box<dyn Future<Output = Result<bool>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn can_send_record<'life0, 'async_trait>(
&'life0 self,
) -> Pin<Box<dyn Future<Output = Result<bool>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn get_status<'life0, 'async_trait>(
&'life0 self,
) -> Pin<Box<dyn Future<Output = Result<GetStatusResponse>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn get_version_info<'life0, 'async_trait>(
&'life0 self,
) -> Pin<Box<dyn Future<Output = Result<GetVersionInfoResponse>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn set_restart<'life0, 'async_trait>(
&'life0 self,
delay: Option<i32>,
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn clean_cache<'life0, 'async_trait>(
&'life0 self,
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
Source§impl EventReceiver<Arc<Event>> for Client
impl EventReceiver<Arc<Event>> for Client
fn get_receiver(&self) -> EventReceiver
Auto Trait Implementations§
impl Freeze for Client
impl !RefUnwindSafe for Client
impl Send for Client
impl Sync for Client
impl Unpin for Client
impl UnsafeUnpin for Client
impl !UnwindSafe for Client
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more