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 upload_file(
&self,
data: &[u8],
name: &str,
mime_type: &str,
) -> Result<UploadedFile, InvocationError>
pub async fn upload_file( &self, data: &[u8], name: &str, mime_type: &str, ) -> Result<UploadedFile, InvocationError>
Upload bytes as a file. Returns an UploadedFile that can be sent.
§Arguments
data— Raw file bytes.name— File name (e.g."photo.jpg").mime_type— MIME type (e.g."image/jpeg"). Used only for documents.
Sourcepub async fn upload_stream<R: AsyncRead + Unpin>(
&self,
reader: &mut R,
name: &str,
mime_type: &str,
) -> Result<UploadedFile, InvocationError>
pub async fn upload_stream<R: AsyncRead + Unpin>( &self, reader: &mut R, name: &str, mime_type: &str, ) -> Result<UploadedFile, InvocationError>
Upload from an async reader.
Sourcepub async fn send_file(
&self,
peer: Peer,
media: InputMedia,
caption: &str,
) -> Result<(), InvocationError>
pub async fn send_file( &self, peer: Peer, media: InputMedia, caption: &str, ) -> Result<(), InvocationError>
Send a file as a document or photo to a chat.
Use uploaded.as_photo_media() to send as a photo,
or uploaded.as_document_media() to send as a file.
Sourcepub async fn send_album(
&self,
peer: Peer,
items: Vec<(InputMedia, String)>,
) -> Result<(), InvocationError>
pub async fn send_album( &self, peer: Peer, items: Vec<(InputMedia, String)>, ) -> Result<(), InvocationError>
Send multiple files as an album (media group) in a single message.
All items must be photos or all must be documents (no mixing).
Sourcepub fn iter_download(&self, location: InputFileLocation) -> DownloadIter
pub fn iter_download(&self, location: InputFileLocation) -> DownloadIter
Create a download iterator for a media attachment.
let mut bytes = Vec::new();
if let Some(loc) = msg.download_location() {
let mut iter = client.iter_download(loc);
while let Some(chunk) = iter.next().await? {
bytes.extend_from_slice(&chunk);
}
}Sourcepub async fn download_media(
&self,
location: InputFileLocation,
) -> Result<Vec<u8>, InvocationError>
pub async fn download_media( &self, location: InputFileLocation, ) -> Result<Vec<u8>, InvocationError>
Download all bytes of a media attachment at once.
Source§impl Client
impl Client
Sourcepub async fn get_participants(
&self,
peer: Peer,
limit: i32,
) -> Result<Vec<Participant>, InvocationError>
pub async fn get_participants( &self, peer: Peer, limit: i32, ) -> Result<Vec<Participant>, InvocationError>
Fetch all participants of a chat, group or channel.
For channels this uses channels.getParticipants; for basic groups it
uses messages.getFullChat.
Returns up to limit participants; pass 0 for the default (200 for channels).
Sourcepub async fn kick_participant(
&self,
chat_id: i64,
user_id: i64,
) -> Result<(), InvocationError>
pub async fn kick_participant( &self, chat_id: i64, user_id: i64, ) -> Result<(), InvocationError>
Kick a user from a basic group (chat). For channels, use [ban_participant].
Sourcepub async fn ban_participant(
&self,
channel: Peer,
user_id: i64,
until_date: i32,
) -> Result<(), InvocationError>
pub async fn ban_participant( &self, channel: Peer, user_id: i64, until_date: i32, ) -> Result<(), InvocationError>
Ban a user from a channel or supergroup.
Pass until_date = 0 for a permanent ban.
Sourcepub async fn promote_participant(
&self,
channel: Peer,
user_id: i64,
promote: bool,
) -> Result<(), InvocationError>
pub async fn promote_participant( &self, channel: Peer, user_id: i64, promote: bool, ) -> Result<(), InvocationError>
Promote (or demote) a user to admin in a channel or supergroup.
Pass promote = true to grant admin rights, false to remove them.
Sourcepub async fn get_profile_photos(
&self,
peer: Peer,
limit: i32,
) -> Result<Vec<Photo>, InvocationError>
pub async fn get_profile_photos( &self, peer: Peer, limit: i32, ) -> Result<Vec<Photo>, InvocationError>
Iterate profile photos of a user or channel.
Returns a list of photo objects (up to limit).
Sourcepub async fn search_peer(
&self,
query: &str,
) -> Result<Vec<Peer>, InvocationError>
pub async fn search_peer( &self, query: &str, ) -> Result<Vec<Peer>, InvocationError>
Search for a peer (user, group, or channel) by name prefix.
Searches contacts, dialogs, and globally. Returns combined results.
Sourcepub async fn send_reaction(
&self,
peer: Peer,
message_id: i32,
reaction: &str,
) -> Result<(), InvocationError>
pub async fn send_reaction( &self, peer: Peer, message_id: i32, reaction: &str, ) -> Result<(), InvocationError>
Send a reaction to a message.
reaction should be an emoji string like "👍" or an empty string to remove.
Source§impl Client
impl Client
Sourcepub async fn get_difference(&self) -> Result<Vec<Update>, InvocationError>
pub async fn get_difference(&self) -> Result<Vec<Update>, InvocationError>
Fetch and apply any missed updates since the last known pts.
This should be called after reconnection to close any update gap. Returns the updates that were missed.
Sourcepub async fn sync_pts_state(&self) -> Result<(), InvocationError>
pub async fn sync_pts_state(&self) -> Result<(), InvocationError>
Fetch the current server update state and store it locally.
Sourcepub async fn check_and_fill_gap(
&self,
new_pts: i32,
pts_count: i32,
) -> Result<Vec<Update>, InvocationError>
pub async fn check_and_fill_gap( &self, new_pts: i32, pts_count: i32, ) -> Result<Vec<Update>, InvocationError>
Check for update gaps and fill them before processing an update with the given pts.
Returns any catch-up updates that were missed.
Source§impl Client
impl Client
Sourcepub fn iter_inline_queries(&self) -> InlineQueryIter
pub fn iter_inline_queries(&self) -> InlineQueryIter
Return an InlineQueryIter that yields every incoming inline query.
Internally this spawns the same update loop as stream_updates but
filters for Update::InlineQuery events only.
Source§impl Client
impl Client
Sourcepub async fn typing(&self, peer: Peer) -> Result<TypingGuard, InvocationError>
pub async fn typing(&self, peer: Peer) -> Result<TypingGuard, InvocationError>
Start a scoped typing indicator that auto-cancels when dropped.
This is a convenience wrapper around TypingGuard::start.
Sourcepub async fn uploading_document(
&self,
peer: Peer,
) -> Result<TypingGuard, InvocationError>
pub async fn uploading_document( &self, peer: Peer, ) -> Result<TypingGuard, InvocationError>
Start a scoped “uploading document” action that auto-cancels when dropped.
Sourcepub async fn recording_video(
&self,
peer: Peer,
) -> Result<TypingGuard, InvocationError>
pub async fn recording_video( &self, peer: Peer, ) -> Result<TypingGuard, InvocationError>
Start a scoped “recording video” action that auto-cancels when dropped.
Source§impl Client
impl Client
pub async fn connect(config: Config) -> Result<Self, InvocationError>
pub async fn save_session(&self) -> Result<(), InvocationError>
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.
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.
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.
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.
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 async fn get_me(&self) -> Result<User, InvocationError>
pub async fn get_me(&self) -> Result<User, InvocationError>
Fetch information about the logged-in user.
Sourcepub fn stream_updates(&self) -> UpdateStream
pub fn stream_updates(&self) -> UpdateStream
Return an UpdateStream that yields incoming Updates.
The reader task (started inside connect()) sends all updates to
inner.update_tx. This method proxies those updates into a fresh
caller-owned channel — typically called once per bot/app loop.
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 message to an already-resolved peer (plain text shorthand).
Sourcepub async fn send_message_to_peer_ex(
&self,
peer: Peer,
msg: &InputMessage,
) -> Result<(), InvocationError>
pub async fn send_message_to_peer_ex( &self, peer: Peer, msg: &InputMessage, ) -> Result<(), InvocationError>
Send a message with full InputMessage options.
Sourcepub async fn send_to_self(&self, text: &str) -> Result<(), InvocationError>
pub async fn send_to_self(&self, text: &str) -> Result<(), InvocationError>
Send directly to Saved Messages.
Sourcepub async fn edit_message(
&self,
peer: Peer,
message_id: i32,
new_text: &str,
) -> Result<(), InvocationError>
pub async fn edit_message( &self, peer: Peer, message_id: i32, new_text: &str, ) -> Result<(), InvocationError>
Edit an existing message.
Sourcepub async fn forward_messages(
&self,
destination: Peer,
message_ids: &[i32],
source: Peer,
) -> Result<(), InvocationError>
pub async fn forward_messages( &self, destination: Peer, message_ids: &[i32], source: Peer, ) -> Result<(), InvocationError>
Forward messages from source to destination.
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.
Sourcepub async fn get_messages_by_id(
&self,
peer: Peer,
ids: &[i32],
) -> Result<Vec<IncomingMessage>, InvocationError>
pub async fn get_messages_by_id( &self, peer: Peer, ids: &[i32], ) -> Result<Vec<IncomingMessage>, InvocationError>
Get messages by their IDs from a peer.
Sourcepub async fn get_pinned_message(
&self,
peer: Peer,
) -> Result<Option<IncomingMessage>, InvocationError>
pub async fn get_pinned_message( &self, peer: Peer, ) -> Result<Option<IncomingMessage>, InvocationError>
Get the pinned message in a chat.
Sourcepub async fn pin_message(
&self,
peer: Peer,
message_id: i32,
silent: bool,
unpin: bool,
pm_oneside: bool,
) -> Result<(), InvocationError>
pub async fn pin_message( &self, peer: Peer, message_id: i32, silent: bool, unpin: bool, pm_oneside: bool, ) -> Result<(), InvocationError>
Pin a message in a chat.
Sourcepub async fn unpin_message(
&self,
peer: Peer,
message_id: i32,
) -> Result<(), InvocationError>
pub async fn unpin_message( &self, peer: Peer, message_id: i32, ) -> Result<(), InvocationError>
Unpin a specific message.
Sourcepub async fn unpin_all_messages(
&self,
peer: Peer,
) -> Result<(), InvocationError>
pub async fn unpin_all_messages( &self, peer: Peer, ) -> Result<(), InvocationError>
Unpin all messages in a chat.
Sourcepub async fn search_messages(
&self,
peer: Peer,
query: &str,
limit: i32,
) -> Result<Vec<IncomingMessage>, InvocationError>
pub async fn search_messages( &self, peer: Peer, query: &str, limit: i32, ) -> Result<Vec<IncomingMessage>, InvocationError>
Search messages in a chat.
Sourcepub async fn search_global(
&self,
query: &str,
limit: i32,
) -> Result<Vec<IncomingMessage>, InvocationError>
pub async fn search_global( &self, query: &str, limit: i32, ) -> Result<Vec<IncomingMessage>, InvocationError>
Search messages globally across all chats.
Sourcepub async fn get_scheduled_messages(
&self,
peer: Peer,
) -> Result<Vec<IncomingMessage>, InvocationError>
pub async fn get_scheduled_messages( &self, peer: Peer, ) -> Result<Vec<IncomingMessage>, InvocationError>
Retrieve all scheduled messages in a chat.
Scheduled messages are messages set to be sent at a future time using
InputMessage::schedule_date. Returns them newest-first.
§Example
let scheduled = client.get_scheduled_messages(peer).await?;
for msg in &scheduled {
println!("Scheduled: {:?} at {:?}", msg.text(), msg.date());
}Sourcepub async fn delete_scheduled_messages(
&self,
peer: Peer,
ids: Vec<i32>,
) -> Result<(), InvocationError>
pub async fn delete_scheduled_messages( &self, peer: Peer, ids: Vec<i32>, ) -> Result<(), InvocationError>
Delete one or more scheduled messages by their IDs.
pub async fn answer_callback_query( &self, query_id: i64, text: Option<&str>, alert: bool, ) -> 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>
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, most recent first. Populates entity/message.
pub async fn delete_dialog(&self, peer: Peer) -> Result<(), InvocationError>
Sourcepub async fn mark_as_read(&self, peer: Peer) -> Result<(), InvocationError>
pub async fn mark_as_read(&self, peer: Peer) -> Result<(), InvocationError>
Mark all messages in a chat as read.
Sourcepub async fn clear_mentions(&self, peer: Peer) -> Result<(), InvocationError>
pub async fn clear_mentions(&self, peer: Peer) -> Result<(), InvocationError>
Clear unread mention markers.
Sourcepub async fn send_chat_action(
&self,
peer: Peer,
action: SendMessageAction,
) -> Result<(), InvocationError>
pub async fn send_chat_action( &self, peer: Peer, action: SendMessageAction, ) -> Result<(), InvocationError>
Send a chat action (typing indicator, uploading photo, etc).
For “typing” use tl::enums::SendMessageAction::Typing.
Sourcepub async fn join_chat(&self, peer: Peer) -> Result<(), InvocationError>
pub async fn join_chat(&self, peer: Peer) -> Result<(), InvocationError>
Join a public chat or channel by username/peer.
Sourcepub async fn accept_invite_link(
&self,
link: &str,
) -> Result<(), InvocationError>
pub async fn accept_invite_link( &self, link: &str, ) -> Result<(), InvocationError>
Accept and join via an invite link.
Sourcepub fn parse_invite_hash(link: &str) -> Option<&str>
pub fn parse_invite_hash(link: &str) -> Option<&str>
Extract hash from https://t.me/+HASH or https://t.me/joinchat/HASH.
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 a page of 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 to a tl::enums::Peer.
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, handling flood-wait retries.
Sourcepub fn iter_dialogs(&self) -> DialogIter
pub fn iter_dialogs(&self) -> DialogIter
Fetch dialogs page by page.
Returns a DialogIter that can be advanced with DialogIter::next.
This lets you page through all dialogs without loading them all at once.
§Example
let mut iter = client.iter_dialogs();
while let Some(dialog) = iter.next(&client).await? {
println!("{}", dialog.title());
}Sourcepub fn iter_messages(&self, peer: Peer) -> MessageIter
pub fn iter_messages(&self, peer: Peer) -> MessageIter
Fetch messages from a peer, page by page.
Returns a MessageIter that can be advanced with MessageIter::next.
§Example
let mut iter = client.iter_messages(peer);
while let Some(msg) = iter.next(&client).await? {
println!("{:?}", msg.text());
}Sourcepub async fn resolve_to_input_peer(
&self,
peer: &Peer,
) -> Result<InputPeer, InvocationError>
pub async fn resolve_to_input_peer( &self, peer: &Peer, ) -> Result<InputPeer, InvocationError>
Try to resolve a peer to InputPeer, returning an error if the access_hash is unknown (i.e. the peer has not been seen in any prior API call).
Sourcepub async fn invoke_on_dc<R: RemoteCall>(
&self,
dc_id: i32,
req: &R,
) -> Result<R::Return, InvocationError>
pub async fn invoke_on_dc<R: RemoteCall>( &self, dc_id: i32, req: &R, ) -> Result<R::Return, InvocationError>
Invoke a request on a specific DC, using the pool.
If the target DC has no auth key yet, one is acquired via DH and then
authorized via auth.exportAuthorization / auth.importAuthorization
so the worker DC can serve user-account requests too.
Trait Implementations§
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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more