Learn R Programming

whapi (version 0.0.2)

whapi_send_document: Send a DOCUMENT via Whapi.Cloud (file, url, or base64)

Description

Sends a document using Whapi's POST /messages/document. Supports three input modes via type:

  • "file" : local path -> reads bytes and builds a data URI (data:<mime>;name=<file>;base64,<...>);

  • "url" : direct http(s) URL;

  • "base64" : pre-built data URI (data:application/...;name=...;base64,...).

Usage

whapi_send_document(
  to,
  document,
  type = c("file", "url", "base64"),
  caption = NULL,
  filename = NULL,
  token = Sys.getenv("WHAPI_TOKEN", unset = ""),
  timeout = 30,
  verbose = TRUE
)

Value

A tibble with id, to, status, timestamp, timestamp_dt, and raw resp.

Arguments

to

Character(1). WhatsApp target (E.164 digits only, no "+") or chat id.

document

Character(1). File path (when type="file"), URL ("url"), or data URI ("base64").

type

One of c("file","url","base64"). Default: "file".

caption

Optional caption text.

filename

Optional filename shown to the user. If omitted:

  • type="file" -> uses basename(document);

  • type="url" -> uses the URL basename (without query/fragment);

  • type="base64" -> tries the name= part inside the data URI.

token

Bearer token (defaults to env var WHAPI_TOKEN).

timeout

Numeric. Request timeout in seconds. Default 30.

verbose

Logical. Print CLI messages? Default TRUE.

Examples

Run this code
if (FALSE) {
 Sys.setenv(WHAPI_TOKEN = "your_token_here")
 whapi_send_document("5581999999999", "report.pdf", type="file", caption="Monthly report")
 whapi_send_document("5581999999999", "https://example.com/contract.docx", type="url")
 b <- openssl::base64_encode(readBin("memo.odt","raw",file.info("memo.odt")$size))
 du <- sprintf("data:application/vnd.oasis.opendocument.text;name=%s;base64,%s",
  basename("memo.odt"), b)
 whapi_send_document("5581999999999", du, type="base64")
}

Run the code above in your browser using DataLab