Sends an image using Whapi's POST /messages/image.
Supports three input modes through type:
"file": local path -> reads bytes and builds adata:<mime>;name=<file>;base64,<...>URI"url": directhttp(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
)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_TOKENif not given).- timeout
Numeric. Timeout in seconds. Default
30.- verbose
Logical. Show CLI messages? Default
TRUE.
Examples
if (FALSE) { # \dontrun{
# 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")
} # }
