Learn R Programming

whapi (version 0.0.2)

whapi_slugify: whapi_slugify strings for safe IDs (e.g., button IDs in Whapi)

Description

Converts free-text labels into a safe "slug" format suitable for use as message button IDs or other identifiers in Whapi API requests. Ensures that IDs contain only lowercase letters, digits, and underscores, and are never empty (defaults to "btn" if the input is blank).

Usage

whapi_slugify(x)

Value

A character vector of the same length with slugified IDs.

Arguments

x

A character vector of labels.

Details

This function is particularly useful when creating interactive messages (buttons, lists) in WhatsApp via Whapi, where each button requires a valid id. By whapi_slugifying titles automatically, we can safely generate IDs even if users provide arbitrary labels with spaces, accents, or symbols.

Transformation steps:

  1. Convert to lowercase;

  2. Replace any sequence of non-alphanumeric characters with _;

  3. Trim leading/trailing underscores;

  4. Replace empty results with "btn".

See Also

Used internally in whapi_send_quick_reply() and other interactive message helpers.

Examples

Run this code
whapi_slugify(c("Yes!", "Call Us", "Sale!", "###"))
# -> "yes", "call_us", "promocao_rapida", "btn"

# Use case in button creation:
titles <- c("Buy Now", "Learn More")
ids <- whapi_slugify(titles)
tibble::tibble(title = titles, id = ids)

Run the code above in your browser using DataLab