Please check the build logs for more information.
See Builds for ideas on how to fix a failed build, or Metadata for how to configure docs.rs builds.
If you believe this is docs.rs' fault, open an issue.
layer-client
High-level async Telegram client for Rust.
Installation
[]
= "0.4.7"
= { = "1", = ["full"] }
Get your api_id and api_hash from my.telegram.org.
Feature Flags
= { = "0.4.7", = ["sqlite-session"] } # SQLite session
= { = "0.4.7", = ["libsql-session"] } # libsql / Turso
= { = "0.4.7", = ["html"] } # HTML parser
= { = "0.4.7", = ["html5ever"] } # html5ever parser
StringSessionBackend, InMemoryBackend, and BinaryFileBackend are always available.
Connecting
use Client;
let = builder
.api_id
.api_hash
.session
.catch_up
.connect
.await?;
ClientBuilder methods
| Method | Description |
|---|---|
.api_id(i32) |
Telegram API ID (required) |
.api_hash(str) |
Telegram API hash (required) |
.session(path) |
Binary file session at path |
.session_string(s) |
Portable base64 string session |
.in_memory() |
Non-persistent in-memory session |
.session_backend(Arc<dyn SessionBackend>) |
Custom backend |
.catch_up(bool) |
Replay missed updates on connect (default: false) |
.dc_addr(str) |
Override first DC address |
.socks5(Socks5Config) |
Route connections through SOCKS5 proxy |
.allow_ipv6(bool) |
Allow IPv6 DC addresses (default: false) |
.transport(TransportKind) |
MTProto transport (default: Abridged) |
.retry_policy(Arc<dyn RetryPolicy>) |
Override flood-wait retry policy |
.connect() |
Build and connect, returns (Client, ShutdownToken) |
Authentication
// Bot
if !client.is_authorized.await?
// User
use SignInError;
if !client.is_authorized.await?
// Sign out
client.sign_out.await?;
String Sessions
// Export
let session_string = client.export_session_string.await?;
// Restore
let = builder
.api_id
.api_hash
.session_string
.connect
.await?;
Update Stream
let mut stream = client.stream_updates;
while let Some = stream.next.await
IncomingMessage accessors: id(), text(), peer_id(), sender_id(), outgoing(), date(), edit_date(), mentioned(), silent(), pinned(), post(), raw.
Messaging
client.send_message.await?;
client.send_message.await?;
client.send_to_self.await?;
if let Some = msg.peer_id
client.edit_message.await?;
client.forward_messages.await?;
client.delete_messages.await?;
client.pin_message.await?;
client.unpin_message.await?;
client.unpin_all_messages.await?;
InputMessage Builder
use ;
use ;
let = parse_markdown;
let kb = new
.row;
client
.send_message_to_peer_ex
.await?;
| Method | Description |
|---|---|
InputMessage::text(str) |
Create with text |
.reply_to(Option<i32>) |
Reply to message ID |
.silent(bool) |
No notification sound |
.no_webpage(bool) |
Suppress link preview |
.schedule_date(Option<i32>) |
Schedule for Unix timestamp |
.entities(Vec<MessageEntity>) |
Formatted text entities |
.keyboard(impl Into<ReplyMarkup>) |
Inline or reply keyboard |
Keyboards
use ;
// Inline
let kb = new
.row;
// Reply
let kb = new
.row
.resize.single_use;
// Answer callback
CallbackQuery =>
Button types: callback, url, url_auth, switch_inline, switch_elsewhere, webview, simple_webview, request_phone, request_geo, request_poll, request_quiz, game, buy, copy_text, text.
Media
// Upload
let uploaded = client.upload_file.await?;
let uploaded = client.upload_file_concurrent.await?;
let uploaded = client.upload_stream.await?;
client.send_file.await?;
client.send_album.await?;
// Download
let bytes = client.download_media.await?;
client.download_media_to_file.await?;
Text Formatting
use ;
let = parse_markdown;
client.send_message_to_peer_ex.await?;
With html feature:
use ;
let = parse_html;
Reactions
use InputReactions;
client.send_reaction.await?;
client.send_reaction.await?;
client.send_reaction.await?;
Typing Guard
TypingGuard starts and stops typing indicators automatically on drop:
let _typing = client.typing.await?;
let _typing = client.uploading_document.await?;
let _typing = client.recording_video.await?;
use TypingGuard;
let _guard = start.await?;
Participants
use ;
let members = client.get_participants.await?;
client.kick_participant.await?;
client.ban_participant.await?;
client.promote_participant.await?;
Search
use MessagesFilter;
let results = client
.search
.filter
.limit
.fetch
.await?;
let global = client.search_global_builder.broadcasts_only.fetch.await?;
Dialogs
let dialogs = client.get_dialogs.await?;
let mut iter = client.iter_dialogs;
while let Some = iter.next.await?
let mut iter = client.iter_messages;
while let Some = iter.next.await?
client.mark_as_read.await?;
client.delete_dialog.await?;
Session Backends
builder.session // binary file
builder.in_memory // tests
builder.session_string // serverless
builder.session_backend
builder.session_backend
Transport and Networking
use ;
builder.transport // DPI bypass
builder.socks5
builder.socks5
client.invoke_on_dc.await?;
client.signal_network_restored;
Error Handling
use ;
match client.send_message.await
FLOOD_WAIT is handled automatically by AutoSleep. To disable:
use NoRetries;
builder.retry_policy
Raw API
use tl;
let req = SetBotCommands ;
client.invoke.await?;
client.invoke_on_dc.await?;
Shutdown
let = connect.await?;
shutdown.cancel; // graceful
client.disconnect; // immediate
Client Methods Reference
All async methods return Result<T, InvocationError> unless noted.
Connection & Session
| Method | Signature | Description |
|---|---|---|
Client::builder() |
ClientBuilder |
Fluent builder |
Client::connect() |
async (Config) → (Client, ShutdownToken) |
Low-level connect |
client.is_authorized() |
async → bool |
Check if logged in |
client.save_session() |
async → () |
Persist session |
client.export_session_string() |
async → String |
Export base64 session |
client.disconnect() |
sync | Immediate disconnect |
client.signal_network_restored() |
sync | Skip reconnect backoff |
Authentication
| Method | Signature | Description |
|---|---|---|
client.bot_sign_in(token) |
async → String |
Bot token login |
client.request_login_code(phone) |
async → LoginToken |
Start user login |
client.sign_in(token, code) |
async → String |
Complete user login |
client.check_password(token, pw) |
async → String |
Submit 2FA password |
client.sign_out() |
async → bool |
Log out |
client.get_me() |
async → tl::types::User |
Get own user |
Updates
| Method | Signature | Description |
|---|---|---|
client.stream_updates() |
sync → UpdateStream |
Typed async update stream |
stream.next() |
async → Option<Update> |
Next typed update |
stream.next_raw() |
async → Option<RawUpdate> |
Next raw TL update |
Messaging
| Method | Signature | Description |
|---|---|---|
client.send_message(peer, text) |
async → () |
Send text |
client.send_message_to_peer(peer, text) |
async → () |
Send text to peer |
client.send_message_to_peer_ex(peer, msg) |
async → () |
Send InputMessage |
client.send_to_self(text) |
async → () |
Send to Saved Messages |
client.edit_message(peer, id, text) |
async → () |
Edit a message |
client.forward_messages(from, to, ids) |
async → () |
Forward messages |
client.delete_messages(peer, ids) |
async → () |
Delete messages |
client.get_messages(peer, limit) |
async → Vec<Message> |
Fetch latest messages |
client.get_messages_by_id(peer, ids) |
async → Vec<Option<Message>> |
Fetch by ID |
client.pin_message(peer, id, notify) |
async → () |
Pin a message |
client.unpin_message(peer, id) |
async → () |
Unpin a message |
client.unpin_all_messages(peer) |
async → () |
Unpin all |
client.get_pinned_message(peer) |
async → Option<Message> |
Get pinned |
client.get_scheduled_messages(peer) |
async → Vec<Message> |
List scheduled |
client.delete_scheduled_messages(peer, ids) |
async → () |
Cancel scheduled |
Media
| Method | Signature | Description |
|---|---|---|
client.upload_file(name, bytes) |
async → UploadedFile |
Sequential upload |
client.upload_file_concurrent(name, bytes) |
async → UploadedFile |
Parallel upload |
client.upload_stream(name, reader) |
async → UploadedFile |
Upload from AsyncRead |
client.send_file(peer, file, as_photo) |
async → () |
Send uploaded file |
client.send_album(peer, files) |
async → () |
Send as album |
client.download_media(loc) |
async → Vec<u8> |
Download to memory |
client.download_media_to_file(loc, path) |
async → () |
Stream to file |
client.download_media_concurrent(loc) |
async → Vec<u8> |
Parallel download |
client.download(item) |
async → Vec<u8> |
Download Downloadable |
Chat Actions
| Method | Signature | Description |
|---|---|---|
client.typing(peer) |
async → TypingGuard |
RAII typing |
client.uploading_document(peer) |
async → TypingGuard |
RAII upload indicator |
client.recording_video(peer) |
async → TypingGuard |
RAII video indicator |
client.mark_as_read(peer) |
async → () |
Mark as read |
client.clear_mentions(peer) |
async → () |
Clear mention badges |
client.send_reaction(peer, msg_id, r) |
async → () |
Send reaction |
Dialogs & Peers
| Method | Signature | Description |
|---|---|---|
client.get_dialogs(limit) |
async → Vec<Dialog> |
Fetch dialogs |
client.iter_dialogs() |
sync → DialogIter |
Lazy dialog iterator |
client.iter_messages(peer) |
sync → MessageIter |
Lazy message iterator |
client.delete_dialog(peer) |
async → () |
Leave and delete |
client.join_chat(peer) |
async → () |
Join public chat |
client.accept_invite_link(link) |
async → () |
Accept invite |
client.resolve_peer(str) |
async → Peer |
Resolve username/phone |
client.resolve_username(str) |
async → Peer |
Resolve bare username |
Search
| Method | Signature | Description |
|---|---|---|
client.search(peer, query) |
sync → SearchBuilder |
In-chat search |
client.search_global_builder(query) |
sync → GlobalSearchBuilder |
Global search |
client.search_messages(peer, q, limit) |
async → Vec<IncomingMessage> |
Quick in-chat search |
client.search_global(q, limit) |
async → Vec<IncomingMessage> |
Quick global search |
Participants
| Method | Signature | Description |
|---|---|---|
client.get_participants(peer, limit) |
async → Vec<Participant> |
Fetch members |
client.iter_participants(peer) |
sync → impl Iterator |
Lazy iterator |
client.kick_participant(peer, user_id) |
async → () |
Kick |
client.ban_participant(peer, user_id, rights) |
async → () |
Ban/restrict |
client.promote_participant(peer, user_id, rights) |
async → () |
Promote admin |
client.get_permissions(peer, user_id) |
async → ParticipantPermissions |
Get permissions |
client.get_profile_photos(user_id, offset, limit) |
async → Vec<Photo> |
Fetch photos |
Raw & Advanced
| Method | Signature | Description |
|---|---|---|
client.invoke(req) |
async → R::Return |
Call any Layer 224 method |
client.invoke_on_dc(req, dc_id) |
async → R::Return |
Call on specific DC |
License
MIT or Apache-2.0, at your option. See LICENSE-MIT and LICENSE-APACHE.
Ankit Chaubey - github.com/ankit-chaubey · docs.rs/layer-client