wuapi 0.15.0

Rust SDK for wuapi, a WhatsApp API for developers. Link numbers by QR or pairing code, send and receive messages, manage chats, contacts, groups and channels, split them into projects and verify webhooks.
Documentation
// Generated by wuapi-codegen from the OpenAPI spec. Do not edit: run `bun run codegen`.

use crate::http::{HttpClient, Method, Request, RequestParts, encode_path};
use crate::pagination::Paginator;
use crate::types;

/// The `chats` methods.
#[derive(Clone, Debug)]
pub struct ChatsResource {
    http: HttpClient,
}

impl ChatsResource {
    pub(crate) fn new(http: HttpClient) -> Self {
        Self { http }
    }

    /// List chats
    ///
    /// The account's chats, the one with the newest message first: every conversation with a contact, group or channel that wuapi holds a message of, each with its latest message and WhatsApp's unread, pinned, archived and muted state. A state wuapi does not know yet is `null` (see `Chat`). The filters combine, and a page of combined filters may hold fewer chats than `limit` while `nextCursor` is set: keep following the cursor. With `q`, the chats that match, best match first.
    ///
    /// `GET /v1/accounts/{accountId}/chats`
    ///
    /// Safe to repeat: retried on timeouts, network errors, `429` and `5xx`.
    pub fn list(
        &self,
        account_id: &str,
        params: types::ChatsListParams,
    ) -> Paginator<types::ChatList> {
        let parts = RequestParts::new(Method::Get, format!("/v1/accounts/{}/chats", encode_path(account_id)))
            .query_opt("archived", params.archived.as_ref())
            .query_opt("unread", params.unread.as_ref())
            .query_opt("type", params.r#type.as_ref())
            .query_opt("q", params.q.as_ref())
            .query_opt("limit", params.limit.as_ref())
            .retryable();
        Paginator::new(self.http.clone(), parts, "cursor", params.cursor)
    }

    /// Get a chat
    ///
    /// One chat of the account, with its latest message and WhatsApp's unread, pinned, archived and muted state. A chat wuapi holds no message of answers `404 not_found`.
    ///
    /// `GET /v1/accounts/{accountId}/chats/{chatId}`
    ///
    /// Safe to repeat: retried on timeouts, network errors, `429` and `5xx`.
    pub fn get(&self, account_id: &str, chat_id: &str) -> Request<types::Chat> {
        let parts = RequestParts::new(Method::Get, format!("/v1/accounts/{}/chats/{}", encode_path(account_id), encode_path(chat_id)))
            .retryable();
        self.http.request(parts)
    }

    /// Show typing or recording
    ///
    /// Show (`typing`, `recording`) or clear (`paused`) the indicator in a chat.
    ///
    /// `POST /v1/accounts/{accountId}/chats/{chatId}/presence`
    ///
    /// Sends `Idempotency-Key` (a random key, or your own with `.idempotency_key(..)`), so it is retried on timeouts, network errors, `429` and `5xx` without repeating its effect.
    pub fn send_presence(
        &self,
        account_id: &str,
        chat_id: &str,
        params: types::ChatsSendPresenceParams,
    ) -> Request<()> {
        let parts = RequestParts::new(Method::Post, format!("/v1/accounts/{}/chats/{}/presence", encode_path(account_id), encode_path(chat_id)))
            .body(&params)
            .keyed()
            .no_content();
        self.http.request(parts)
    }

    /// Send read receipts
    ///
    /// Send read receipts (blue ticks) for messages in a chat. Without `messageIds`, every unread inbound message wuapi stores for the chat. In a group WhatsApp takes one receipt per author, so this makes one change per author.
    ///
    /// `POST /v1/accounts/{accountId}/chats/{chatId}/read`
    ///
    /// Sends `Idempotency-Key` (a random key, or your own with `.idempotency_key(..)`), so it is retried on timeouts, network errors, `429` and `5xx` without repeating its effect.
    pub fn send_read_receipts(
        &self,
        account_id: &str,
        chat_id: &str,
        params: types::ChatsSendReadReceiptsParams,
    ) -> Request<types::ChatRead> {
        let parts = RequestParts::new(Method::Post, format!("/v1/accounts/{}/chats/{}/read", encode_path(account_id), encode_path(chat_id)))
            .body(&params)
            .keyed();
        self.http.request(parts)
    }

    /// Mark a chat read
    ///
    /// Clear the chat's unread badge on the phone and other devices. Sends no read receipts; use `POST .../read` for those.
    ///
    /// `POST /v1/accounts/{accountId}/chats/{chatId}/mark-read`
    ///
    /// Sends `Idempotency-Key` (a random key, or your own with `.idempotency_key(..)`), so it is retried on timeouts, network errors, `429` and `5xx` without repeating its effect.
    pub fn mark_read(&self, account_id: &str, chat_id: &str) -> Request<()> {
        let parts = RequestParts::new(Method::Post, format!("/v1/accounts/{}/chats/{}/mark-read", encode_path(account_id), encode_path(chat_id)))
            .keyed()
            .no_content();
        self.http.request(parts)
    }

    /// Mark a chat unread
    ///
    /// Put the unread badge back on the chat.
    ///
    /// `POST /v1/accounts/{accountId}/chats/{chatId}/mark-unread`
    ///
    /// Sends `Idempotency-Key` (a random key, or your own with `.idempotency_key(..)`), so it is retried on timeouts, network errors, `429` and `5xx` without repeating its effect.
    pub fn mark_unread(&self, account_id: &str, chat_id: &str) -> Request<()> {
        let parts = RequestParts::new(Method::Post, format!("/v1/accounts/{}/chats/{}/mark-unread", encode_path(account_id), encode_path(chat_id)))
            .keyed()
            .no_content();
        self.http.request(parts)
    }

    /// Archive a chat
    ///
    /// Archive the chat on the phone and other devices.
    ///
    /// `POST /v1/accounts/{accountId}/chats/{chatId}/archive`
    ///
    /// Sends `Idempotency-Key` (a random key, or your own with `.idempotency_key(..)`), so it is retried on timeouts, network errors, `429` and `5xx` without repeating its effect.
    pub fn archive(&self, account_id: &str, chat_id: &str) -> Request<()> {
        let parts = RequestParts::new(Method::Post, format!("/v1/accounts/{}/chats/{}/archive", encode_path(account_id), encode_path(chat_id)))
            .keyed()
            .no_content();
        self.http.request(parts)
    }

    /// Unarchive a chat
    ///
    /// Move the chat out of the archive.
    ///
    /// `POST /v1/accounts/{accountId}/chats/{chatId}/unarchive`
    ///
    /// Sends `Idempotency-Key` (a random key, or your own with `.idempotency_key(..)`), so it is retried on timeouts, network errors, `429` and `5xx` without repeating its effect.
    pub fn unarchive(&self, account_id: &str, chat_id: &str) -> Request<()> {
        let parts = RequestParts::new(Method::Post, format!("/v1/accounts/{}/chats/{}/unarchive", encode_path(account_id), encode_path(chat_id)))
            .keyed()
            .no_content();
        self.http.request(parts)
    }

    /// Pin a chat
    ///
    /// Pin the chat to the top.
    ///
    /// `POST /v1/accounts/{accountId}/chats/{chatId}/pin`
    ///
    /// Sends `Idempotency-Key` (a random key, or your own with `.idempotency_key(..)`), so it is retried on timeouts, network errors, `429` and `5xx` without repeating its effect.
    pub fn pin(&self, account_id: &str, chat_id: &str) -> Request<()> {
        let parts = RequestParts::new(Method::Post, format!("/v1/accounts/{}/chats/{}/pin", encode_path(account_id), encode_path(chat_id)))
            .keyed()
            .no_content();
        self.http.request(parts)
    }

    /// Unpin a chat
    ///
    /// Unpin the chat.
    ///
    /// `POST /v1/accounts/{accountId}/chats/{chatId}/unpin`
    ///
    /// Sends `Idempotency-Key` (a random key, or your own with `.idempotency_key(..)`), so it is retried on timeouts, network errors, `429` and `5xx` without repeating its effect.
    pub fn unpin(&self, account_id: &str, chat_id: &str) -> Request<()> {
        let parts = RequestParts::new(Method::Post, format!("/v1/accounts/{}/chats/{}/unpin", encode_path(account_id), encode_path(chat_id)))
            .keyed()
            .no_content();
        self.http.request(parts)
    }

    /// Mute a chat
    ///
    /// Mute notifications for the chat, for `durationSeconds` or until unmuted.
    ///
    /// `POST /v1/accounts/{accountId}/chats/{chatId}/mute`
    ///
    /// Sends `Idempotency-Key` (a random key, or your own with `.idempotency_key(..)`), so it is retried on timeouts, network errors, `429` and `5xx` without repeating its effect.
    pub fn mute(
        &self,
        account_id: &str,
        chat_id: &str,
        params: types::ChatsMuteParams,
    ) -> Request<()> {
        let parts = RequestParts::new(Method::Post, format!("/v1/accounts/{}/chats/{}/mute", encode_path(account_id), encode_path(chat_id)))
            .body(&params)
            .keyed()
            .no_content();
        self.http.request(parts)
    }

    /// Unmute a chat
    ///
    /// Turn the chat's notifications back on.
    ///
    /// `POST /v1/accounts/{accountId}/chats/{chatId}/unmute`
    ///
    /// Sends `Idempotency-Key` (a random key, or your own with `.idempotency_key(..)`), so it is retried on timeouts, network errors, `429` and `5xx` without repeating its effect.
    pub fn unmute(&self, account_id: &str, chat_id: &str) -> Request<()> {
        let parts = RequestParts::new(Method::Post, format!("/v1/accounts/{}/chats/{}/unmute", encode_path(account_id), encode_path(chat_id)))
            .keyed()
            .no_content();
        self.http.request(parts)
    }

    /// Delete a chat
    ///
    /// Delete the chat on the linked devices. Messages stored in wuapi are kept.
    ///
    /// `DELETE /v1/accounts/{accountId}/chats/{chatId}`
    ///
    /// Safe to repeat: retried on timeouts, network errors, `429` and `5xx`.
    pub fn delete(
        &self,
        account_id: &str,
        chat_id: &str,
        params: types::ChatsDeleteParams,
    ) -> Request<()> {
        let parts = RequestParts::new(Method::Delete, format!("/v1/accounts/{}/chats/{}", encode_path(account_id), encode_path(chat_id)))
            .query_opt("deleteMedia", params.delete_media.as_ref())
            .retryable()
            .no_content();
        self.http.request(parts)
    }

    /// Set a chat's disappearing timer
    ///
    /// Messages in the chat disappear after `durationSeconds`; 0 turns it off.
    ///
    /// `PUT /v1/accounts/{accountId}/chats/{chatId}/disappearing-timer`
    ///
    /// Safe to repeat: retried on timeouts, network errors, `429` and `5xx`.
    pub fn set_disappearing_timer(
        &self,
        account_id: &str,
        chat_id: &str,
        params: types::ChatsSetDisappearingTimerParams,
    ) -> Request<()> {
        let parts = RequestParts::new(Method::Put, format!("/v1/accounts/{}/chats/{}/disappearing-timer", encode_path(account_id), encode_path(chat_id)))
            .body(&params)
            .retryable()
            .no_content();
        self.http.request(parts)
    }

    /// Label a chat
    ///
    /// WhatsApp Business accounts only; others answer `400 not_supported`.
    ///
    /// `POST /v1/accounts/{accountId}/chats/{chatId}/labels`
    ///
    /// Sends `Idempotency-Key` (a random key, or your own with `.idempotency_key(..)`), so it is retried on timeouts, network errors, `429` and `5xx` without repeating its effect.
    pub fn add_label(
        &self,
        account_id: &str,
        chat_id: &str,
        params: types::ChatsAddLabelParams,
    ) -> Request<()> {
        let parts = RequestParts::new(Method::Post, format!("/v1/accounts/{}/chats/{}/labels", encode_path(account_id), encode_path(chat_id)))
            .body(&params)
            .keyed()
            .no_content();
        self.http.request(parts)
    }

    /// Remove a label from a chat
    ///
    /// WhatsApp Business accounts only.
    ///
    /// `DELETE /v1/accounts/{accountId}/chats/{chatId}/labels/{labelId}`
    ///
    /// Safe to repeat: retried on timeouts, network errors, `429` and `5xx`.
    pub fn remove_label(&self, account_id: &str, chat_id: &str, label_id: &str) -> Request<()> {
        let parts = RequestParts::new(Method::Delete, format!("/v1/accounts/{}/chats/{}/labels/{}", encode_path(account_id), encode_path(chat_id), encode_path(label_id)))
            .retryable()
            .no_content();
        self.http.request(parts)
    }
}