Skip to main content

Bot

Struct Bot 

Source
pub struct Bot<C> { /* private fields */ }
Expand description

A cheaply cloneable handle to initialized SimpleX bot.

Implementations§

Source§

impl<C> Bot<C>

Source

pub fn client(&self) -> &C

Source

pub fn user_id(&self) -> UserId

Source§

impl<C: ClientApi> Bot<C>

Source

pub async fn init(client: C, settings: BotSettings) -> Result<Self, C::Error>

Source

pub fn wrap_client<W, F>(self, wrap: F) -> Bot<W>
where W: ClientApi, F: FnOnce(C) -> W,

This method allows ot wrap or replace the underlying bot client.

You can define your own clients implementing the ClientApi trait and then you can extend the bot functionalitty by implementing extension methods on Bot<YourCustomClient> type.

Source

pub fn default_profile(name: impl Into<String>) -> Profile

Returns a minimal bot profile with conservative defaults: no files, calls, reactions, or voice.

Source

pub async fn info(&self) -> Result<Arc<User>, C::Error>

Get full bot user info

Source

pub async fn initiate_connection( &self, link: impl Into<String>, ) -> Result<UndocumentedResponse<ConnectResponse>, C::Error>

Initiates the connection sequence.

Source

pub async fn check_connection_plan( &self, link: impl Into<String>, ) -> Result<Arc<ConnectionPlanResponse>, C::Error>

Inspect a SimpleX link before connecting: resolves its type (contact address, group link, or 1-time invitation) and reports whether the bot is already connected via it.

Create one-time-invitation link. Can be used for admin-access or for private connections with other bots. The PendingContactConnection::pcc_conn_id can be matched with crate::types::Connection::conn_id to recognize the user connected by this link when handling the crate::events::ContactConnected event(see crate::events::ContactConnected::contact)

Source

pub async fn create_address(&self) -> Result<String, C::Error>

Source

pub async fn address(&self) -> Result<String, C::Error>

Throws crate::types::errors::StoreError::UserContactLinkNotFound if bot doesn’t have an address. Use Self::get_or_create_address to ensure that address is available

Source

pub async fn get_or_create_address(&self) -> Result<String, C::Error>

Source

pub async fn configure_address( &self, settings: AddressSettings, ) -> Result<(), C::Error>

Source

pub async fn publish_address( &self, ) -> Result<Arc<UserProfileUpdatedResponse>, C::Error>

Make address visible in bot/user profile

Source

pub async fn hide_address( &self, ) -> Result<Arc<UserProfileUpdatedResponse>, C::Error>

Hide address from bot/user profile

Source

pub async fn delete_address(&self) -> Result<(), C::Error>

Source

pub async fn update_profile<F>( &self, updater: F, ) -> Result<ApiUpdateProfileResponse, C::Error>
where F: 'static + Send + FnOnce(&mut Profile),

Fetches the current profile and applies updater to it before saving.

Source

pub async fn set_display_name( &self, name: impl Into<String>, ) -> Result<ApiUpdateProfileResponse, C::Error>

Source

pub async fn set_full_name( &self, full_name: impl Into<String>, ) -> Result<ApiUpdateProfileResponse, C::Error>

Source

pub async fn set_bio( &self, bio: impl Into<String>, ) -> Result<ApiUpdateProfileResponse, C::Error>

Source

pub async fn set_avatar( &self, avatar: ImagePreview, ) -> Result<ApiUpdateProfileResponse, C::Error>

Set the bot/user avatar

Source

pub async fn set_peer_type( &self, peer_type: ChatPeerType, ) -> Result<ApiUpdateProfileResponse, C::Error>

Set account type Bot or Person

Source

pub async fn set_preferences( &self, preferences: Preferences, ) -> Result<ApiUpdateProfileResponse, C::Error>

Set global preferences

Source

pub async fn update_preferences<F>( &self, updater: F, ) -> Result<ApiUpdateProfileResponse, C::Error>
where F: 'static + Send + FnOnce(&mut Preferences),

Update global preferences via closure accepting current preferences

Source

pub async fn set_contact_preferences<CID: Into<ContactId>>( &self, contact_id: CID, preferences: Preferences, ) -> Result<Arc<ContactPrefsUpdatedResponse>, C::Error>

Set preferences for particular contact

Source

pub async fn tweak_preferences_for_contact<CID: Into<ContactId>, F>( &self, contact_id: CID, updater: F, ) -> Result<Arc<ContactPrefsUpdatedResponse>, C::Error>
where F: 'static + Send + FnOnce(&mut Preferences),

Tweak global preferences for particular contact via closure accepting current global preferences

Source

pub async fn contacts(&self) -> Result<Vec<Contact>, C::Error>

Get all contacts known to the bot(connected or not)

Source

pub async fn groups(&self) -> Result<Vec<GroupInfo>, C::Error>

Get all groups known to the bot

Source

pub async fn accept_contact<CRID: Into<ContactRequestId>>( &self, contact_request_id: CRID, ) -> Result<Arc<AcceptingContactRequestResponse>, <C as ClientApi>::Error>

Accept contact request

Source

pub async fn reject_contact<CRID: Into<ContactRequestId>>( &self, contact_request_id: CRID, ) -> Result<Arc<ContactRequestRejectedResponse>, <C as ClientApi>::Error>

Reject contact request

Source

pub fn send_msg<CID: Into<ChatId>, M: MessageLike>( &self, chat_id: CID, msg: M, ) -> MessageBuilder<'_, C, M::Kind>

Send a message. See the messages module for details

Source

pub fn multicast<I, M>( &self, chat_ids: I, msg: M, ) -> MulticastBuilder<'_, I, C, M::Kind>
where I: IntoIterator<Item = ChatId>, M: MessageLike,

Send the same message to multiple recepients

Source

pub async fn chat_ids(&self) -> Result<impl Iterator<Item = ChatId>, C::Error>

Returns a list of all known chat IDs

Source

pub async fn chat_ids_with<F>( &self, f: F, ) -> Result<impl 'static + Send + Iterator<Item = ChatId>, C::Error>
where F: 'static + Send + FnMut(&ChatId) -> bool,

Returns a list of all known chat IDs matching the filter f.

Source

pub async fn prepare_broadcast<M: MessageLike>( &self, msg: M, ) -> Result<MulticastBuilder<'_, impl 'static + Send + Iterator<Item = ChatId>, C, M::Kind>, C::Error>

Generate a MulticastBuilder that is ready to send messages to all known chats

bot.prepare_broadcast("Hey, what's up?!")
   .await
   .send()
   .await?;
Source

pub async fn prepare_broadcast_with<M, F>( &self, msg: M, f: F, ) -> Result<MulticastBuilder<'_, impl 'static + Send + Iterator<Item = ChatId>, C, M::Kind>, C::Error>
where F: 'static + Send + FnMut(&ChatId) -> bool, M: MessageLike,

Generate a MulticastBuilder that is ready to send messages to chats matching the filter

bot.prepare_broadcast_with("What do you think about this logo?", |chat| chat.is_direct())
   .await
   .with_image(Image::new("logo.jpg"))
   .send()
   .await?;
Source

pub async fn update_msg<CID: Into<ChatId>, MID: Into<MessageId>>( &self, chat_id: CID, message_id: MID, new_content: MsgContent, ) -> Result<ApiUpdateChatItemResponse, C::Error>

Source

pub async fn delete_msg<CID: Into<ChatId>, MID: Into<MessageId>>( &self, chat_id: CID, message_id: MID, mode: CIDeleteMode, ) -> Result<Arc<ChatItemsDeletedResponse>, C::Error>

Source

pub async fn batch_delete_msgs<CID: Into<ChatId>, I: IntoIterator<Item = MessageId>>( &self, chat_id: CID, message_ids: I, mode: CIDeleteMode, ) -> Result<Arc<ChatItemsDeletedResponse>, C::Error>

Source

pub async fn batch_msg_reactions<CID: Into<ChatId>, MID: Into<MessageId>, I: IntoIterator<Item = Reaction>>( &self, chat_id: CID, message_id: MID, reactions: I, ) -> Vec<Result<Arc<ChatItemReactionResponse>, C::Error>>

Applies multiple reactions to a message. Returns one result per reaction.

Source

pub async fn update_msg_reaction<CID: Into<ChatId>, MID: Into<MessageId>>( &self, chat_id: CID, message_id: MID, reaction: Reaction, ) -> Vec<Result<Arc<ChatItemReactionResponse>, C::Error>>

Source

pub fn accept_file<FID: Into<FileId>>( &self, file_id: FID, ) -> AcceptFileBuilder<'_, C>

Source

pub async fn reject_file<FID: Into<FileId>>( &self, file_id: FID, ) -> Result<CancelFileResponse, C::Error>

Source

pub async fn delete_chat<CID: Into<ChatId>>( &self, chat_id: CID, mode: DeleteMode, ) -> Result<ApiDeleteChatResponse, C::Error>

ChatId can be created from various types. See ChatId docs for the full list of From impls.

Source

pub async fn create_group( &self, profile: GroupProfile, ) -> Result<Arc<GroupCreatedResponse>, C::Error>

Create a new group. The bot’s user becomes the owner.

Source

pub async fn create_public_group<I: IntoIterator<Item = RelayId>>( &self, relay_ids: I, profile: GroupProfile, ) -> Result<ApiNewPublicGroupResponse, C::Error>

Create a new public group with relay members. The bot’s user becomes the owner. Relay IDs can be obtained from Bot::get_group_relays

Source

pub async fn set_auto_accept_member_contacts( &self, on: bool, ) -> Result<Arc<CmdOkResponse>, C::Error>

Enable or disable automatically accepting contacts from group members.

Source

pub async fn add_member<GID: Into<GroupId>, CID: Into<ContactId>>( &self, group_id: GID, contact_id: CID, role: GroupMemberRole, ) -> Result<Arc<SentGroupInvitationResponse>, C::Error>

Sends a group invitation to a contact.

Source

pub async fn join_group<GID: Into<GroupId>>( &self, group_id: GID, ) -> Result<Arc<UserAcceptedGroupSentResponse>, C::Error>

Accepts a pending group invitation.

Source

pub async fn accept_member<GID: Into<GroupId>, MID: Into<MemberId>>( &self, group_id: GID, member_id: MID, role: GroupMemberRole, ) -> Result<Arc<MemberAcceptedResponse>, C::Error>

Confirms a pending group membership request.

Source

pub async fn set_members_role<GID: Into<GroupId>, I: IntoIterator<Item = MemberId>>( &self, group_id: GID, member_ids: I, role: GroupMemberRole, ) -> Result<Arc<MembersRoleUserResponse>, C::Error>

Source

pub async fn set_member_role<GID: Into<GroupId>, MID: Into<MemberId>>( &self, group_id: GID, member_id: MID, role: GroupMemberRole, ) -> Result<Arc<MembersRoleUserResponse>, C::Error>

Source

pub async fn block_members_for_all<GID: Into<GroupId>, I: IntoIterator<Item = MemberId>>( &self, group_id: GID, member_ids: I, ) -> Result<Arc<MembersBlockedForAllUserResponse>, C::Error>

Blocks members so their messages are hidden for everyone in the group.

Source

pub async fn unblock_members_for_all<GID: Into<GroupId>, I: IntoIterator<Item = MemberId>>( &self, group_id: GID, member_ids: I, ) -> Result<Arc<MembersBlockedForAllUserResponse>, C::Error>

Reverses a previous block_members_for_all.

Source

pub async fn block_member_for_all<GID: Into<GroupId>, MID: Into<MemberId>>( &self, group_id: GID, member_id: MID, ) -> Result<Arc<MembersBlockedForAllUserResponse>, C::Error>

Blocks a member so their messages are hidden for everyone in the group.

Source

pub async fn unblock_member_for_all<GID: Into<GroupId>, MID: Into<MemberId>>( &self, group_id: GID, member_id: MID, ) -> Result<Arc<MembersBlockedForAllUserResponse>, C::Error>

Reverses a previous block_member_for_all.

Source

pub async fn remove_members<GID: Into<GroupId>, I: IntoIterator<Item = MemberId>>( &self, group_id: GID, member_ids: I, ) -> Result<Arc<UserDeletedMembersResponse>, C::Error>

Removes members from the group, preserving their past messages.

Source

pub async fn remove_members_with_messages<GID: Into<GroupId>, I: IntoIterator<Item = MemberId>>( &self, group_id: GID, member_ids: I, ) -> Result<Arc<UserDeletedMembersResponse>, C::Error>

Removes members from the group and deletes their messages.

Source

pub async fn remove_member<GID: Into<GroupId>, MID: Into<MemberId>>( &self, group_id: GID, member_id: MID, ) -> Result<Arc<UserDeletedMembersResponse>, C::Error>

Removes a member from the group, preserving their past messages.

Source

pub async fn remove_member_with_messages<GID: Into<GroupId>, MID: Into<MemberId>>( &self, group_id: GID, member_id: MID, ) -> Result<Arc<UserDeletedMembersResponse>, C::Error>

Removes a member from the group and deletes their messages.

Source

pub async fn leave_group<GID: Into<GroupId>>( &self, group_id: GID, ) -> Result<Arc<LeftMemberUserResponse>, C::Error>

Source

pub async fn list_members<GID: Into<GroupId>>( &self, group_id: GID, ) -> Result<Vec<GroupMember>, C::Error>

Source

pub async fn moderate_messages<GID: Into<GroupId>, I: IntoIterator<Item = MessageId>>( &self, group_id: GID, message_ids: I, ) -> Result<Arc<ChatItemsDeletedResponse>, C::Error>

Deletes messages for all group members. Requires admin or owner role.

Source

pub async fn moderate_message<GID: Into<GroupId>, MID: Into<MessageId>>( &self, group_id: GID, message_id: MID, ) -> Result<Arc<ChatItemsDeletedResponse>, C::Error>

Deletes a message for all group members. Requires admin or owner role.

Source

pub async fn update_group_profile<GID: Into<GroupId>>( &self, group_id: GID, profile: GroupProfile, ) -> Result<Arc<GroupUpdatedResponse>, C::Error>

Source

pub async fn set_group_custom_data<GID: Into<GroupId>>( &self, group_id: GID, data: Option<JsonObject>, ) -> Result<Arc<CmdOkResponse>, C::Error>

Stores arbitrary app-defined JSON on the group. Pass None to clear it.

Source

pub async fn set_contact_custom_data<CID: Into<ContactId>>( &self, contact_id: CID, data: Option<JsonObject>, ) -> Result<Arc<CmdOkResponse>, C::Error>

Stores arbitrary app-defined JSON on the contact. Pass None to clear it.

Changes the default role assigned to members who join via the group link.

Source

pub async fn get_group_relays<GID: Into<GroupId>>( &self, group_id: GID, ) -> GetGroupRelaysResponse<C>

Source

pub async fn add_group_relays<GID: Into<GroupId>, I: IntoIterator<Item = RelayId>>( &self, group_id: GID, relay_ids: I, ) -> AddGroupRelaysResponse<C>

Source

pub async fn add_group_relay<GID: Into<GroupId>, RID: Into<RelayId>>( &self, group_id: GID, relay_id: RID, ) -> AddGroupRelaysResponse<C>

Source

pub async fn get_chats( &self, pagination: PaginationByTime, query: ChatListQuery, ) -> Result<Arc<ApiChatsResponse>, C::Error>

Get chats with time-based pagination. Prefer this over Bot::contacts / Bot::groups for large databases as it avoids loading all records into memory at once.

Source§

impl Bot<Client>

Source

pub fn shutdown(self) -> impl Future<Output = ()>

Trait Implementations§

Source§

impl<C: Clone> Clone for Bot<C>

Source§

fn clone(&self) -> Bot<C>

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more

Auto Trait Implementations§

§

impl<C> Freeze for Bot<C>
where C: Freeze,

§

impl<C> RefUnwindSafe for Bot<C>
where C: RefUnwindSafe,

§

impl<C> Send for Bot<C>
where C: Send,

§

impl<C> Sync for Bot<C>
where C: Sync,

§

impl<C> Unpin for Bot<C>
where C: Unpin,

§

impl<C> UnsafeUnpin for Bot<C>
where C: UnsafeUnpin,

§

impl<C> UnwindSafe for Bot<C>
where C: UnwindSafe,

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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