Skip to main content

fetch_chats/
fetch_chats.rs

1use beeper_desktop_api::BeeperClient;
2use std::env;
3
4#[tokio::main]
5async fn main() -> Result<(), Box<dyn std::error::Error>> {
6    // Get authentication token from environment variable
7    let token = env::var("BEEPER_TOKEN")
8        .unwrap_or_else(|_| {
9            eprintln!("Error: BEEPER_TOKEN environment variable not set");
10            eprintln!("Usage: BEEPER_TOKEN=your_token cargo run --example fetch_chats");
11            std::process::exit(1);
12        });
13
14    // Get API base URL from environment variable or use default
15    let base_url = env::var("BEEPER_API_URL")
16        .unwrap_or_else(|_| "http://localhost:23373".to_string());
17
18    println!("Connecting to Beeper Desktop API at: {}", base_url);
19    println!();
20
21    // Create a client with the provided token
22    let client = BeeperClient::new(&token, &base_url);
23
24    // Fetch all chats
25    println!("📋 Fetching chats from Beeper...");
26    let chats_response = client.list_chats(None, None).await?;
27
28    // Extract chat names into a vector using the display_name method
29    let chat_names: Vec<String> = chats_response
30        .items
31        .iter()
32        .map(|chat| chat.display_name())
33        .collect();
34
35    // Display results
36    println!("✅ Successfully retrieved {} chats:", chat_names.len());
37    println!();
38
39    if chat_names.is_empty() {
40        println!("No chats found.");
41    } else {
42        for (index, name) in chat_names.iter().enumerate() {
43            println!("  {}. {}", index + 1, name);
44        }
45    }
46
47    println!();
48    println!("Chat names as array:");
49    println!("{:#?}", chat_names);
50
51    Ok(())
52}