Skip to main content

fetch_messages/
fetch_messages.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_messages");
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 to find the first one
25    println!("📋 Fetching chats...");
26    let chats_response = client.list_chats(None, None).await?;
27
28    if chats_response.items.is_empty() {
29        println!("❌ No chats found!");
30        return Ok(());
31    }
32
33    // Get the first chat
34    let first_chat = &chats_response.items[0];
35    let chat_id = &first_chat.id;
36    let chat_title = &first_chat.title;
37
38    println!("✅ Found {} chats", chats_response.items.len());
39    println!();
40    println!("đŸ“Ŧ Fetching messages from first chat: '{}'", chat_title);
41    println!("   Chat ID: {}", chat_id);
42    println!();
43
44    // Fetch messages from the first chat
45    let messages_response = client.list_messages(chat_id, None, None).await?;
46
47    println!("✅ Successfully retrieved {} messages:", messages_response.items.len());
48    println!();
49
50    if messages_response.items.is_empty() {
51        println!("No messages found in this chat.");
52    } else {
53        // Display messages with formatting
54        for (index, message) in messages_response.items.iter().enumerate() {
55            let sender = message.sender_name.as_deref().unwrap_or("Unknown");
56            let text = message.text.as_deref().unwrap_or("[No text content]");
57            let timestamp = &message.timestamp;
58
59            // Show attachments count if any
60            let attachment_info = if let Some(attachments) = &message.attachments {
61                format!(" [+{} attachment(s)]", attachments.len())
62            } else {
63                String::new()
64            };
65
66            // Show reactions count if any
67            let reaction_info = if let Some(reactions) = &message.reactions {
68                format!(" [+{} reaction(s)]", reactions.len())
69            } else {
70                String::new()
71            };
72
73            // Show reply indicator if replying to another message
74            let reply_info = if message.reply_to_id.is_some() {
75                " [â†Šī¸ Reply]".to_string()
76            } else {
77                String::new()
78            };
79
80            println!("  {}. [{}] {}: {}{}{}{}", 
81                index + 1, 
82                timestamp, 
83                sender, 
84                text,
85                attachment_info,
86                reaction_info,
87                reply_info
88            );
89        }
90    }
91
92    println!();
93    println!("Message details (JSON):");
94    println!("{:#?}", messages_response.items);
95
96    // Show pagination info if available
97    if messages_response.has_more {
98        println!();
99        println!("â„šī¸  More messages available. Use pagination to fetch older/newer messages.");
100    }
101
102    Ok(())
103}