Learn R Programming

whapi (version 0.0.2)

whapi_send_image: Send an image via Whapi.Cloud (file, url, or base64)

Description

Sends an image using Whapi's POST /messages/image. Supports three input modes through type:

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

  • "url": direct http(s) URL

  • "base64": pre-built data URI (data:image/...;base64,...)

Usage

whapi_send_image(
  to,
  image,
  type = c("file", "url", "base64"),
  caption = 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.

image

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

type

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

caption

Optional caption text.

token

Bearer token (defaults to env var WHAPI_TOKEN if not given).

timeout

Numeric. Timeout in seconds. Default 30.

verbose

Logical. Show CLI messages? Default TRUE.

Examples

Run this code
if (FALSE) {
# Sys.setenv(WHAPI_TOKEN = "your_token_here")
 whapi_send_image("5581999999999", image = "card.png", type = "file", caption = "Card")
 whapi_send_image("5581999999999", image = "https://site.com/img.png", type = "url")
 b64 <- openssl::base64_encode(readBin("card.png","raw",file.info("card.png")$size))
 data_uri <- sprintf("data:image/png;name=%s;base64,%s", basename("card.png"), b64)
 whapi_send_image("5581999999999", image = data_uri, type = "base64")
}

Run the code above in your browser using DataLab