wuapi 0.13.1

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;

/// An account is one WhatsApp number linked as a device, by QR code or by pairing code. Create one, show `qrCodeUrl` to the phone owner or have them type `pairingCode`, and wait for `ready`. Routes under `/v1/accounts/{accountId}` that talk to WhatsApp need the account `ready` (otherwise `409 account_not_ready`).
///
/// An account is billable once it has finished linking (`linkedAt` is set), until it is deleted or logged out.
#[derive(Clone, Debug)]
pub struct AccountsResource {
    http: HttpClient,
}

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

    /// List accounts
    ///
    /// Accounts in scope, newest first. Deleted accounts are not listed.
    ///
    /// `GET /v1/accounts`
    ///
    /// Safe to repeat: retried on timeouts, network errors, `429` and `5xx`.
    pub fn list(&self, params: types::AccountsListParams) -> Paginator<types::AccountList> {
        let parts = RequestParts::new(Method::Get, "/v1/accounts")
            .query_opt("limit", params.limit.as_ref())
            .query_opt("projectId", params.project_id.as_ref())
            .retryable();
        Paginator::new(self.http.clone(), parts, "cursor", params.cursor)
    }

    /// Create an account
    ///
    /// Start linking a new WhatsApp number. Choose where its residential proxy exits with `proxyLocation`, a pair from `GET /v1/proxy-locations` (ideally the number's own country). The account is returned with `status: initializing`. Poll `GET /v1/accounts/{accountId}` (or listen for `account.qr_code_issued`) until `status` is `qr_ready`, show `qrCodeUrl` to the phone owner (WhatsApp > Linked devices > Link a device), then wait for `ready`.
    ///
    /// To link by phone number instead, pass `pairingPhone`: the code appears as `pairingCode` (and the `account.pairing_code_issued` event) once the session is up. For a page someone else opens to link their own number, use `POST /v1/invitations`.
    ///
    /// Chat history is not imported unless you pass `historySync: recent`. WhatsApp sends recent history once, right after the number links; those messages are stored with `source: history` and `history.synced` fires.
    ///
    /// Requires an active subscription. Inside a project, `maxAccounts` applies.
    ///
    /// `POST /v1/accounts`
    ///
    /// 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 create(&self, params: types::AccountsCreateParams) -> Request<types::Account> {
        let parts = RequestParts::new(Method::Post, "/v1/accounts").body(&params).keyed();
        self.http.request(parts)
    }

    /// Get an account
    ///
    /// The account, with its current QR code or pairing code while it links.
    ///
    /// `GET /v1/accounts/{accountId}`
    ///
    /// Safe to repeat: retried on timeouts, network errors, `429` and `5xx`.
    pub fn get(&self, account_id: &str) -> Request<types::Account> {
        let parts = RequestParts::new(Method::Get, format!("/v1/accounts/{}", encode_path(account_id)))
            .retryable();
        self.http.request(parts)
    }

    /// Update an account
    ///
    /// Rename it, set automatic call rejection, or turn on and tune its pacing (anti-ban protections, off by default). Call settings are sent to the live session (the engine must know the account; it does not need to be `ready`). Pacing applies from the next send, without reconnecting. `imageQuality` applies to the images sent from then on. `historySync` is stored at once and applies to the next link: WhatsApp sends history only right after a number links. A `proxyLocation` change is checked before anything else is applied: a refused location (`400` or `429`) leaves the whole request unapplied.
    ///
    /// `PATCH /v1/accounts/{accountId}`
    ///
    /// Not idempotent: retried only when the request never reached the API or was refused with `429`.
    pub fn update(
        &self,
        account_id: &str,
        params: types::AccountsUpdateParams,
    ) -> Request<types::Account> {
        let parts = RequestParts::new(Method::Patch, format!("/v1/accounts/{}", encode_path(account_id)))
            .body(&params);
        self.http.request(parts)
    }

    /// Delete an account
    ///
    /// Log the session out on WhatsApp, delete the account and stop billing for it. Fires no event.
    ///
    /// `DELETE /v1/accounts/{accountId}`
    ///
    /// Safe to repeat: retried on timeouts, network errors, `429` and `5xx`.
    pub fn delete(&self, account_id: &str) -> Request<()> {
        let parts = RequestParts::new(Method::Delete, format!("/v1/accounts/{}", encode_path(account_id)))
            .retryable()
            .no_content();
        self.http.request(parts)
    }

    /// Reconnect an account
    ///
    /// Restart the session. Produces a fresh QR code (or pairing code) when the link is no longer valid.
    ///
    /// `POST /v1/accounts/{accountId}/reconnect`
    ///
    /// 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 reconnect(&self, account_id: &str) -> Request<types::Account> {
        let parts = RequestParts::new(Method::Post, format!("/v1/accounts/{}/reconnect", encode_path(account_id)))
            .keyed();
        self.http.request(parts)
    }

    /// Log an account out
    ///
    /// Unlink the phone and stop billing, keeping the account and its messages. Reconnect to link again.
    ///
    /// `POST /v1/accounts/{accountId}/logout`
    ///
    /// 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 logout(&self, account_id: &str) -> Request<types::Account> {
        let parts = RequestParts::new(Method::Post, format!("/v1/accounts/{}/logout", encode_path(account_id)))
            .keyed();
        self.http.request(parts)
    }

    /// Create a pairing code
    ///
    /// Link by phone number instead of QR code: returns an 8-character code the phone owner types in WhatsApp > Linked devices > Link a device > Link with phone number instead. Works while the account is not linked (a session the engine lost is recreated first). The code lives about 160 seconds; ask again after `expiresAt`.
    ///
    /// `POST /v1/accounts/{accountId}/pairing-code`
    ///
    /// 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 create_pairing_code(
        &self,
        account_id: &str,
        params: types::AccountsCreatePairingCodeParams,
    ) -> Request<types::PairingCode> {
        let parts = RequestParts::new(Method::Post, format!("/v1/accounts/{}/pairing-code", encode_path(account_id)))
            .body(&params)
            .keyed();
        self.http.request(parts)
    }

    /// Set online or offline
    ///
    /// Show the account as online or offline to contacts.
    ///
    /// `POST /v1/accounts/{accountId}/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 set_presence(
        &self,
        account_id: &str,
        params: types::AccountsSetPresenceParams,
    ) -> Request<()> {
        let parts = RequestParts::new(Method::Post, format!("/v1/accounts/{}/presence", encode_path(account_id)))
            .body(&params)
            .keyed()
            .no_content();
        self.http.request(parts)
    }

    /// Set the default disappearing timer
    ///
    /// The timer new chats start with.
    ///
    /// `PUT /v1/accounts/{accountId}/disappearing-timer`
    ///
    /// Safe to repeat: retried on timeouts, network errors, `429` and `5xx`.
    pub fn set_default_disappearing_timer(
        &self,
        account_id: &str,
        params: types::AccountsSetDefaultDisappearingTimerParams,
    ) -> Request<()> {
        let parts = RequestParts::new(Method::Put, format!("/v1/accounts/{}/disappearing-timer", encode_path(account_id)))
            .body(&params)
            .retryable()
            .no_content();
        self.http.request(parts)
    }
}