pub struct Client { /* private fields */ }Expand description
The main Telegram client. Cheap to clone — internally Arc-wrapped.
Implementations§
Source§impl Client
impl Client
Sourcepub async fn connect(config: Config) -> Result<Self, InvocationError>
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.
Sourcepub async fn save_session(&self) -> Result<(), InvocationError>
pub async fn save_session(&self) -> Result<(), InvocationError>
Save the current session to disk.
Returns true if the client is already authorized.
Sourcepub async fn bot_sign_in(&self, token: &str) -> Result<String, InvocationError>
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?;Sourcepub async fn request_login_code(
&self,
phone: &str,
) -> Result<LoginToken, InvocationError>
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.
Sourcepub async fn sign_in(
&self,
token: &LoginToken,
code: &str,
) -> Result<String, SignInError>
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.
Sourcepub async fn check_password(
&self,
token: PasswordToken,
password: impl AsRef<[u8]>,
) -> Result<String, InvocationError>
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)).
Sourcepub async fn sign_out(&self) -> Result<bool, InvocationError>
pub async fn sign_out(&self) -> Result<bool, InvocationError>
Sign out and invalidate the current session.
Sourcepub fn stream_updates(&self) -> UpdateStream
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.
Sourcepub async fn send_message(
&self,
peer: &str,
text: &str,
) -> Result<(), InvocationError>
pub async fn send_message( &self, peer: &str, text: &str, ) -> Result<(), InvocationError>
Send a text message. Use "me" for Saved Messages.
Sourcepub async fn send_message_to_peer(
&self,
peer: Peer,
text: &str,
) -> Result<(), InvocationError>
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.
Sourcepub async fn send_to_self(&self, text: &str) -> Result<(), InvocationError>
pub async fn send_to_self(&self, text: &str) -> Result<(), InvocationError>
Send a text message directly to “me” (Saved Messages).
Sourcepub async fn delete_messages(
&self,
message_ids: Vec<i32>,
revoke: bool,
) -> Result<(), InvocationError>
pub async fn delete_messages( &self, message_ids: Vec<i32>, revoke: bool, ) -> Result<(), InvocationError>
Delete messages by ID in a given peer.
Sourcepub async fn answer_callback_query(
&self,
query_id: i64,
text: Option<&str>,
alert: bool,
) -> Result<bool, InvocationError>
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.
Sourcepub 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>
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.
Sourcepub async fn get_dialogs(
&self,
limit: i32,
) -> Result<Vec<Dialog>, InvocationError>
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.
Sourcepub async fn get_messages(
&self,
peer: InputPeer,
limit: i32,
offset_id: i32,
) -> Result<Vec<IncomingMessage>, InvocationError>
pub async fn get_messages( &self, peer: InputPeer, limit: i32, offset_id: i32, ) -> Result<Vec<IncomingMessage>, InvocationError>
Fetch messages from a peer’s history.
Sourcepub async fn resolve_peer(&self, peer: &str) -> Result<Peer, InvocationError>
pub async fn resolve_peer(&self, peer: &str) -> Result<Peer, InvocationError>
Resolve a peer string ("me", "@username", phone, or numeric ID)
to an InputPeer.
Sourcepub async fn invoke<R: RemoteCall>(
&self,
req: &R,
) -> Result<R::Return, InvocationError>
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.