Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Contacts & Blocking

Methods for managing your contact list and blocking/unblocking users.


Contacts

async client.get_contacts() → Result<Option<Vec<tl::enums::User>>, InvocationError>
Fetch the full contact list. Returns None when the server indicates the contact list is unchanged since the last fetch (the server uses a hash-based caching scheme). In practice always returns Some on the first call.
#![allow(unused)]
fn main() {
if let Some(contacts) = client.get_contacts().await? {
    for c in contacts {
        if let tl::enums::User::User(u) = c {
            println!("{} {}", u.first_name.unwrap_or_default(), u.last_name.unwrap_or_default());
        }
    }
}
}
async client.add_contact(user_id: i64, first_name: impl Into<String>, last_name: impl Into<String>, phone: impl Into<String>) → Result<(), InvocationError>
Add a user to your contact list. phone can be an empty string if you are adding by user ID rather than phone number. The name you supply is stored locally as your label for this contact, independent of the user's own profile name.
#![allow(unused)]
fn main() {
client.add_contact(user_id, "Alice", "Smith", "").await?;
}
async client.delete_contacts(user_ids: Vec<i64>) → Result<(), InvocationError>
Remove one or more users from your contact list. Passing an empty vec is a no-op.
#![allow(unused)]
fn main() {
client.delete_contacts(vec![user_a, user_b]).await?;
}
async client.search_contacts(query: impl Into<String>, limit: i32) → Result<Vec<tl::enums::Peer>, InvocationError>
Search for users, groups, and channels by name. Searches across contacts, dialogs, and global results. Returns a merged, deduplicated list ordered by relevance.
#![allow(unused)]
fn main() {
let results = client.search_contacts("John", 20).await?;
}

Import contacts

Import phone-number contacts in bulk. Each entry is (phone, first_name, last_name). Returns the raw ImportedContacts result containing imported IDs and resolved user objects.

#![allow(unused)]
fn main() {
let result = client.import_contacts(&[
    ("+15550001234", "Alice", "Smith"),
    ("+15550005678", "Bob",   "Jones"),
]).await?;

println!("Imported {} contacts", result.imported.len());
for user in &result.users {
    println!("  resolved: {user:?}");
}
}

result.retry_contacts contains entries that could not be resolved (e.g. the number is not registered on Telegram).


Blocking

async client.block(peer: impl Into<PeerRef>, block: bool) → Result<(), InvocationError>
Block a user. Blocked users cannot send you messages, see your phone number, or add you to groups. The block also suppresses their stories from your feed.
async → Result<(), InvocationError>
Remove a user from your block list.
async client.get_blocked_users(offset: i32, limit: i32) → Result<Vec<tl::enums::Peer>, InvocationError>
Paginate through your block list. Start with offset = 0. The server caps limit at 100.
#![allow(unused)]
fn main() {
let mut offset = 0;
loop {
    let page = client.get_blocked_users(offset, 100).await?;
    if page.is_empty() { break; }
    offset += page.len() as i32;
    for peer in &page {
        println!("{peer:?}");
    }
}
}

Full example

#![allow(unused)]
fn main() {
// Find and block all users named "Spammer"
let results = client.search_contacts("Spammer", 50).await?;
for peer in results {
    client.block(peer, true).await?;
    println!("Blocked {peer:?}");
}

// List current block list
let mut offset = 0;
loop {
    let page = client.get_blocked_users(offset, 100).await?;
    if page.is_empty() { break; }
    println!("Blocked users page: {page:?}");
    offset += page.len() as i32;
}
}