Skip to main content

Client

Struct Client 

Source
pub struct Client { /* private fields */ }
Expand description

The main Telegram client. Cheap to clone — internally Arc-wrapped.

Implementations§

Source§

impl Client

Source

pub async fn connect(config: Config) -> Result<Self, InvocationError>

Connect to Telegram and return a ready-to-use client.

Loads an existing session if the file exists, otherwise performs a full DH key exchange on DC2.

Source

pub async fn save_session(&self) -> Result<(), InvocationError>

Save the current session to disk.

Source

pub async fn is_authorized(&self) -> Result<bool, InvocationError>

Returns true if the client is already authorized.

Source

pub async fn bot_sign_in(&self, token: &str) -> Result<String, InvocationError>

Sign in as a bot using a bot token from @BotFather.

§Example
client.bot_sign_in("1234567890:ABCdef...").await?;
Source

pub async fn request_login_code( &self, phone: &str, ) -> Result<LoginToken, InvocationError>

Request a login code for a user account.

Returns a LoginToken to pass to sign_in.

Source

pub async fn sign_in( &self, token: &LoginToken, code: &str, ) -> Result<String, SignInError>

Complete sign-in with the code sent to the phone.

On 2FA accounts, returns Err(SignInError::PasswordRequired(token)). Pass the token to check_password.

Source

pub async fn check_password( &self, token: PasswordToken, password: impl AsRef<[u8]>, ) -> Result<String, InvocationError>

Complete 2FA login.

token comes from Err(SignInError::PasswordRequired(token)).

Source

pub async fn sign_out(&self) -> Result<bool, InvocationError>

Sign out and invalidate the current session.

Source

pub fn stream_updates(&self) -> UpdateStream

Return an UpdateStream that yields incoming Updates.

The stream must be polled regularly (e.g. in a while let loop) for the client to stay connected and receive updates. Multiple streams can be created but only one should be polled at a time.

Source

pub async fn send_message( &self, peer: &str, text: &str, ) -> Result<(), InvocationError>

Send a text message. Use "me" for Saved Messages.

Source

pub async fn send_message_to_peer( &self, peer: Peer, text: &str, ) -> Result<(), InvocationError>

Send a text message to an already-resolved layer_tl_types::enums::InputPeer.

Source

pub async fn send_to_self(&self, text: &str) -> Result<(), InvocationError>

Send a text message directly to “me” (Saved Messages).

Source

pub async fn delete_messages( &self, message_ids: Vec<i32>, revoke: bool, ) -> Result<(), InvocationError>

Delete messages by ID in a given peer.

Source

pub async fn answer_callback_query( &self, query_id: i64, text: Option<&str>, alert: bool, ) -> Result<bool, InvocationError>

Answer a callback query (from an inline button press).

Pass the query_id from update::CallbackQuery::query_id.

Source

pub async fn answer_inline_query( &self, query_id: i64, results: Vec<InputBotInlineResult>, cache_time: i32, is_personal: bool, next_offset: Option<String>, ) -> Result<bool, InvocationError>

Answer an inline query.

results should be a list of tl::enums::InputBotInlineResult.

Source

pub async fn get_dialogs( &self, limit: i32, ) -> Result<Vec<Dialog>, InvocationError>

Fetch up to limit dialogs (conversations), most recent first.

Returns a Vec<Dialog>. For paginated access, call repeatedly with offset parameters derived from the last result.

Source

pub async fn get_messages( &self, peer: InputPeer, limit: i32, offset_id: i32, ) -> Result<Vec<IncomingMessage>, InvocationError>

Fetch messages from a peer’s history.

Source

pub async fn resolve_peer(&self, peer: &str) -> Result<Peer, InvocationError>

Resolve a peer string ("me", "@username", phone, or numeric ID) to an InputPeer.

Source

pub async fn invoke<R: RemoteCall>( &self, req: &R, ) -> Result<R::Return, InvocationError>

Invoke any TL function directly.

Handles flood-wait and I/O retries according to the configured RetryPolicy.

Trait Implementations§

Source§

impl Clone for Client

Source§

fn clone(&self) -> Client

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more

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> 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.