Skip to main content

Client

Struct Client 

Source
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

Source

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 特征的对象
  • timeout API请求超时时间,若为 None 则一直等待
  • api_channel_cap API请求消息通道的容量,默认为16
  • event_channel_cap Event消息通道的容量,默认为16
Source§

impl Client

Source

pub async fn start_service(&self) -> ServiceStartResult<()>

启动服务 在 Client 实例构造完成或调用 change_service 后都需要调用该方法启动服务

Source

pub fn change_service( &mut self, service: impl IntoService, ) -> Box<dyn CommunicationService>

更换服务 即使在原服务启动后也可以更换服务 但需保证原服务被drop,即调用原服务的析构函数

§Examples
use std::time::Duration;
use onebot_api::communication::utils::Client;
use onebot_api::communication::ws::WsService;
use onebot_api::communication::ws_reverse::WsReverseService;

let ws_service = WsService::new("wss://example.com", Some("example_token".to_string())).unwrap();
let mut client = Client::new(ws_service, Duration::from_secs(5), None, None);
client.start_service().await.unwrap();

let ws_reverse_service = WsReverseService::new("0.0.0.0:8080", Some("example_token".to_string()));
client.change_service(ws_reverse_service);
client.start_service().await.unwrap();
Source

pub fn get_service(&self) -> &dyn CommunicationService

获取当前服务的引用

Source

pub fn get_service_mut(&mut self) -> &mut dyn CommunicationService

获取当前服务的可变引用

Source§

impl Client

Source

pub fn generate_id() -> String

随机生成uuid格式的id 用于echo的生成

Source

pub async fn get_response(&self, echo: String) -> Option<APIResponse>

自动获取 receiver 并监听带有指定 echo 的API调用结果

§Returns
  • Some(api_response) 成功获取API调用结果
  • None 监听过程中事件通道被关闭或监听超时
Source

pub fn parse_response<T: DeserializeOwned>( response: APIResponse, ) -> APIResult<T>

Source

pub async fn send_request( &self, action: String, params: JsonValue, echo: String, ) -> Result<(), SendError<APIRequest>>

Source

pub async fn send_and_parse<T: DeserializeOwned>( &self, action: impl ToString, params: JsonValue, ) -> APIResult<T>

生成echo并发送API请求
同时等待API响应并自动解析

§Params
  • action 要调用的 action 的名称
  • params 调用 action 所需要的参数
§Examples
use std::time::Duration;
use serde_json::{json, Value};
use onebot_api::communication::utils::Client;
use onebot_api::communication::ws::WsService;

let client: Client = Client::new(WsService::new("ws://localhost:8080", None).unwrap(), Some(Duration::from_secs(5)), None, None);
let response: Value =  client.send_and_parse("send_like", json!({})).await.unwrap();

Trait Implementations§

Source§

impl APISender for Client

Source§

fn send_private_msg<'life0, 'async_trait>( &'life0 self, user_id: i64, message: Vec<SendSegment>, auto_escape: Option<bool>, ) -> Pin<Box<dyn Future<Output = APIResult<i32>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Source§

fn send_group_msg<'life0, 'async_trait>( &'life0 self, group_id: i64, message: Vec<SendSegment>, auto_escape: Option<bool>, ) -> Pin<Box<dyn Future<Output = APIResult<i32>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Source§

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 = APIResult<i32>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Source§

fn delete_msg<'life0, 'async_trait>( &'life0 self, message_id: i32, ) -> Pin<Box<dyn Future<Output = APIResult<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Source§

fn get_msg<'life0, 'async_trait>( &'life0 self, message_id: i32, ) -> Pin<Box<dyn Future<Output = APIResult<GetMsgResponse>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Source§

fn get_forward_msg<'life0, 'async_trait>( &'life0 self, id: String, ) -> Pin<Box<dyn Future<Output = APIResult<Vec<ReceiveSegment>>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Source§

fn send_like<'life0, 'async_trait>( &'life0 self, user_id: i64, times: Option<i32>, ) -> Pin<Box<dyn Future<Output = APIResult<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Source§

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 = APIResult<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Source§

fn set_group_ban<'life0, 'async_trait>( &'life0 self, group_id: i32, user_id: i32, duration: Option<i32>, ) -> Pin<Box<dyn Future<Output = APIResult<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Source§

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 = APIResult<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Source§

fn set_group_whole_ban<'life0, 'async_trait>( &'life0 self, group_id: i32, enable: Option<bool>, ) -> Pin<Box<dyn Future<Output = APIResult<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Source§

fn set_group_admin<'life0, 'async_trait>( &'life0 self, group_id: i32, user_id: i32, enable: Option<bool>, ) -> Pin<Box<dyn Future<Output = APIResult<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Source§

fn set_group_anonymous<'life0, 'async_trait>( &'life0 self, group_id: i32, enable: Option<bool>, ) -> Pin<Box<dyn Future<Output = APIResult<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Source§

fn set_group_card<'life0, 'async_trait>( &'life0 self, group_id: i32, user_id: i32, card: Option<String>, ) -> Pin<Box<dyn Future<Output = APIResult<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Source§

fn set_group_name<'life0, 'async_trait>( &'life0 self, group_id: i32, group_name: String, ) -> Pin<Box<dyn Future<Output = APIResult<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Source§

fn set_group_leave<'life0, 'async_trait>( &'life0 self, group_id: i32, is_dismiss: Option<bool>, ) -> Pin<Box<dyn Future<Output = APIResult<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Source§

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 = APIResult<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Source§

fn set_friend_add_request<'life0, 'async_trait>( &'life0 self, flag: String, approve: Option<bool>, remark: Option<String>, ) -> Pin<Box<dyn Future<Output = APIResult<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Source§

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 = APIResult<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Source§

fn get_login_info<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = APIResult<GetLoginInfoResponse>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Source§

fn get_stranger_info<'life0, 'async_trait>( &'life0 self, user_id: i32, no_cache: Option<bool>, ) -> Pin<Box<dyn Future<Output = APIResult<GetStrangerInfoResponse>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Source§

fn get_friend_list<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = APIResult<Vec<GetFriendListResponse>>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Source§

fn get_group_info<'life0, 'async_trait>( &'life0 self, group_id: i32, no_cache: Option<bool>, ) -> Pin<Box<dyn Future<Output = APIResult<GetGroupInfoResponse>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Source§

fn get_group_list<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = APIResult<Vec<GetGroupInfoResponse>>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Source§

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 = APIResult<GetGroupMemberInfoResponse>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Source§

fn get_group_member_list<'life0, 'async_trait>( &'life0 self, group_id: i32, ) -> Pin<Box<dyn Future<Output = APIResult<Vec<GetGroupMemberInfoResponse>>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Source§

fn get_group_honor_info<'life0, 'async_trait>( &'life0 self, group_id: i64, honor_type: String, ) -> Pin<Box<dyn Future<Output = APIResult<GetGroupMemberInfoResponse>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Source§

fn get_cookies<'life0, 'async_trait>( &'life0 self, domain: Option<String>, ) -> Pin<Box<dyn Future<Output = APIResult<String>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Source§

fn get_csrf_token<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = APIResult<i32>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Source§

fn get_credentials<'life0, 'async_trait>( &'life0 self, domain: Option<String>, ) -> Pin<Box<dyn Future<Output = APIResult<GetCredentialsResponse>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Source§

fn get_record<'life0, 'async_trait>( &'life0 self, file: String, out_format: String, ) -> Pin<Box<dyn Future<Output = APIResult<String>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Source§

fn get_image<'life0, 'async_trait>( &'life0 self, file: String, ) -> Pin<Box<dyn Future<Output = APIResult<String>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Source§

fn can_send_image<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = APIResult<bool>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Source§

fn can_send_record<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = APIResult<bool>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Source§

fn get_status<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = APIResult<GetStatusResponse>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Source§

fn get_version_info<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = APIResult<GetVersionInfoResponse>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Source§

fn set_restart<'life0, 'async_trait>( &'life0 self, delay: Option<i32>, ) -> Pin<Box<dyn Future<Output = APIResult<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Source§

fn clean_cache<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = APIResult<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Source§

impl EventReceiver<Arc<Event>> for Client

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more