beeper_desktop_api/client/
messages.rs

1//! Message-related API operations
2
3use crate::models::{ListMessagesOutput, SendMessageInput, SendMessageOutput};
4use crate::error::Result;
5use super::{BeeperClient, handle_response};
6
7impl BeeperClient {
8    /// Lists all messages in a chat
9    ///
10    /// Paginated message list sorted by timestamp.
11    pub async fn list_messages(
12        &self,
13        chat_id: &str,
14        cursor: Option<&str>,
15        direction: Option<&str>,
16    ) -> Result<ListMessagesOutput> {
17        let mut url = format!(
18            "{}/v1/chats/{}/messages",
19            self.get_base_url(),
20            urlencoding::encode(chat_id)
21        );
22
23        if let Some(c) = cursor {
24            url.push_str(&format!("?cursor={}", urlencoding::encode(c)));
25            if direction.is_some() {
26                url.push('&');
27            }
28        } else if direction.is_some() {
29            url.push('?');
30        }
31
32        if let Some(d) = direction {
33            url.push_str(&format!("direction={}", d));
34        }
35
36        let response = self
37            .get_http_client()
38            .get(&url)
39            .header("Authorization", self.get_auth_header())
40            .send()
41            .await?;
42
43        handle_response(response).await
44    }
45
46    /// Sends a message to a chat
47    ///
48    /// Sends a text message to a specific chat. Supports replying to existing messages.
49    /// Returns the sent message ID.
50    pub async fn send_message(&self, chat_id: &str, input: SendMessageInput) -> Result<SendMessageOutput> {
51        let url = format!(
52            "{}/v1/chats/{}/messages",
53            self.get_base_url(),
54            urlencoding::encode(chat_id)
55        );
56
57        let response = self
58            .get_http_client()
59            .post(&url)
60            .header("Authorization", self.get_auth_header())
61            .json(&input)
62            .send()
63            .await?;
64
65        handle_response(response).await
66    }
67}